Это видео недоступно.
Сожалеем об этом.

Python Pillow(PIL) Tutorial - how to resize an image using pillow in python

Поделиться
HTML-код
  • Опубликовано: 21 авг 2020
  • Resizing Images
    To resize an image, you call the resize() method on it, passing in a two-integer tuple argument representing the width and height of the resized image. The function doesn't modify the used image, it instead returns another Image with the new dimensions.
    image = Image.open('unsplash_01.jpg') new_image = image.resize((400, 400)) new_image.save('image_400.jpg') print(image.size) # Output: (1200, 776) print(new_image.size) # Output: (400, 400)
    The resize() method returns an image whose width and height exactly match the passed in value. This could be what you want, but at times you might find that the images returned by this function aren't ideal. This is mostly because the function doesn't account for the image's Aspect Ratio, so you might end up with an image that either looks stretched or squished.
    You can see this in the newly created image from the above code: image_400.jpg. It looks a bit squished horizontally.
    If you want to resize images and keep their aspect ratios, then you should instead use the thumbnail() function to resize them. This also takes a two-integer tuple argument representing the maximum width and maximum height of the thumbnail.
    image = Image.open('unsplash_01.jpg') image.thumbnail((400, 400)) image.save('image_thumbnail.jpg') print(image.size) # Output: (400, 258)
    The above will result in an image sized 400x258, having kept the aspect ratio of the original image. As you can see below, this results in a better looking image.
    Another significant difference between the resize() and thumbnail() functions is that the resize() function 'blows out' an image if given parameters that are larger than the original image, while the thumbnail() function doesn't. For example, given an image of size 400x200, a call to resize((1200, 600)) will create a larger sized image 1200x600, thus the image will have lost some definition and is likely to be blurry compared to the original. On the other hand, a call to thumbnail((1200, 600)) using the original image, will result in an image that keeps its size 400x200 since both the width and height are less than the specified maximum width and height.

Комментарии • 2