2017-07-01 105 views
0

我有這樣的代碼,它origionally來自這裏:renaming files base on the file structure 我添加了一個縮略圖另存爲從這裏功能:Create thumbnail images for jpegs with python保存圖像縮略圖的Python

我試圖從一個目錄結構,選擇所有文件,根據文件結構重新命名它們。我已經使用'in'添加了一個過濾器,但是我無法將縮放比例調整爲可用。

名稱圖像未定義。

import os 
import shutil 
#testing 
import sys 
import image 


def image_thumb_filter(): 
size = 128, 128 
for path, dirs, files in os.walk(wd): 
    for file in files: 
    if file.endswith(".jpg"): 
     src = os.path.join(path, file) 
     #print(src) 
     dst = os.path.join(os.getcwd(), "Photo_selection") 
     if not os.path.exists(dst): #Checks 'Photos_mirror' folder exists 
otherwise creates 
     os.makedirs(dst) 
     new_name = src.replace(wd+'\\', '').replace('\\', '_') 
     new_dst = os.path.join(dst, new_name + "_thumb") 
     if not os.path.isfile(new_dst): 
     if "99\\526" in src: 
      try: 
       im = image.open(src) 
       im.thumbnail(size) 
       im.save(new_dst, "JPEG") 
      except IOError: 
       print ("cannot create thumbnail for", infile)    
    shutil.copy(src, dst) 
    old_dst = os.path.join(dst, file) 
       #Assuming your /dir/dir/... structure is inside of your working 
    directory (wd) 
    os.rename(old_dst, new_dst) 
    else: # it doesn't like this else statement 
    continue 
    image_thumb_filter() 

回答

0

您尚未在您的代碼中的任何位置定義對象「Image」。請記住,Python和許多其他語言都區分大小寫。也許你的意思是「形象?」

+0

哪些語言不區分大小寫? – Holloway

+0

我編輯了代碼,仍然無法工作....我安裝了... pip安裝圖像... pilow,django,olefile,pytz,返回:Traceback(最近調用最後一個): 文件「」 ,第1行,在 ModuleNotFoundError:沒有名爲'image'的模塊 –

+0

@Holloway - 平方 –