TypeError: No to_python (by-value) converter found for C++ type: MagickCore::ResolutionType

57 Views Asked by At

The following code:

import subprocess
import PythonMagick
subprocess.run(["convert","rose:","test.pnm"])
print(PythonMagick.Image("test.pnm").resolutionUnits())

produces the error: TypeError: No to_python (by-value) converter found for C++ type: MagickCore::ResolutionType

How can I get the resolution units of an image using PythonMagick?

2

There are 2 best solutions below

0
DobbyTheElf On

This is a foul hack and doesn't really answer my question:

import subprocess
import PythonMagick
subprocess.run(["convert","rose:","test.pnm"])
# print(PythonMagick.Image("test.pnm").resolutionUnits())
spo = subprocess.run(["identify","-verbose","test.pnm"], capture_output=True, text=True)
for line in spo.stdout.splitlines():
    values = line.split(":")
    if len(values) == 2:
        key, value = values
        if key.strip() == "Units":
            print(value.strip())

I'd love a better answer.

4
Mark Setchell On

You can more succinctly extract x-resolution (dpi) like this:

magick image.png -format "%x" info:

If you want x-resolution, y-resolution and their units, use:

magick image.png -format "%x %y %U" info:

exiftool may be a lighter-weight dependency, see example here.