Convert Image Format using wand in Python
After this tutorial, you will able to convert image format using the wand in Python. At first, you have to know about the wand library.
To open and manipulate images, a wand library is used.
Using wand, it is possible to perform following functions.
- Read/Write images that belong to different formats
- Do scaling and cropping
- Convert images from one format to another
- Add special effects to images
- Transform images
- Perform color enhancements.
pip install wand
It is the one that tells how the data of an image is stored. Below are different image file formats.
Image file formats:
- TIFF(.tiff or .tif)
Program 1: To convert from PNG to JPEG
- First, we have imported Image from the wand.image module.
- Then we read the image using Image() function.
- Syntax: Image(filename)
- After that, we have changed the format of the image using the image.format property.
- Syntax: image.format=’final_format’
- Finally, we save the file with a new format using the image.save() function.
- Syntax: image.save(filename)
Now, have a look at the program.
from wand.image import Image with Image(filename ='SampleImage.jpg') as Sampleimg: Sampleimg.format = 'png' Sampleimg.save(filename ='SampleImage.png')
After executing the above code, SampleImage’s format(.jpg) is changed to a new format(.png).
Program 2: To convert from JPEG to TIFF
Let’s take a look at the program.
from wand.image import Image with Image(filename ='SampleImage.png') as Sampleimg: Sampleimg.format = 'tiff' Sampleimg.save(filename ='SampleImage.tiff')
If we run the above code, our image’s format will change from PNG(.png) to TIFF(.tiff).
So, I hope you all enjoyed this tutorial and learned as well.