Skip to main content
 首页 » 编程设计

python之使用 python 加载 CCITT T.3 压缩 tiff 的最佳方法是什么

2025年05月04日14xxx_UU

我正在尝试将 CCITT T.3 压缩的 tiff 加载到 python 中,并从中获取像素矩阵。它应该只是一个逻辑矩阵。

我尝试过使用 pylibtiff 和 PIL,但是当我加载它们时,它返回的矩阵是空的。我在很多地方读到这两个工具支持加载 CCITT 但不支持访问像素。

我愿意转换图像,只要我能从中获取逻辑矩阵并用 python 代码完成即可。疯狂的是,如果我在 paint 中打开我的一个图像,保存它而不改变它,然后尝试用 pylibtiff 加载它,它工作。 Paint 将其重新压缩为 LZW 压缩。

所以我想我真正的问题是:有没有办法将 CCITT 图像本地加载到矩阵或使用 python 将图像转换为 LZW?

谢谢,

泰勒米勒

请您参考如下方法:

似乎最好的方法是不完全使用 Python,而是依靠 netpbm :

import Image 
import ImageFile 
import subprocess 
 
tiff = 'test.tiff' 
im = Image.open(tiff) 
print 'size', im.size 
try: 
    print 'extrema', im.getextrema() 
except IOError as e: 
    print 'help!', e, '\n' 
 
print 'I Get by with a Little Help from my Friends' 
pbm_proc = subprocess.Popen(['tifftopnm', tiff], 
        stdout=subprocess.PIPE, 
        stderr=subprocess.PIPE) 
(pbm_data, pbm_error) = pbm_proc.communicate() 
ifp = ImageFile.Parser() 
ifp.feed(pbm_data) 
im = ifp.close() 
print 'conversion message', pbm_error, 
print 'extrema', im.getextrema() 
print 'size', im.size 
# houston: we have an image 
im.show() 

似乎可以解决这个问题:

$ python g3fax.py  
size (1728, 2156) 
extrema help! decoder group3 not available  
 
I Get by with a Little Help from my Friends 
conversion message tifftopnm: writing PBM file 
extrema (0, 255) 
size (1728, 2156)