![](/img/trans.png)
[英]How to read all images from subfolder, resize them and save them with their original name in Python?
[英]Read all images from subfolder, resize them and save them with their original name in Python?
我想從不同的子文件夾中讀取所有圖像,調整它們的大小並使用它們的原始名稱保存它們。 但是,我收到如下所示的錯誤。 我該如何解決這個錯誤?
示例代碼:
from PIL import Image
import os
size = (112, 112)
def resize_img(path,dest_path):
name_list = []
label_list = []
resized_list = []
if not os.path.exists(dest_path):
os.makedirs(dest_path)
folder_list = os.listdir(path)
for num_imgs,folder in enumerate(folder_list):
print(str(num_imgs) + " : " + folder)
files_path = path + "/" + folder
image_list = os.listdir(files_path)
name_list.append(folder)
for image_name in image_list:
image_path = path + "/" + folder + "/" + image_name
print(image_path)
img = Image.open(image_path)
label_list.append(img)
for image in label_list:
image = image.resize((112, 112))
resized_list.append(image)
for (i,new) in enumerate (resized_list):
new.save('{}{}{}'.format(dest_path+'/'+str(label_list)+str('_'), i+1, '.jpg'))
resize_img('C:/Users/aunglay/PycharmProjects/FR_using_cnn/Face_database',
'C:/Users/aunglay/PycharmProjects/FR_using_cnn/resized_images')
出現錯誤:
Traceback (most recent call last):
File "C:/Users/aunglay/PycharmProjects/FR_using_cnn/smaple2.py", line 30, in <module>
'C:/Users/aunglay/PycharmProjects/FR_using_cnn/resized_images')
File "C:/Users/aunglay/PycharmProjects/FR_using_cnn/smaple2.py", line 27, in resize_img
new.save('{}{}{}'.format(dest_path+'/'+str(label_list)+str('_'), i+1, '.jpg'))
File "C:\Users\aunglay\Anaconda3\envs\tensorflow_env\lib\site-packages\PIL\Image.py", line 2099, in save
fp = builtins.open(filename, "w+b")
OSError: [Errno 22] Invalid argument: 'C:/Users/aunglay/PycharmProjects/FR_using_cnn/resized_images/[<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=64x64 at 0x2D7ACFD508>]_1.jpg'
問題似乎出在代碼的最后一行:
new.save('{}{}{}'.format(dest_path+'/'+str(label_list)+str('_'), i+1, '.jpg'))
具體來說,label_list 將是一個圖像對象列表,並且不包含您剛剛閱讀的圖像的名稱。 另外,我認為最后兩個 for 循環不是必需的。 您可以更改代碼:
for image_name in image_list:
image_path = path + "/" + folder + "/" + image_name
print(image_path)
img = Image.open(image_path)
label_list.append(img)
for image in label_list:
image = image.resize((112, 112))
resized_list.append(image)
for (i,new) in enumerate (resized_list):
new.save('{}{}{}'.format(dest_path+'/'+str(label_list)+str('_'), i+1, '.jpg'))
到:
for image_name in image_list:
image_path = path + "/" + folder + "/" + image_name
print(image_path)
img = Image.open(image_path)
label_list.append(img)
resized_list.append(img.resize((112, 112)))
resized_list[-1].save(dest_path+'/'+image_name+'.jpg')
這應該實現你所描述的。
編輯:關於評論中提出的問題:
def resize_img(path,dest_path):
name_list = []
label_list = []
resized_list = []
folder_list = os.listdir(path)
for num_imgs,folder in enumerate(folder_list):
if not os.path.exists(dest_path+'/'+folder):
os.makedir(dest_path+'/'+folder)
print(str(num_imgs) + " : " + folder)
files_path = path + "/" + folder
image_list = os.listdir(files_path)
name_list.append(folder)
for image_name in image_list:
image_path = path + "/" + folder + "/" + image_name
print(image_path)
img = Image.open(image_path)
label_list.append(img)
resized_list.append(img.resize((112, 112)))
resized_list[-1].save(dest_path+'/'+folder+'/'+image_name+'.jpg'
這應該實現您所描述的內容。 您需要為要創建的每個子文件夾調用 os.mkdir(),因此它必須位於第一個 for 循環內。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.