Aug-13-2020, 02:24 PM
(Aug-13-2020, 12:01 PM)GOTO10 Wrote: When you pass the path variable to os.chdir(), you need it to be a valid directory like 'C:\TMP'. When you pass the path variable to glob.glob() (lines 18 and 36), you need it to include a wildcard like 'C:\TMP\*'. My guess is you are passing path without a wildcard in lines 18 and 36, so your glob.glob() loop is not actually finding anything that satisfies the if condition (nothing that ends with .jpg or .png).
import glob print('Printing items using C:\TMP as path') for item in glob.glob('C:\TMP'): print(item) print('Printing items using C:\TMP\* as path') for item in glob.glob('C:\TMP\*'): print(item)
Output:Printing items using C:\TMP as path C:\TMP Printing items using C:\TMP\* as path C:\TMP\Testing.jpg C:\TMP\Placeholder.jpg
Thank you - I have managed to make some changes so that the code for resizing works again. So that part is all sorted. I just now need to make sure that all images save into the directory created (which would be a user path to a folder named thumbnails - already created in line seven (please see below)
from PIL import Image import os imgs = [] path = input("Where are your images? > ") x = os.chdir(path) new_path = os.makedirs('Thumbnails') valid_images = [".jpg",".gif",".png",".tga", ".bmp", ".svg", ".tif"] for f in os.listdir(path): ext = os.path.splitext(f)[1] if ext.lower() not in valid_images: print ("This folder contains invalid file types - please check again. ") continue imgs.append(Image.open(os.path.join(path,f))) print(f) viewimgs = Image.open(os.path.join(path,f)) #viewimgs.show() fn, fext = os.path.splitext(f) new_height = int(input("Height of thumbnail (in pixels)? ")) new_width = int(input("Width of thumbnail (in pixels)? ")) new_size = viewimgs.resize((new_width, new_height)) new_size.show() new_size.save(new_path + fext)How do I adjust line 26 to make sure that it saves in the user path subdirectory named "thumbnails" and not in the original folder?
Many thanks,
paulmerton4pope