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...
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With