Python Program Doesn't Recognize Input

Viewed 73

I have a program that is supposed to convert an image to a squared png with transparent borders (if the image is 1000x1500 then the png is 1500x1500 and then the image will be placed on the middle of the png creating a transparent border of 250x1500 on both sides). The program is supposed to import the image from a folder which I you can choose which you wish to import from, and then export it to another folder that you can also choose. I use recursion to go throw all images from the folder, however it says that the input is not recognizable. Can anyone help me with this one? You have everything you need explained on the code I'll send right now:

from PIL import Image
from os import listdir
from os.path import isfile, join,splitext

Folder_Input = 'D:\\0Pi_\Downloads\ScpServer\Originais'                         #directory where we will upload images
Folder_Output ='D:\\0Pi_\Downloads\ScpServer\PNGs'                              #directory where we will save images

typelist = ['.png','.jpg','.jpeg']
images_list = [f for f in listdir(Folder_Input) if isfile(join(Folder_Input, f)) and splitext(f)[1].lower() in typelist]

for im in images_list:
    try:
        image = Image.open(im)                                                  #this is supposed to open the image, however it doesn't. what's missing
    except:
        print(f'Problem opening image{im}!\n')
        image.close()
        continue
    width, height = image.size
    
    if height >= width:
        new_width = height
        new_height = height
    else:
        new_width = width
        new_height = width

    result = Image.new(mode = 'RGBA', size = (new_width, new_height) ,color = (0,0,0,0))   #creates a new transparent squared png
    offset = ((new_width - width) // 2, (new_height - height) // 2)             #centers the image in relation to the squared png
    
    result.paste(image,offset)                                                  #unites both the image and the squared layer
    image.close()                                                               #closes the file now finished
    
    output_file = join(Folder_Output,splitext(im)[0] + '.png')
    if not isfile(output_file):
        result.save(output_file)                                                #saves file on the output folder
    else:
        print(f"The file {splitext(im)[0] + '.png'} was not saved because it already exists on {Folder_Output}!\n")

This is the Tracebak:

Traceback (most recent call last):
  File "D:/0Pi_/Documents/add_borders.py", line 16, in <module>
    image.close()
builtins.NameError: name 'image' is not defined
1 Answers

If the try/except block fails, it may not have assigned the object to the image variable.

try:
    image = Image.open(im)  # this is supposed to open the image, however it doesn't. what's missing
except:
    print(f'Problem opening image{im}!\n')
    image.close() # will have problem here

Since, you are looking only to the Image.open(im) if it was succeed, just remove image.close() from the except or, use another try/except to close the object. Maybe the im is not readable or have a wrong path, so that will cause to fail.

Related