views:

41

answers:

1

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.

+1  A: 

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.

manifest
Thanks @manifest ; my code already defends against situations like yours and resized to the exact size of the crop. The issue wasn't with a black line, but that there was an artifacted line that was either black with specks of color or did not seem to be resized.
jvanasco
Well I'm going from a 1600 x 1200 to 50 x 37 thumbnail, than I changed my crop above to have the "lower" 37. Black line is gone and no artifacts. I'm not sure what your issue might be, but good luck!
manifest
here's a pastebin of the code that i use:http://pastebin.com/T3zMemTvthanks to @bobince , i'll refactor the thumbnail() to resize() for all situations -- not just the crop methods.feel free to use this code it if it helps you at all.
jvanasco