Does PIL create artifact at the bottom of the image with thumbnail (), then crop ()? If so, what is your workaround?

When I call PIL to sketch () the image, and then crop (), I get the artifact in the last row of pixels - they are either mostly black with spots of intense color, or something that seems not (that is, this line of pixels is in the original resolution and did not decrease with the rest)

This is not like a thumbnail () without a crop. This happens regardless of whether I cause a load () on the cropped image.

To get around this visually, I enlarged the image to 1 pixel and then cropped it to the same size. It seems to work. This is kind of a dirty hack. I am wondering if there is a correct fix.

+3
source share
1 answer

Yes, this happens to me. This was an exercise for me, because I never cropped or created thumbnails using PIL ...

not

thumbnails (size, filter = None)

Replaces the original image in place with a new image of the specified size (p. 2). The optional filter argument works the same as in the .resize () method. This operation retains the proportions (height: width). The resulting image will be as large as possible, while still adjusting to the specified size. For example, if the image im has a size of (400,150), its size after im.thumbnail ((40,40)) will be (40,15) .

So what happens

  • You are using a sketch that supports aspect
  • 40 x 40.
  • , , -

, , :

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 50 x 37, Desktop.croppedthumb.jpeg 30 x 30, 3 .

, , , .

+2

Source: https://habr.com/ru/post/1733923/


All Articles