You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Pillow提取TIFF图片属性并导出为Excel表格

TIFF图片属性提取与Excel导出实现方案

1. 优化图片加载方式

先将所有图片路径整理到列表中,通过循环批量加载,避免重复代码:

from PIL import Image
import pandas as pd

# 存放所有TIFF图片的路径
image_paths = [
    r"C:\Users\inpunindg\Downloads\Images\test_image.tif",
    r"C:\Users\inpunindg\Downloads\Images\02444349.tif",
    r"C:\Users\inpunindg\Downloads\Images\02445323.tif"
]

# 用于存储每张图片属性的空列表
image_properties = []

2. 提取目标属性

遍历每张图片,逐一提取像素尺寸、Alpha通道、剪贴路径信息:

for img_path in image_paths:
    # 使用with语句自动管理图片资源
    with Image.open(img_path) as img:
        # 提取像素尺寸(宽、高)
        img_width, img_height = img.size
        # 判断是否存在Alpha通道
        has_alpha = "A" in img.mode or (hasattr(img, 'has_transparency_data') and img.has_transparency_data)
        # 判断是否存在剪贴路径(TIFF的剪贴路径通常存储在info元数据中)
        has_clip_path = 'clip_path' in img.info or img.getchannel('ClippingPath') is not None
        
        # 将当前图片的属性存入列表
        image_properties.append({
            "图片路径": img_path,
            "像素宽度": img_width,
            "像素高度": img_height,
            "Alpha通道": "存在" if has_alpha else "不存在",
            "剪贴路径": "存在" if has_clip_path else "不存在"
        })

3. 生成表格并导出Excel

借助pandas将属性列表转换为表格,导出为Excel文件:

# 将属性列表转换为DataFrame表格格式
df = pd.DataFrame(image_properties)

# 设置Excel输出路径
output_path = r"C:\Users\inpunindg\Downloads\Images\图片属性统计.xlsx"
# 导出Excel,不包含索引列
df.to_excel(output_path, index=False)

print(f"属性表格已成功导出至:{output_path}")

前置依赖安装

执行以下命令安装所需库:

pip install pillow pandas openpyxl

补充说明

  • 若你的Pillow版本低于9.1.0,has_transparency_data属性不可用,可替换为判断img.convert('RGBA').getchannel('A').getextrema() != (255, 255)来检测透明区域
  • 部分特殊编码的TIFF剪贴路径可能需要额外解析,若上述判断失效,可尝试查看img.tag_v2或使用exifread库读取更详细的元数据

内容的提问来源于stack exchange,提问作者INDRANIL GHOSH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 13:26:13