path_school="/content/drive/MyDrive"
test_path=path_school "//" "test"
processedex_path=path_school "//" "test_ex"
for (path, dir, files) in os.walk(train_path):
for filename in files:
ext = os.path.splitext(filename)[-1]
test_folder_list = [f for f in os.listdir(path_school '//' 'test')]
for f in os.listdir(test_path):
fol=os.path.splitext(f)[-1]
'''
os.makedirs(processedex_path "//" f)
'''
if ext == '.jpg':
img=Image.open ("%s/%s" % (path, filename)).convert('L')
img=img.resize((256,256))
img.save(processedex_path "//" f "//" "pre" "_" filename)
在'test_path'有很多類似的檔案夾'A356_4_50_TS_167'和該檔案夾中有名為images'0232-0024.jpg'.
我想保存在正確的位置的檔案夾“圖片A356_4_50_TS_167'中的'processedex_path'檔案夾中。此代碼將每個更改的影像保存在每個檔案夾中。請幫助我將影像保存在正確的檔案夾中。
在此處輸入圖片說明
在此處輸入圖片說明
這些是我的原始路徑,我想將影像保存在 'test_ex'(=processedex_path) 檔案夾中的同名檔案夾中
在此處輸入影像描述, 但每個檔案夾中的每個影像都保存在每個檔案夾中,不是每個檔案夾 2 個影像,而是每個檔案夾 70 個影像我想每個檔案夾保存 2 個影像
謝謝你的回答
uj5u.com熱心網友回復:
我無法運行你的代碼,但我認為你有太多的for-loops
我會做
import os
from PIL import Image
path_school = "/content/drive/MyDrive"
# source & destination folder
test_path = os.path.join(path_school, "test")
processedex_path = os.path.join(path_school, "test_ex")
os.makedirs(processedex_path, exist_ok=True)
for subfolder in os.listdir(test_path):
# source & destination subfolder
src_subfolder = os.path.join(test_path, subfolder)
dst_subfolder = os.path.join(processedex_path, subfolder)
if os.path.isdir(src_subfolder): # check if it is really subfolder
os.makedirs(dst_subfolder, exist_ok=True)
for filename in os.listdir(src_subfolder):
if filename.lower().endswith( ('.jpg', '.png', 'webp') ):
# source & destination file
src_file = os.path.join(src_subfolder, filename)
dst_file = os.path.join(dst_subfolder, "pre_" filename)
img = Image.open(src_file).convert('L')
img = img.resize((256, 256))
img.save(dst_file)
print('converted:', filename)
print('src:', src_file)
print('dst:', dst_file)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/370672.html
