[英]Python/PIL Resize all images in all subfolder and save as new file name
I am going to resize image file which located in Desktop with set of subfolder.我将使用一组子文件夹调整位于桌面中的图像文件的大小。
I try following command and it works without rename save new.我尝试以下命令,它可以在不重命名保存新的情况下工作。
from PIL import Image
import os, sys
import glob
root_dir='./././Desktop/python/'
def resize():
for filename in glob.iglob(root_dir + '**/*.jpg', recursive=True):
print(filename)
im = Image.open(filename)
imResize = im.resize((450,600), Image.ANTIALIAS)
imResize.save(filename , 'JPEG', quality=90)
resize()
My question is how can I add the correct command to save the renewe resized image file with append 'f + _thumbnail' +.jpg
?我的问题是如何添加正确的命令来使用 append
'f + _thumbnail' +.jpg
保存重新调整大小的图像文件? Eg file1 > file1_thumbnail.jpg
例如
file1 > file1_thumbnail.jpg
I tried add followings to row print(filename)
but show error.我尝试将以下内容添加到行
print(filename)
但显示错误。
f, e = os.path.splitext(path+item)
size = im.size
ratio = float(final_size) / max(size)
new_image_size = tuple([int(x*ratio) for x in size])
im = im.resize(new_image_size, Image.ANTIALIAS)
new_im = Image.new("RGB", (final_size, final_size))
new_im.paste(im, ((final_size-new_image_size[0])//2, (final_size-new_image_size[1])//2))
new_im.save(f + 'resized.jpg', 'JPEG', quality=90)
May I know how can fix it?我可以知道如何解决吗?
Many Thanks非常感谢
You can use python slicing of strings for this - You can modify your code to -您可以为此使用 python 对字符串进行切片 - 您可以将代码修改为 -
from PIL import Image
import os, sys
import glob
root_dir='./././Desktop/python/'
def resize():
for filename in glob.iglob(root_dir + '**/*.jpg', recursive=True):
print(filename)
im = Image.open(filename)
imResize = im.resize((450,600), Image.ANTIALIAS)
filename = filename[:-4] + " _thumbnail" + filename[-4:]
imResize.save(filename , 'JPEG', quality=90)
resize()
The ".jpg" can be sliced at the end. “.jpg”可以在最后切片。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.