统计多级文件夹中的tif文件的数量、分辨率、是否空白页、是否是彩色、是否歪斜的连贯代码
统计多级文件夹中的tif文件信息通常需要通过编程来完成,这里可以提供一个Python脚本示例,利用PIL(Pillow)库来处理TIFF文件的基本属性:
```python import os from PIL import Image from pytesseract import image_to_string
定义函数来检查图片属性
def check_tiff_info(file_path): try: img = Image.open(file_path)
# 获取分辨率(宽度和高度)
resolution = (img.width, img.height)
# 判断是否为空白页
is_blank = img.getbbox() is None or all(value == 0 for value in img.histogram())
# 检查是否彩色,彩色 TIFF 文件通常有3个通道(RGB)
is_color = len(img.getbands()) > 1
# 使用Tesseract尝试检测文本,判断是否歪斜(此部分仅对包含文字的图像有效)
text = image_to_string(img, config='--psm 6') # OCR设置
is_skewed = "倾斜" in text.lower() or "斜体" in text.lower()
return {
'count': 1, # 假设单个文件
'resolution': resolution,
'is_blank': is_blank,
'is_color': is_color,
'is_skewed': is_skewed
}
except Exception as e:
print(f"Error processing {file_path}: {e}")
return {'count': 0}
遍历文件夹及其子文件夹
def count_files_in_tree(root_folder): file_counts = {} for root, dirs, files in os.walk(root_folder): tif_files = [os.path.join(root, f) for f in files if f.endswith('.tif')] for file_path in tif_files: info = check_tiff_info(file_path) if info['count']: file_counts[file_path] = info
return file_counts
调用函数并打印结果
root_dir = '/path/to/your/directory' tif_data = count_files_in_tree(root_dir) for file_path, data in tif_data.items(): print(f"{file_path} - Count: {data['count']}, Resolution: {data['resolution']}, " f"Blank: {data['is_blank']}, Color: {data['is_color']}, Skewed: {data['is_skewed']}")
相关推荐
















