[英]storing a PILLOW image in same name after editing
I want to crop a set of Pillow images and save it with the same name in the same folder, from where it is opened.我想裁剪一组枕头图像并将其以相同的名称保存在打开它的同一文件夹中。 It is clustered and stored into 4 groups.它被聚类并存储为4组。
I wrote the code as below.我写的代码如下。
for c in range(4):
for image_file in glob.glob(f"plot_images/{c}/*.jpg"):
im=Image.open(image_file)
im = im.convert("RGB")
im = im.crop(offset)
im.save(im.filename)
It gives me the error它给了我错误
AttributeError Traceback (most recent call last)
<ipython-input-24-9f3d3a38e4e4> in <module>
15 im = im.crop(offset)
16 #im.show()
---> 17 im.save(im.filename)
18 #print(c,end='\r')
19
/srv/conda/envs/notebook/lib/python3.8/site-packages/PIL/Image.py in __getattr__(self, name)
539 )
540 return self._category
--> 541 raise AttributeError(name)
542
543 @property
AttributeError: filename
I don't understand why the error comes.我不明白为什么会出现错误。 please help.请帮忙。
If you check type(im)
in different moments then you should see PIL.JpegImagePlugin.JpegImageFile
after loading but PIL.Image.Image
after converting which don't have filename
.如果您在不同的时刻检查type(im)
,那么您应该在加载后看到PIL.JpegImagePlugin.JpegImageFile
但在转换后看到PIL.Image.Image
没有filename
。 Use image_file
instead of im.filename
使用image_file
而不是im.filename
im.save(image_file)
im = Image.open(image_file)
print(type(im)) # <class 'PIL.JpegImagePlugin.JpegImageFile'>
im = im.convert("RGB")
print(type(im)) # <class 'PIL.Image.Image'>
im = im.crop(offset)
print(type(im)) # <class 'PIL.Image.Image'>
im.save(image_file)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.