简体   繁体   中英

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.

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.

Just posted an answer here: How to change the dpi or density when saving images using PIL , and I found this as well. I think the full answer posted on this will answer your question. In brief I would suggest to save a temp file and then delete it after you are done with it. I couldn't find any other workaround.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM