Python Forum
Changing Directory based on user input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing Directory based on user input
#11
(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
Reply
#12
Your assignment statements on line 6 and 7 are not doing what you think they are. In each case, you are assigning a variable to the value returned by a function call. Both of these functions just return the Python default value of None, so if you use them later (as you do with new_path in line 26), you are passing a value of None. When you make the call on line 26, you need to ensure that you are passing a valid path and filename, so make sure you're assigning your path variables the values you actually want them to contain.

Also, be aware that os.makedirs('Thumbnails') will fail if the Thumbnails folder already exists. Here is one way to correctly assign a variable to the desired thumbnails path and create that folder only if it doesn't already exist (this do:

path = input("Where are your images? > ")
# you should probably add code here to verify that the path entered by the user is valid before proceeding
thumb_path = (path + '\\Thumbnails')
if not os.path.exists(thumb_path):
    os.mkdir(thumb_path)
Reply
#13
(Aug-13-2020, 02:54 PM)GOTO10 Wrote: Your assignment statements on line 6 and 7 are not doing what you think they are. In each case, you are assigning a variable to the value returned by a function call. Both of these functions just return the Python default value of None, so if you use them later (as you do with new_path in line 26), you are passing a value of None. When you make the call on line 26, you need to ensure that you are passing a valid path and filename, so make sure you're assigning your path variables the values you actually want them to contain.

Also, be aware that os.makedirs('Thumbnails') will fail if the Thumbnails folder already exists. Here is one way to correctly assign a variable to the desired thumbnails path and create that folder only if it doesn't already exist (this do:

path = input("Where are your images? > ")
# you should probably add code here to verify that the path entered by the user is valid before proceeding
thumb_path = (path + '\\Thumbnails')
if not os.path.exists(thumb_path):
    os.mkdir(thumb_path)

Thank you for that - that's a great help!

The images are saving to the created folder (which is great) but I am now getting the print message from line 13 (regarding invalid file type) - is there a way in which I can still keep the

 new_size.save( "Thumbnail " + fn + ext)
that now works, but make sure that it doesn't print the error?

I am hoping that this will be my last question with this - I really appreciate your help with this.

Many thanks,
paulmerton4pope
Reply
#14
I think you just need to rethink your logic in the for loop at line 10 and your error condition/message in lines 12-14. You are currently looping through all of the contents of the file path and printing the error each time an item doesn't have one of your chosen extensions. You are guaranteed to encounter the error at least once per execution, because the Thumbnails folder is going to be one of the items in the path, and that will trigger the message since it doesn't have an extension. It would probably make more sense to print the error only if there are no valid image files to work with, but otherwise just ignore any non-image files as long as there is at least one valid image in the path.

There are lots of ways to approach this, but one thing that might make it easier is to first create a list of items in the path, then work with the list:

file_list = [f for f in os.listdir(path)]
# put a check here to confirm that at least one valid image is present in file_list, otherwise print error and start over
# after confirming there is at least one valid image, remove non-images from file_list
# loop through file_list, which now contains only valid images in the path, to perform image manipulations
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Print user input into triangle djtjhokie 1 804 Nov-07-2020, 07:01 PM
Last Post: buran
  sys.stdin to do a word count based on user entry Kaltex 3 2,125 Jul-19-2020, 01:54 PM
Last Post: deanhystad
  how to add the user input from file into list wilson20 8 2,003 May-03-2020, 10:52 PM
Last Post: Larz60+
  Writing a function that changes its answer based on user input SirRavenclaw 2 1,242 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  Print the longest str from user input edwdas 5 1,951 Nov-04-2019, 02:02 PM
Last Post: perfringo
  how to add user input to a dictionary to a graph KINGLEBRON 3 1,482 Jul-31-2019, 09:09 PM
Last Post: SheeppOSU
  New to Python - tiny coding assistance on user input function and assign to variable Mountain_Duck 1 1,299 Mar-23-2019, 06:54 PM
Last Post: Yoriz
  Extracting list element with user input valve 1 1,247 Mar-11-2019, 07:37 PM
Last Post: Yoriz
  turtle polygon as specified by user input johneven 7 5,803 Mar-02-2019, 10:11 PM
Last Post: johneven
  Print The Length Of The Longest Run in a User Input List Ashman111 3 1,856 Oct-26-2018, 06:56 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020