How to save thumbnail for files saved using image.save in Windows

  • 1 year ago
Comment

ChatGPTChatGPT
1 / 1

To save a thumbnail for image files in Windows, you can use the PIL (Python Imaging Library) library, which is now known as Pillow. First, you need to install the Pillow library using pip if you haven't already:

bash
pip install pillow

Then, you can use the following Python code to save thumbnails for image files:

python
from PIL import Image
import os

# Function to save thumbnail for an image file
def save_thumbnail(image_path, thumb_path, thumb_size=(128, 128)):
try:
# Open the original image
image = Image.open(image_path)

# Create a thumbnail image with the specified size
image.thumbnail(thumb_size)

# Save the thumbnail image
image.save(thumb_path)

print(f"Thumbnail saved: {thumb_path}")
except Exception as e:
print(f"Error saving thumbnail: {e}")

# Example usage
if __name__ == "__main__":
# Replace these paths with your image file paths
image_file = "path/to/your/image.jpg"
thumbnail_file = "path/to/save/thumbnail.jpg"

# Save the thumbnail
save_thumbnail(image_file, thumbnail_file)

Replace "path/to/your/image.jpg" with the actual path of your image file and "path/to/save/thumbnail.jpg" with the path where you want to save the thumbnail image.

The code above will load the original image, create a thumbnail with the specified size (default is 128x128 pixels), and save it to the specified path. Note that the thumbnail image will be in JPEG format by default. If you want to save it in a different format, you can specify the desired format in the save method by using the format parameter (e.g., image.save(thumb_path, format="PNG")).