Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change the DPI of an image using the PIL without saving?

I am trying to process numerous images through tesseract. Nonetheless, I have just discovered that by increasing the dpi I obtained better results. I have tried looking throughout StackOverflow on how I could possibly increase the dpi of an image, but all of the answers that I have found involved in changing the dpi while saving the file so something similar to this:

img.save('test.png', dpi=(300.0, 300.0))

Therefore I was wondering If it is possible to do this without necessarily saving the file.

I have trying doing the following:

image = Image.open('test.png', dpi=(300.0, 300.0)) 

but sadly it doesn't work

Thank you very much for your help in advance.

like image 319
Nazim Kerimbekov Avatar asked Feb 11 '18 16:02

Nazim Kerimbekov


1 Answers

The PNG format stores DPI information in a metadata chunk in the file, it's called a "pHYs" chunk. So, you're really looking for a tool which can edit or add these kinds of chunks in/to PNG files without reading the image data itself.

A library which can do that is libpng and there exist Python bindings for it.

You might also want to check out this question which addresses the same problem. Apparently there are tools like IrfanView and ImageMagick which can change PNG files, like putting a pHYs metadata block in it.

like image 112
Michiel Overtoom Avatar answered Sep 29 '22 15:09

Michiel Overtoom