Python – Wand 中的 Image()函数
在这篇具体的文章中,我们将学习如何通过Python wand 模块读取我们的图像。要在 Wand 中读取图像,我们使用 Image()函数。首先要操作图像,我们需要在Python中读取图像。
Parameters :
Parameter | Input Type | Description |
---|---|---|
image | Image | make exact copy of image |
blob | bytes | opens an image of blob byte array |
file | object | opens an image of the file object |
filename | basestring | opens image from filename |
width | numbers.Integral | the width of anew blank image or an image loaded from raw data |
height | numbers.Integral | the height of a new blank image or an image loaded from raw data |
depth | numbers.Integral | the depth used when loading raw data. |
background | wand.color.Color | an optional background color. |
colorspace | basestring | sets the stack’s default colorspace value before reading any images. |
units | basestring | paired with resolution for defining an image’s pixel density. |
现在我们将编写一个代码来打印图像的高度和宽度。
代码 :
# 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)
输出 :
height = 300
width = 400
我们还可以使用 urllib2 通用Python库中的urlopen
函数从 url 读取图像。让我们看看打印从 url 读取的图像高度和宽度的代码。
# import required libraries
from __future__ import print_function
from urllib2 import urlopen
from wand.image import Image
response = urlopen('https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-6.png')
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()