19

How to get tiff image DPI using pillow? Cant see in documentation.

from PIL import Image
im = Image.open('test.tif')
print("im dpi?")
Monica Heddneck
  • 2,973
  • 10
  • 55
  • 89
user3654650
  • 5,283
  • 10
  • 27
  • 28

1 Answers1

34

Image resolution in DPI should be available in info dictionary (more about info for tiff images can be found here):

print(im.info['dpi'])

Though, not all images provide this information.

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • 1
    I get a tuple such as `(72, 72)`, how do I interpret this tuple? – aspiring1 May 26 '20 at 06:16
  • @aspiring1 These are [dots per inch](https://en.wikipedia.org/wiki/Dots_per_inch) values. Basically number of pixels in 1 inch, or how many pixels there are in 1 inch. – Marcin May 26 '20 at 06:19
  • Okay as I understand now, the two values indicate dpi values across each dimension i.e height and width, i.e my image has 72 dots in 1 inch across height dimension and width dimension – aspiring1 May 26 '20 at 06:31
  • 1
    @aspiring1 yes. So one pixels has size of 0.013889 x 0.013889 inch (1/72). – Marcin May 26 '20 at 06:33
  • 1
    Also is changing the **dpi** using (using PIL here) `im.save(imagename, dpi=(600, 600))` helpful for OCR purposes? I read that a minimum of _300_ ppi (what is called high resolution) is required for OCR engines for good accuracy, so is changing it this way from (72, 72) to (600, 600) meaningful, and also what does it mean when I do perform this operation, do my pixels get smaller in size from 1/72 to 1/300 by breaking up the large pixels ( also is it helpful)? – aspiring1 May 26 '20 at 06:38
  • @aspiring1 In depends on images. You can save any value as DPI in your image, it does not make them automatically have better resolution. DPI is only meaningful if the image acquisition technique actually uses that resolution to acquire the digital version of it. E.g. a scanner which phtiscally will scan a photograph with 600 DPI. In fact 72 is often used a placeholder for lack of any meaningful value. – Marcin May 26 '20 at 06:44
  • I take it that my `info` dictionary might be empty and I maybe getting returned that 72 which as you have said and I quote _is a placeholder for lack of any meaningful value_ – aspiring1 May 26 '20 at 08:09
  • @aspiring1 You have to check how the images you analyze were required. Without this, you can't be sure that the DPI values have real physical meaning. – Marcin May 26 '20 at 08:11
  • As I understand there can be two ways one of which is having a scanned image from a scanner the other is a digital image from a camera, also couldn't understand **how the mode of acquisition affects dpi meaningfulness** – aspiring1 May 26 '20 at 08:13
  • @aspiring1 Have to read about it. Sorry, I can't explain much in comments section, nor I know all the details you mean ask. – Marcin May 26 '20 at 08:16
  • I am interested in improving OCR quality and am using **tesseract** for the same, I read [here](https://stackoverflow.com/a/10034214/8030107) about fixing the dpi where it says 300 dpi is the minimum. So, I was trying to increase the dpi to atleast 300 – aspiring1 May 26 '20 at 08:18
  • @aspiring1 You can only do this by interpolation (upscaling/resampling) your images. – Marcin May 26 '20 at 08:21