【发布时间】:2020-03-28 02:16:07
【问题描述】:
所以我有一个文件夹,其中包含 500 多张需要裁剪的图像。我已经搜索并设法创建了这个剪切和粘贴脚本。但是,由于某种原因,它没有保存新图像!?终端是静止的,没有错误,什么都没有。
from PIL import Image # import the Python Image processing Library
import os # To read the folder
directory_in_str = "/Users/hora/Downloads/Etik"
directory = os.fsencode(directory_in_str)
for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.endswith(".png"):
image = os.path.join(directory_in_str, filename)
imageObject = Image.open(image) # Create an Image object from an Image
cropped = imageObject.crop((1025,85,2340,2040)) # Crop the iceberg portion (top left x, top left y, bottom right x, bottom right y)
cropped.save("{}".format(filename+"_cropped"), 'png') # Save the cropped portion
continue
else:
continue
我在特定文件夹中搜索,裁剪后的图像应使用filename_cropped.png 保存。但没必要,如果有什么事情发生了,我有备份。
预期结果:
- 循环浏览文件夹
- 裁剪所有以
.png结尾的图像 - 并使用以前的文件名但带有扩展名保存裁剪图像
FILNAME_cropped.png - 完成
【问题讨论】:
-
您实际上不需要为此编写任何 Python,如果可以的话,您可以使用 ImageMagick 单线非常简单地做到这一点?
标签: python-3.x python-imaging-library