0 votes
in Python Imaging Library by
How would you use PIL to create a thumbnail of an image?

1 Answer

0 votes
by

To create a thumbnail using Python Imaging Library (PIL), you first need to import the library with “from PIL import Image”. Then, open the image file by calling “Image.open(‘path_to_image’)”, replacing ‘path_to_image’ with your image’s path. This returns an Image object. To convert this into a thumbnail, use the “.thumbnail()” method on the Image object and pass in the desired size as a tuple of width and height like “(100, 100)”. The aspect ratio is preserved during this process. If the image is larger than the given size, it’s resized; smaller images are left unchanged. Save the thumbnail using “.save(‘path_to_thumbnail’)”, replacing ‘path_to_thumbnail’ with your preferred save location.

...