如何在python或java中精确计算此文件的页面数?
**所附tif文件的原始扩展名为img** 链接的Img文件是多页图像。 在window picture viewer中,此文件的总页数为178。 但是Imagick(图像处理程序)、JAI(java模块)和pilpython包的总页面是350如何在python或java中精确计算此文件的页面数?,java,python,Java,Python,**所附tif文件的原始扩展名为img** 链接的Img文件是多页图像。 在window picture viewer中,此文件的总页数为178。 但是Imagick(图像处理程序)、JAI(java模块)和pilpython包的总页面是350 ## my python code ## import os from PIL import Image count = 0 i = 0 tiffs_path = "l:/fun" for filename in os.listdir("l:/fun")
## my python code ##
import os
from PIL import Image
count = 0
i = 0
tiffs_path = "l:/fun"
for filename in os.listdir("l:/fun"):
if filename.endswith(".tif"):
img = Image.open(filename)
while True:
try:
img.seek(count)
except EOFError:
break
count += 1
print(filename+"::"+str(count))
我要像上面一样精确地计算文件的页数。
(本文件总页数为178页。)
如何在python或java中计算此文件的页面数?
(链接的img文件-03512378.tif-中似乎有一个my unknown属性。)这个问题有点棘手。根据, TIFF文件中可能有多个图像文件目录(IFD)。每个IFD定义一个子文件。子文件的一种用途是描述相关图像,例如传真文档的页面 本质上,这意味着您的帧可能只包含(元)信息,而不包含任何实际图像。我试图研究特定TIFF文件的元数据,很快意识到图像描述中存在重复,暗示帧属于同一图像。我在下面写的代码打印
PIL报告“350”帧
唯一帧数:177
计数的帧数:350
我的操作系统ImageVewer报告了178幅图像,虽然不是很准确,但我还是非常接近;和你一样。不过,这可能会对您有所帮助:例如,您可以通过打印dictmeta
继续调查元数据。此外,请注意,这种方法假设所有TIFF文件的元数据结构都相似
import PIL
from PIL import Image
from PIL.TiffTags import TAGS
im = Image.open("03512378.tif")
print("PIL reports '"+ str(im.n_frames) +"' frames")
count = 1
img_numbers = []
while True:
try:
im.seek(im.tell()+1)
meta = {TAGS[key] : im.tag[key] for key in im.tag.keys()}
img_numbers.append((meta["ImageDescription"][0]))
# Note: elements in the meta dict are tuples
except EOFError:
break
count += 1
s = set(img_numbers) # remove duplicates
print("Number of unique frames : " + str(len(s)))
print("Number of counted frames: " + str(count))
检查:总页面是350使用代码的。这与我的python代码类似。我想得到结果值(178)。^;