Spatial Digger
Spatial Digger

Reputation: 1993

Saving images as thumbnails Python

I have this code, it origionally comes from here: renaming files base on the file structure I added a thumbnail save as to the function from here: Create thumbnail images for jpegs with python

I'm trying to select all files from a directory structure, rename them based on the file structure. I've added a filter using 'in', but I can't get the resize as thumbnail bit to work.

Name Image is not defined.

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()  

Upvotes: 0

Views: 149

Answers (1)

Julian Rachman
Julian Rachman

Reputation: 575

You have not defined the object "Image" specifically anywhere in your code. Remember that Python and many other languages are case sensitive. Maybe you meant "image?"

Upvotes: 0

Related Questions