Can I access ImageMagick API with Python?

前端 未结 3 2092
你的背包
你的背包 2020-11-29 23:17

I need to use ImageMagick as PIL does not have the amount of image functionality available that I am looking for. However, I am wanting to use Python.

The python bin

3条回答
  •  醉酒成梦
    2020-11-29 23:32

    I found no good Python binding for ImageMagick, so in order to use ImageMagick in Python program I had to use subprocess module to redirect input/output.

    For example, let's assume we need to convert PDF file into TIF:

    path = "/path/to/some.pdf"
    cmd = ["convert", "-monochrome", "-compress", "lzw", path, "tif:-"]
    fconvert = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = fconvert.communicate()
    assert fconvert.returncode == 0, stderr
    
    # now stdout is TIF image. let's load it with OpenCV
    filebytes = numpy.asarray(bytearray(stdout), dtype=numpy.uint8)
    image = cv2.imdecode(filebytes, cv2.IMREAD_GRAYSCALE)
    

    Here I used tif:- to tell ImageMagick's command-line utility that I want to get TIF image as stdout stream. In the similar way you may tell it to use stdin stream as input by specifying - as input filename.

提交回复
热议问题