Python 3.x 使用枕头和python自动裁剪?
所以我有一个文件夹,里面有500多张需要裁剪的图片。我已经搜索并成功创建了这个剪切粘贴脚本。但是,由于某些原因,它无法保存新图像!?终端只是静止不动,没有错误,什么都没有Python 3.x 使用枕头和python自动裁剪?,python-3.x,python-imaging-library,Python 3.x,Python Imaging Library,所以我有一个文件夹,里面有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.fs
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
Im在特定文件夹中搜索,剪切后的图像应保存为文件名\u cropped.png
。但没必要,我有备份,如果有什么事情发生了
预期结果:
- 循环浏览文件夹
- 裁剪以
.png
- 并使用以前的文件名保存裁剪图像,但扩展名为
FILNAME\u cropped.png
- 完成
directory_in_str = "/Users/hora/Downloads/Etik/"
cropped.save("{}".format(directory_in_str+filename+"_cropped"),'png')
这方面有两个问题:
crapped.save(“{}.format(filename+”),“png”)
文件名仍然包含文件扩展名
xxx.png\u
我建议修改您的代码:
os.listdir(目录)中文件的:
filename=os.fsdecode(文件)
如果filename.endswith(“.png”):
image=os.path.join(目录\u在\u str中,文件名)
filename,file_extension=os.path.splitext(filename)#您实际上不需要为此编写任何Python,您可以非常简单地使用ImageMagick one liner(如果可以的话)来完成它?