English 中文(简体)
Does PIL create artifacting on the bottom edge of the image when you thumbnail() then crop()? If so, what is your workaround?
原标题:

When I call PIL to thumbnail() an image, and then crop(), I get artifacting on the last row of pixels -- they re either mostly black with specks of intense color, or what seems to be a not-resized area of the image ( ie, that line of pixels is at the original resolution and did not scale down with the rest )

This does not seem to happen on a thumbnail() without a crop. This happens whether or not I call a load() on the cropped image either.

To get around this visually, i ve been thumbnailing to a 1pixel larger image, and then cropping to the same size. That seems to work. It s kind of a dirty hack though. I m wondering if there s a proper fix.

问题回答

Yes, this happens to me also. This was a learning exercise for me because I have never cropped or created thumbnails using the PIL...

thumbnail(size,filter=None)

Replaces the original image, in place, with a new image of the given size (p. 2). The optional filter argument works in the same way as in the .resize() method. The aspect ratio (height : width) is preserved by this operation. The resulting image will be as large as possible while still fitting within the given size. For example, if image im has size (400,150), its size after im.thumbnail((40,40)) will be (40,15).

So what is happening is

  1. You use thumbnail which maintains the aspect
  2. You re expecting the image to be 40 x 40
  3. You re cropping beyond the actual size of the thumbnail
  4. A black strip most likely on the bottom due to cropping beyond the size

Code I wrote to repeat the issue:

def croptest(file, width, height):
    import Image as pil
    import os

    max_width = width
    max_height = height
    file, ext = os.path.splitext(file)

    img = pil.open(file)
    img.thumbnail((max_width, max_height), pil.ANTIALIAS)
    img.save(file + ".thumb.jpeg",  JPEG )
    croppedImage = img.crop((10, 10, 40, 40))
    croppedImage.save(file + ".croppedthumb.jpeg",  JPEG )

if __name__ == "__main__":
   croptest("Desktop.bmp", 50, 50)

Desktop.thumb.jpeg was 50 x 37 whereas Desktop.croppedthumb.jpeg was 30 x 30 so I had a 3 pixel high black line across the bottom.

Your solution would be either to crop inside the actual size of the thumbnail or figure out how to create a thumbnail ignoring the aspect ratio.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签