0

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
3
  • Where does it say that? Do you get an error? What's the full traceback? Commented Apr 23, 2021 at 21:35
  • I forgot to add that. I edited my post and it has that at the end now Commented Apr 23, 2021 at 21:53
  • Does this answer your question? Can't Open files from a directory in python Commented Apr 23, 2021 at 22:42

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I have made a program similar to these and I could read the files and even on this, if I take the image.close(). I get the message "Problem opening image test.png!", which test is the name of the image I have on that folder. This means that im is readable and the path is correct. I think the function shouldn't even go to except: cause that means there's a problem in the first place
@Pedro The point is that just from the fact that the line image.close() is executed, it means that image is not assigned. Now you need to check why the error happened in the first place. One way to do that is temporarily remove the except to actually see the full error
@Pedro Hint: you're already doing somewhere join(Folder_Input, f). Think where you need it else
You don't even need to close the image. When you reassign the name to something else, that Image object will have no references bound to it and will soon be garbage collected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.