sending an email through gmail using python

Convert any format of the image to jpg format using python.

In this tutorial, we are going to learn how to convert the image format into jpg and other formats. Before starting the coding, please make sure you have installed the pillow library into your system. If you have not installed the pillow module yet install it using the following command.

pip install pillow

Pillow.

Pillow or PIL(python image library) is one of the widely used image processing library. The pillow library supports opening, manipulating, and saving different formats of the image in python. Image is one of the important class of the PIL module, we can use  it by importing it in python script as follows:

from PIL import Image

Converting any image to jpg format

import os
import sys
from PIL import Image
#check whether the user passed the input image or not 
if len(sys.argv) > 1:
    file =sys.argv[1]
    if os.path.exists(file):
        filename=file.split(".")
        img = Image.open(file)
        target_name = filename[0] + ".jpg"
        rgb_image = img.convert('RGB')
        rgb_image.save(target_name)
        print("Converted image saved as " + target_name)
    else:
        print(file + " not found in given location")
else:
    print("please execute the script with input image as : python tqbimageconvert.py <file>")

save above script and give name tqbimageconvert.py then we will convert below png format image using above script.

Fig: Input image in PNG format

lets execute the script as follows.

Fig : Image conversion script execution
Fig: Output image in JPG format.

Similarly to change other formats of the image to JPG we will pass that image while executing the script. let assume we have a TIFF file (overview.tiff) and needs to convert into JPG format then we simply pass the file name as a parameter in the above example as below.

python tqbimageconvert.py overview.tiff

This will generates overview.jpg file in current directory.