📜  Python Wand – 概述

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

Python Wand – 概述

Wand是一个用于Python的 Imagick 库。它支持Python 2.6、2.7、3.3+ 和 PyPy 中的 Imagick API 的功能。该库不仅有助于处理图像,还为使用 NumPy 的机器学习代码提供有价值的功能。

安装:
通过点:

$ pip install Wand

由于 wand 是一个 Imagick API,所以我们需要 Imagick 依赖项。

Imagick的安装:

  • 对于 Ubuntu/Debian:
    $ sudo apt-get install libmagickwand-dev
  • 对于 Mac(通过 Brew 安装程序)
    $ brew install imagemagick

    MacPorts 的安装

    $ sudo port install imagemagick

    注意:如果没有使用 MacPorts 安装Python ,我们需要导出 MAGICK_HOME。

    $ export MAGICK_HOME=/opt/local
    示例 1:读取图像:
    输入图像: geeksforgeeks.png
    极客
    # Import library from the wand
    from wand.image import Image
      
    # Import the image
    with Image(filename ='geeksforgeeks.png') as pic:
      
        # Read the image to fetch actual dimensions
        print('Width of the image:', pic.width)
        print('Height of the image:', pic.height)
    

    输出:

    ('Width of the image:', 667L)
    ('Height of the image:', 184L)
    

    示例 2:模糊图像:

    # Import library from the wand 
    from wand.image import Image
      
    # Import the image
    with Image(filename ="geeksforgeeks.png") as pic:
      
        # Invoke blur function with radius 0 and sigma 3
        pic.blur(radius = 0, sigma = 3)
      
        # save the processed iamge
        pic.save(filename ="blur1.png")
    

    输出:
    blur-geeksforgeeks-pyhton

    示例 3:转换图像

    # Import library from the wand
    from wand.image import Image
      
    # Import the image
    with Image(filename ='geeksforgeeks.png') as image:
        # Clone the image in order to process
        with image.clone() as flip:
      
            # Invoke flip function
            flip.flip()
      
            # Save the image
            flip.save(filename ='flip-geeksforgeeks.jpg')
    

    输出:
    翻转极客forgeeks

    示例 4:绘图:

    # Import libraries from the wand  
    from wand.image import Image
    from wand.drawing import Drawing
    from wand.color import Color
      
    with Drawing() as draw:
        # Set Stroke color the circle to black
        draw.stroke_color = Color('black')
      
        # Set Width of the circlw to 2 
        draw.stroke_width = 2
      
        # Set the fill color to 'White (# FFFFFF)'
        draw.fill_color = Color('white')
      
        # Invoke Circle function with center 
        # at 200, 200 and radius 100
        draw.circle((200, 200), # Center point
                    (100, 100)) # Perimeter point
        with Image(width = 400, height = 400, 
                  background = Color('lightgreen')) as pic:
            draw(pic)
            pic.save(filename ='circle1.jpg')
    

    输出:
    圆圈
    参考:

    • http://docs.wand-py.org/en/0.5.9/index.html