2017-10-09 91 views
1

我正在通过scipy.misc模块(imread,imresize,imsave函数)对屏幕截图(PNG格式)进行大小调整并将其写回TIF格式。 TIF格式图像将被输入到Tesseract-OCR中。但是,Tesseract抱怨TIF文件的元数据中指定的dpi是0。如何通过scipy.misc.imsave或任何其他方法保存图像时指定此项?如何通过scipy.misc.imsave将图像保存为tif时指定dpi?

回答

1

请把这个下“任何其他方法” :-)

你可以exiftool这样设置分辨率:

exiftool SomeImage.tif -xresolution=300 -yresolution=300 -resolutionunit=inches 

ImageMagick的一下:

identify -verbose SomeImage.tif 

Image: SomeImage.tif 
    Format: TIFF (Tagged Image File Format) 
    Mime type: image/tiff 
    Class: DirectClass 
    Geometry: 100x100+0+0 
    Resolution: 300x300 
    Print size: 0.333333x0.333333 
    ... 
    ... 

我建议你用shell来运行这个命令,并使用os.system()

A Python wrapper存在,但我从来没有使用过它,不能担保。

2

没有分析您的问题究竟从何而来,the approach of Mark(也许这足以让你,也许不是,我能想象有别的代码中的一些东西,可能是这个原因),可以通过使用Pillow(和我不要效仿在scipy的包装中没有看到这个选项)。

实际上,我们不是像他那样重写标签,而是在做原始任务时关注这些标签。在实践中,两种方法都应该没问题。

scipy的概率很高,已经为using Pillow under the hoodNote that Pillow (https://python-pillow.org/) is not a dependency of SciPy, but the image manipulation functions indicated in the list below are not available without it.;此列表包含imsave)。

from scipy.misc import ascent # test image 
import PIL.Image 

scipy_img = ascent().astype('uint8') 
arr2im = PIL.Image.fromarray(scipy_img) 

arr2im.save('test.tif', format='TIFF', 
     dpi=(100., 100.), # there still seems to be a bug when using int's here 
     compression='tiff_lzw',) 

与exiftool检查:

ExifTool Version Number   : 10.63 
File Name      : test.tif 
... 
Image Width      : 512 
Image Height     : 512 
Bits Per Sample     : 8 
Compression      : LZW 
... 
X Resolution     : 100 
Y Resolution     : 100 
... 
Resolution Unit     : inches 
Image Size      : 512x512 
Megapixels      : 0.262