📜  Python – Wand 中的 Image()函数

📅  最后修改于: 2022-05-13 01:54:54.928000             🧑  作者: Mango

Python – Wand 中的 Image()函数

在这篇具体的文章中,我们将学习如何通过Python wand 模块读取我们的图像。要在 Wand 中读取图像,我们使用 Image()函数。首先要操作图像,我们需要在Python中读取图像。

现在我们将编写一个代码来打印图像的高度和宽度。
代码 :

# 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()