CodeGym /Courses /Python SELF EN /Managing Formats and Conversion

Managing Formats and Conversion

Python SELF EN
Level 46 , Lesson 2
Available

1. Identifying Image Format

Supported Image Formats

Pillow supports working with a bunch of image formats, including:

  • JPEG (JPG): One of the most popular formats for photos. Compresses images with quality loss but significantly reduces file size. Doesn’t support transparency.
  • PNG: Lossless format that supports transparency (alpha channel). Perfect for logos, icons, and images where sharpness matters.
  • BMP: High-quality format without compression, but with large file size. Supports transparency.
  • TIFF: Popular for storing lossless images. Often used in printing.
  • WEBP: Modern format with high compression and transparency support. Great for web images as it offers small file sizes with good quality.

Loading an Image for Conversion

Before converting an image, you need to load it using the Image.open() method.

Python

from PIL import Image

# Open an image
image = Image.open("example.jpg")
print(image.format)  # Check the original format of the image

2. Converting an Image to Another Format

The save() method lets you save images in different formats, simply by changing the file extension in the filename or explicitly specifying the format.

Converting JPEG to PNG

To convert an image from JPEG to PNG, just specify a new extension when saving the file:

Python

# Save the image in PNG format
image.save("example_converted.png")

This code transforms an image from JPEG to PNG format, saving it with the specified name. PNG is great for lossless images and also supports transparent areas.

Converting PNG to JPEG

Converting an image from PNG to JPEG is also possible, but keep in mind that JPEG doesn’t support transparency. Transparent areas will be filled with white.

Python

# Convert PNG to JPEG (transparency will be replaced with a white background)
image = image.convert("RGB")  # Switch to RGB mode necessary for JPEG
image.save("example_converted.jpg", "JPEG")

The convert("RGB") method is used to convert the image to RGB mode, as JPEG doesn’t support transparent areas. This is necessary to save the file in JPEG format.

3. Managing Image Quality

To optimize images, Pillow has the quality and optimize parameters, which let you control the quality and file size. These parameters are especially useful for JPEG and PNG.

Optimizing and Managing JPEG Quality

When saving an image in JPEG format, the quality parameter adjusts the compression level. Higher quality values result in larger file sizes and better image quality. The quality value ranges from 1 to 95 (default 75).

Python

# Save the JPEG image with reduced quality
image.save("example_compressed.jpg", "JPEG", quality=85)

This code saves the image with quality 85, which usually maintains good quality while reducing the file size. If less quality is needed, e.g., for creating thumbnails, you can specify quality = 50.

Optimizing PNG

For PNG, the optimize = True parameter performs lossless compression by removing redundant data. This is especially useful for publishing images on the web.

Python

# Save an optimized PNG
image.save("example_optimized.png", "PNG", optimize=True)

Optimizing PNG helps reduce file size without quality loss, which is especially important for logos and icons where every kilobyte matters.

Comparing JPEG Quality at Different Compression Levels

Let’s see how changing the quality parameter impacts file size and image quality.

Python

# Save the JPEG image with different quality levels
image.save("quality_95.jpg", "JPEG", quality=95)
image.save("quality_75.jpg", "JPEG", quality=75)
image.save("quality_50.jpg", "JPEG", quality=50)

In this example, we save the same image with three quality levels: 95, 75, and 50. The 95-quality image will be almost indistinguishable from the original but take up more space, while quality 50 will result in significant compression and noticeable detail loss.

4. Using the WebP Format

The WebP format is a modern format that supports both lossy and lossless compression as well as transparency. It’s widely used on the web because of its high compression rates.

Converting an Image to WebP Format

Python

# Convert an image to WebP
image.save("example.webp", "WEBP", quality=80)

In this example, we save the image in WebP format with quality 80. WebP achieves significantly smaller file sizes compared to JPEG or PNG while maintaining similar quality.

Full Conversion Example with Optimization

Now let’s bring all the methods together and create a script to convert an image into multiple formats with optimization.

Python

from PIL import Image

# Open the image
image = Image.open("example.jpg")

# Convert to PNG with optimization
image.save("converted_optimized.png", "PNG", optimize=True)

# Convert to JPEG with reduced quality
image_rgb = image.convert("RGB")  # Switch to RGB mode for JPEG
image_rgb.save("converted_compressed.jpg", "JPEG", quality=85)

# Convert to WebP with quality optimization
image_rgb.save("converted_optimized.webp", "WEBP", quality=80)

In this example:

  • We convert the image to PNG with optimization, reducing file size without quality loss.
  • Save the image in JPEG format with quality 85, ensuring good quality and small file size.
  • Convert the image to WebP format, which is perfect for web publishing, maintaining high quality with a small file size.
1
Task
Python SELF EN, level 46, lesson 2
Locked
Image Format Determination
Image Format Determination
2
Task
Python SELF EN, level 46, lesson 2
Locked
Convert and Save an Image
Convert and Save an Image
3
Task
Python SELF EN, level 46, lesson 2
Locked
JPEG Quality Control when Saving
JPEG Quality Control when Saving
4
Task
Python SELF EN, level 46, lesson 2
Locked
Batch Image Conversion
Batch Image Conversion
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION