Search code examples
pythonimage-processingpython-imaging-librarytesseract

Python - Pillow setting TIFF image resolution when saving


I have an OpenCV image I'm trying to save as a .tiff file:

img_to_save = Image.fromarray(array.astype("uint8"))
img_to_save.save(os.path.join(args[2], "1.tif"))

The file is saved but the resolution metadata is missing Warning: Invalid resolution 0 dpi. Using 70 instead. when I run tesseract on it.

I tried the following but I'm not sure how to convert resolution to dpi correctly or if it adds the metadata to the file:

img_to_save.save(os.path.join(args[2], "1.tif"), dpi=(array.shape[0], array.shape[1]))

How can I include the correct image resolution in the tiff?


Solution

  • Try using dpi=(72,72).

    Check in Terminal with:

    exiftool YourFile.tif
    

    or with ImageMagick:

    magick identify -verbose YourFile.tif
    

    or with:

    tiffinfo YourFile.tif