body {
font-family: Arial, sans-serif;
margin: 30px;
}
h1 {
text-align: center;
}
.thumbnail {
max-width: 100px;
max-height: 100px;
}
#03 Python Image Converter – Creating Thumbnails and Showing Image Details
When working with images in Python, it’s often necessary to resize them and display their details. In this tutorial, we’ll explore how to use Python to create thumbnail images and showcase their details.
Creating Thumbnails
To create a thumbnail of an image in Python, we can use the PIL
(Python Imaging Library) module. First, make sure you have the PIL module installed by running the following command:
pip install pillow
Once you have PIL installed, you can use the following code to create a thumbnail of an image:
from PIL import Image
img = Image.open('input_image.jpg')
img.thumbnail((100, 100))
img.save('thumbnail_image.jpg')
Showing Image Details
To display the details of an image, such as its dimensions and file format, we can use the following code:
from PIL import Image
img = Image.open('input_image.jpg')
width, height = img.size
file_format = img.format
print(f'Image dimensions: {width}x{height}, Format: {file_format}')
Displaying Thumbnails and Image Details in HTML
Finally, we can display the thumbnail image and its details in an HTML file using the following code:
<img src="thumbnail_image.jpg" class="thumbnail" />
<p>Image dimensions: 100x100, Format: JPEG</p>
By combining these techniques, you can easily create thumbnails of images and showcase their details in your Python applications.