如何修改Python PIL脚本仅处理PNG格式图片文件
如何修改代码仅处理PNG图片文件?
我用从Stack Overflow找到的代码裁剪文件夹里的图片并保存到另一个文件夹,在Windows 11的Jupyter Notebook中运行。复制文件到Linux根目录子文件夹后,系统自动生成了Zone.Identifier文件,导致脚本尝试处理这些非图片文件,手动删除后脚本正常运行。
我的原代码:
from PIL import Image import os.path SOURCE_DIRECTORY = "/home/andy/crop/nyc/" TARGET_DIRECTORY = "/home/andy/crop/output/nyc/" directory_list = os.listdir(SOURCE_DIRECTORY) def crop(): for source_file in directory_list: source_path = os.path.join(SOURCE_DIRECTORY, source_file) if os.path.isfile(source_path): raw_image = Image.open(source_path) file_name = os.path.basename(source_path) file_name, extension = os.path.splitext(file_name) image_cropped_top = raw_image.crop((344, 340, 474, 519)) image_cropped_top.save(TARGET_DIRECTORY + file_name+'.png', "PNG", quality=100) crop()
运行后报错:
--------------------------------------------------------------------------- UnidentifiedImageError Traceback (most recent call last) Input In [39], in <cell line: 20>() 17 image_cropped_top = raw_image.crop((344, 340, 474, 519)) 18 image_cropped_top.save(TARGET_DIRECTORY + file_name+'.png', "PNG", quality=100) ---> 20 crop() Input In [39], in crop() 11 source_path = os.path.join(SOURCE_DIRECTORY, source_file) 12 if os.path.isfile(source_path): ---> 13 raw_image = Image.open(source_path) 14 file_name = os.path.basename(source_path) 15 file_name, extension = os.path.splitext(file_name) File ~/miniconda3/lib/python3.9/site-packages/PIL/Image.py:3147, in open(fp, mode, formats) 3145 for message in accept_warnings: 3146 warnings.warn(message) ---> 3147 raise UnidentifiedImageError( 3148 "cannot identify image file %r" % (filename if filename else fp) 3149 ) UnidentifiedImageError: cannot identify image file '/home/andy/crop/nyc/DUDE.png:Zone.Identifier'
我倾向于通过代码仅选择PNG文件进行裁剪,请问该如何操作?
修改方案
只需在代码中添加文件扩展名判断,只处理.png(包括大小写.PNG)格式的文件,以下是两种可行的修改方式:
方式一:循环内添加判断
from PIL import Image import os.path SOURCE_DIRECTORY = "/home/andy/crop/nyc/" TARGET_DIRECTORY = "/home/andy/crop/output/nyc/" directory_list = os.listdir(SOURCE_DIRECTORY) def crop(): for source_file in directory_list: source_path = os.path.join(SOURCE_DIRECTORY, source_file) # 先判断是否为文件,再筛选PNG格式(不区分大小写) if os.path.isfile(source_path): _, extension = os.path.splitext(source_file) if extension.lower() == '.png': raw_image = Image.open(source_path) file_name = os.path.splitext(os.path.basename(source_path))[0] image_cropped_top = raw_image.crop((344, 340, 474, 519)) # 用os.path.join拼接路径,避免跨平台问题 image_cropped_top.save(os.path.join(TARGET_DIRECTORY, f"{file_name}.png"), "PNG", quality=100) crop()
方式二:提前筛选PNG文件
直接在获取文件列表时过滤出PNG文件,循环内无需再做判断,代码更简洁:
from PIL import Image import os.path SOURCE_DIRECTORY = "/home/andy/crop/nyc/" TARGET_DIRECTORY = "/home/andy/crop/output/nyc/" # 提前筛选出目录下的PNG文件(不区分大小写) directory_list = [ f for f in os.listdir(SOURCE_DIRECTORY) if os.path.isfile(os.path.join(SOURCE_DIRECTORY, f)) and f.lower().endswith('.png') ] def crop(): for source_file in directory_list: source_path = os.path.join(SOURCE_DIRECTORY, source_file) raw_image = Image.open(source_path) file_name = os.path.splitext(os.path.basename(source_path))[0] image_cropped_top = raw_image.crop((344, 340, 474, 519)) image_cropped_top.save(os.path.join(TARGET_DIRECTORY, f"{file_name}.png"), "PNG", quality=100) crop()
关键优化点:
- 加入扩展名小写转换判断,兼容
.PNG、.Png等大小写变体 - 用
os.path.join拼接文件路径,避免手动拼接字符串出现的路径分隔符问题,提升代码跨平台性
内容的提问来源于stack exchange,提问作者Andrew D. Clay
相关产品推荐
相关产品推荐

