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
- You use thumbnail which maintains
the aspect
- You re expecting the
image to be 40 x 40
- You re cropping beyond the actual size of the thumbnail
- 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.