将图像另存为缩略图 Python



我有这段代码,它原文来自这里:根据文件结构重命名文件 我从这里添加了一个缩略图保存为函数:使用 python 为 jpeg 创建缩略图图像

我正在尝试从目录结构中选择所有文件,根据文件结构重命名它们。我使用"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()  

您尚未在代码中的任何位置专门定义对象"图像"。请记住,Python 和许多其他语言都区分大小写。也许你的意思是"图像"?

最新更新