计算Python中.TIF文件中的总页数 [英] Count total number of pages in .TIF file in Python

查看:445
本文介绍了计算Python中.TIF文件中的总页数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让Python准确读取.TIF中的页面数,并且我从昨天得到的一些帮助中修改了一些代码.我已经让Python读取.TIF文件并输出页面,但是它只读取可以找到的第一个.TIF文件.我需要它在相同位置浏览所有.TIF文件.

I'm trying to get Python to read exactly how many pages are in a .TIF and I've modified some code from some help I got yesterday. I've gotten Python to read the .TIF file, and output the pages, however it only reads the first .TIF file it can find. I need it to go through all the .TIF files in the same location.

我想知道如何做到这一点,以便一旦完成计数,它将继续到下一个文件,直到完全完成.

I was wondering how can I make it so that once it is done counting, it will continue to the next file until it is completely done.

这是我到目前为止所拥有的

Here is what I have so far

import os
from PIL import Image

count = 0
i = 0
tiffs_path = "c:\\tiftest"

for filename in os.listdir("c:\\tiftest"):
    if filename.endswith(".TIF"):
        img = Image.open(filename)
        while True:
            try:   
                img.seek(count)
                print(filename)
                print(count)
            except EOFError:
                break       
            count += 1          

print(count)

推荐答案

您可以使用Image.n_frames查找TIFF中的帧数.它是在枕头2.9.0中添加的.

You can use Image.n_frames to find the number of frames in a TIFF. It was added in Pillow 2.9.0.

例如,使用枕头4.2.1:

For example, with Pillow 4.2.1:

Python 2.7.13 (default, Dec 18 2016, 07:03:39)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
>>> img = Image.open("multipage.tiff")
>>> img.n_frames
3
>>>

所以,像这样:

import os
from PIL import Image

count = 0
i = 0
tiffs_path = "c:\\tiftest"

for filename in os.listdir("c:\\tiftest"):
    if filename.endswith(".TIF"):
        img = Image.open(filename)
        print(filename)
        print(img.n_frames)

这篇关于计算Python中.TIF文件中的总页数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆