Skip to content
Related Articles

Related Articles

Python – Image() function in Wand
  • Last Updated : 08 May, 2020
GeeksforGeeks - Summer Carnival Banner

In this specific article we will learn how to read our image through Python wand module. To read image in Wand we use Image() function. To manipulate an image first of all we need to read image in Python.

Parameters :

ParameterInput TypeDescription
imageImagemake exact copy of image
blobbytesopens an image of blob byte array
fileobjectopens an image of the file object
filenamebasestringopens image from filename
widthnumbers.Integralthe width of anew blank image or an image loaded from raw data
heightnumbers.Integralthe height of a new blank image or an image loaded from raw data
depthnumbers.Integralthe depth used when loading raw data.
backgroundwand.color.Coloran optional background color.
colorspacebasestringsets the stack’s default colorspace value before reading any images.
unitsbasestringpaired with resolution for defining an image’s pixel density.

Now we will write a code to print height and width of image.
Code :




# import required libraries
from __future__ import print_function
from wand.image import Image
  
    # read image using Image() function
    img = Image(filename ='koala.jpg')
  
    # print height of image
    print('height =', img.height)
  
    # print width of image
    print('width = ', img.width)

Output :

height = 300
width = 400

We can also read image from a url using urlopen function from urllib2 generic python library. Let’s see code to print image height and width read from a url.




# import required libraries
from __future__ import print_function
from urllib2 import urlopen
from wand.image import Image
  
try:
         
        # read image using Image() function
        img = Image(file = response)
  
        # print height of image
        print('Height =', img.height)
  
        # print width of image
        print('Width =', img.width)
finally:
    response.close()

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
Recommended Articles
Page :