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.

Wand:

To open and manipulate images, a wand library is used.

Using wand, it is possible to perform following functions.

We can,

  1. Read/Write images that belong to different formats
  2. Do scaling and cropping
  3. Convert images from one format to another
  4. Add special effects to images
  5. Transform images
  6. Perform color enhancements.

Installation:

pip install wand

Image format:

It is the one that tells how the data of an image is stored. Below are different image file formats.

Image file formats:

  • JPEG(.jpeg)
  • TIFF(.tiff or .tif)
  • PNG(.png)
  • GIF(.gif)
  • EPS(.eps)
  • Bitmap(.bmp)

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.

Leave a Reply

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