使用wand库将多个jpeg图像转换为一个压缩的tiff图像

时间:2018-03-20 09:46:59

标签: python python-2.7 imagemagick windows-10 wand

我正在开发 Windows10 x64 Python 2.7.14

我必须将扫描的JPEG图像收集到一个压缩的TIFF文件中。

我使用Pillow尝试libtiff图片库,但无论我设置哪种压缩算法,它都会破坏生成的tiff文件。如果没有压缩算法,它会产生100 MB的未损坏的tiff文件。

然后我找到了Image Magick命令行工具,它在压缩方面效果很好。它将100 MB未压缩的tiff文件压缩为7 MB的jpeg压缩tiff文件。

magick.exe -compress jpeg *.jpg abc.tif

很难获得在我们的服务器上运行第三方exe的权限。所以我决定在我的Python应用程序中将它用作库,并找到Wand库。现在问题是Wand库不压缩结果tiff文件。

如何使用Wand库生成压缩的tiff文件?

以下是我的最终结果:

import os
from wand.image import Image

imagepaths = []
folder = "d:/scan/"
for file in os.listdir(folder):
    if file.endswith(".jpg") or file.endswith(".jpeg"):
        imagepaths.append(os.path.join(folder, file))

with Image() as img:
    img.sequence.extend( [ Image(filename=f, resolution=300) for f in  imagepaths] )
    img.compression = "jpeg"
    img.compression_quality = 50 # these two lines have no effect, no compression is done
    img.save(filename="d:/scan/wand_converted.tif")
    # this generates a tiff file with about 100 MBs of size
    # When I convert jpegs to tiff with magick at command line it produces a 7 MBs file

0 个答案:

没有答案