Read Modifiers in Wand Python

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:

  1. Read frames/pages
  2. Read Resize
  3. 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import Image from wand.image module
from wand.image import Image
  
    # Read first page of pdf using Image() function
    with Image(filename ='document.pdf[0]') as first_page:
  
        # convert pdf page to image file
        first_page.convert("png")
  
        # save final image
        first_page.save(filename = "first_page_image.png")

chevron_right


Output:

Example 2:

Input:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import Image from wand.image module
from wand.image import Image
   
# Read first six frames of gif using Image() function
with Image(filename='sample.gif[0-5]') as f:
    #save final image
    f.save(filename = "final.gif")

chevron_right


Output:

Example 3: In this we will perform Read Resize.

Input:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Import Image from wand.image module
from wand.image import Image
  
# Read first six frames of gif using Image() function
with Image(filename ='initial.jpg[400x300]') as resized_image:
    # convert  jpg image fie to png image file
    resized_image.convert("png")
    # save final image
    resized_image.save(filename = 'final.png')

chevron_right


Output :

Example 4: In this we will perform Read Crop and then save it in another format.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import Image from wand.image module
from wand.image import Image
  
# Read first six frames of gif using Image() function
with Image(filename ='sample.gif[100x100 + 50 + 75]') as cropped_image:
    # convert gif file to png file
    cropped_image.convert("png")
    # save final image
    cropped_image.save(filename = 'final.png')

chevron_right


Input:

Output:

Read-Modifiers-in-Wand-Python

Read-Modifiers-in-Wand-Python-2

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Hi there I am a student of CGC COE, Landran in CSE department and have a great interest in programming

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.