2017-10-09 89 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