当图像保存为tif时,如何指定dpiscipy.misc.imsave?

2024-10-03 17:16:33 发布

您现在位置:Python中文网/ 问答频道 /正文

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


Tags: 模块文件函数图像png格式scipyocr
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检查:

^{pr2}$

请将此文件归档到“任何其他方法”:-)

您可以使用exiftool设置分辨率,如下所示:

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

用ImageMagick进行检查:

^{pr2}$

我建议您使用os.system()来运行这个命令。在

一个Python wrapper存在,但我从未使用过它,也无法担保。在

相关问题 更多 >