Read Modifiers in Wand Python
Last Updated :
12 Nov, 2021
Read modifier refers to immediately modify input image or file format after reading or to make the image perfect to manipulate just after reading the image. This can be done by using the filename parameter of Image() function.
For instance, let us say we want to read an image of (100 x 100) dimensions/aspect ratio but the original image is of (200 X 200) dimensions/aspect ratio. So we can use Read modifier in order to convert image to (100 X 100) dimensions/aspect ratio just after reading it.
We can use read modifiers to display specific frames from the .gif file. Also to select specific pages from pdf files
Types of Read Modifiers:
- Read frames/pages
- Read Resize
- Read Crop
Syntax: with Image(filename=’filename.format[read_modifier]’) as modified_Read:
Example 1: Let us say we need to read only the first page from pdf and convert it in .png format.
from wand.image import Image
with Image(filename = 'document.pdf[0]' ) as first_page:
first_page.convert( "png" )
first_page.save(filename = "first_page_image.png" )
|
Output:
Example 2:
Input:
from wand.image import Image
with Image(filename = 'sample.gif[0-5]' ) as f:
f.save(filename = "final.gif" )
|
Output:
Example 3: In this we will perform Read Resize.
Input:
from wand.image import Image
with Image(filename = 'initial.jpg[400x300]' ) as resized_image:
resized_image.convert( "png" )
resized_image.save(filename = 'final.png' )
|
Output :
Example 4: In this we will perform Read Crop and then save it in another format.
from wand.image import Image
with Image(filename = 'sample.gif[100x100 + 50 + 75]' ) as cropped_image:
cropped_image.convert( "png" )
cropped_image.save(filename = 'final.png' )
|
Input:
Output:
Share your thoughts in the comments
Please Login to comment...