Convert an image into jpg format using Pillow in Python


In python pillow is one of the image processing libraries that is built on top of PIL (Python Image Library). It provides image processing functionalities to the python interpreter to work with image objects. And this library supports above 30 different image file formats.

Install Pillow with pip

By installing pillow using the pip command we can easily access the pillow functionalities.

python3 -m pip install --upgrade pip 
python3 -m pip install --upgrade Pillow

Just by running the above commands in the command prompt we will get the pillow module. In this article, we will discuss converting an image into .jpg format using Pillow in Python.

Converting .tif to .jpg format

The image extension JPG or JPEG stands for Joint Photographic Expert Group. JPG is a produced and accessible photographic image format that is generated through digital photography.

The .tif or .tiff stands for Tagged Image File Format; this is an uncompressed image file.

Algorithm

Following is the algorithm to convert .tif to .jpg format –

  • Import the Image module from PIL

  • Open the image to be converted using the open() method.

  • Before saving to JPG, discard alpha = transparency by Creating a copy of the image using the convert() method and Passing "RGB" as the parameter.

  • And Finally, save the image using the save() method with the .jpg extension.

Example

In this example, we will convert the .tiff or .tif image file format to .jpg format.

from PIL import Image
#Open image using Image module
im = Image.open("bali.tif")
rgb_im = im.convert('RGB')
rgb_im.save('bali_new.jpeg')
print("Image saved successfully ...")

Output

Image saved successfully ...

If you visit the folder where the images are saved you can observe both the original and new images as shown below –

We have successfully converted the input image format from .tif to .jpg using the pillow module.

Converting .png to .jpg format

The .png stands for Portable Network Graphics. This is a raster graphics file format that supports lossless data compression.

Example

In this example, we will convert the .png image file format to .jpg format.

from PIL import Image

#Open image using Image module
im = Image.open("hourglass.png")

rgb_im = im.convert('RGB')
rgb_im.save('hourglass_new.jpg')
print("Image saved successfully ...")

Output

Image saved successfully ...

If you visit the folder where the images are saved you can observe both the original and new images as shown below –

Converting .NEF to .jpg format:

The file format .NEF is abbreviated as Nikon Electronic Format. It is a RAW image file taken by Nikon cameras.

Example

In this example, we will convert the .NEF image file format to .jpg format.

from PIL import Image

#Open image using Image module
im = Image.open("Nikon1.NEF")

rgb_im = im.convert('RGB')
rgb_im.save('Nikon1_new.jpg')
print("Image saved successfully ...")

Output

Image saved successfully ...

If you visit the folder where the images are saved you can observe both the original and new images as shown below –

Converting .bpm to .jpg format

The .bpm is a bitmap file format, this format is used to store bitmap digital images.

Example

In this example, we will convert the .bpm image file format to .jpg format.

from PIL import Image

#Open image using Image module
im = Image.open("lena.bmp")

rgb_im = im.convert('RGB')
rgb_im.show()
rgb_im.save('lena_new.jpg')
print("Image saved successfully ...")

Output

Image saved successfully ...

If you visit the folder where the images are saved you can observe both the original and new images as shown below –


We successfully converted the image files from different formats to jpg format using the pillow module. In the output blocks we can found the both input image files and converted image files.

Updated on: 30-May-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements