Python Pillow – Converting Image File Formats


Python Pillow – Converting Image File Formats



”;


Converting image file formats with Python Pillow is a straightforward process. You can open an image in one format and save it in another, specifying the desired output format. This process typically involves the following steps −

  • Open the Source Image: Use the Image.open() function to open the source image.

  • Save the Image: Use the save() method to save the image in the desired format, specifying the new file format and destination.

Pillow can recognize and read over 30 different formats. When using the open() function, Pillow determines the format based on the image content, not just the file name. However, when saving images with the save() method, it usually looks at the file name to decide the format unless you specifically tell which format to use.

Pillow Supported File Formats

Pillow supports various image formats for both reading and writing. Some formats are fully supported, meaning you can both read from and write to images in those formats. These include popular formats like JPEG, PNG, BMP, BLP, DDS, EPS, GIF, ICNS, ICO, MSP, PCX, PNG, PPM, SGI, TGA, TIFF, WebP, and XBM.

Additionally, it provides read-only and write-only support for a range of other formats.

  • Read-Only Formats: CUR, DCX, FITS, FLI, FLC, FPX, GBR, GD, IMT, IPTC/NAA, MCIDAS, MIC, MPO, PCD, PIXAR, PSD, QOI, SUN, WAL, and WMF/EMF.
  • Write-Only Formats: PALM, PDF, and XV Thumbnails.

The library can also identify the format of images in formats such as BUFR, GRIB, HDF5, and MPEG. Let”s see the exmaples of converting image file formats with Python Pillow.

Example

This example converts the image from JPEG to PNG format.


from PIL import Image

# Open the source image in JPEG format
image = Image.open("Images/logo.jpg")

# Convert and save the image in PNG format
image.save("output_image_PNG_format.png")
print("Image saved successfully in PNG format...")

Output


Image saved successfully in PNG format...

Example

This example converts the image from BMP to GIF format.


from PIL import Image

# Open the source image in BMP format
image = Image.open("Images/lena.bmp")

# Convert and save the image in GIF format
image.save("output_image.gif")
print("Image saved successfully in GIF format...")

Output


Image saved successfully in GIF format...

Example

This example converts the image from GIF to TIFF format.


from PIL import Image

# Open the source image in GIF format
image = Image.open("Images/Book_Animation.gif")

# Convert and save the image in TIFF format
image.save("output_image.tiff")
print("Image saved successfully in TIFF format...")

Output


Image saved successfully in TIFF format...

Example

This example converts the image from .bpm file format to .jpg format.


from PIL import Image

# Open the source image in BMP format
image = Image.open("Images/lena.bmp")

# Convert and save the image in JPEG format
image.save(''lena_new.jpg'')
print("Image saved successfully in JPEG format...")

Output


Image saved successfully in JPEG format...

If you visit the folder where the output images are saved you can observe resultant images.









Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *