Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: UnboundLocalError on input_image variable assignment #413

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

JPABotermans
Copy link

Fix: UnboundLocalError on input_image variable assignment

Summary

This PR fixes an UnboundLocalError caused by referencing the input_image variable before it is properly assigned when the image size is not divisible by 64.

Problem

When an image's dimensions (h or w) are not divisible by 64, the following block resizes the image. However, due to inconsistent handling of the input_image assignment, this error occurs:

input_image = input_image.resize((width, height)) # UnboundLocalError
This happens because input_image is referenced before being initialized in all cases, particularly when the image.mode != "RGBA".

Solution

Fixed the code by ensuring input_image is consistently assigned regardless of the image mode.
Corrected the logic to ensure input_image is initialized only once after the width and height adjustments.

if h % 64 != 0 or w % 64 != 0:
    width, height = map(lambda x: x - x % 64, (w, h))
    
    if image.mode == "RGBA":
        input_image = input_image.resize((width, height))
    else:
        input_image = image.resize((width, height))  # Fixed initialization
    
    print(f"WARNING: Your image is of size {h}x{w} which is not divisible by 64. Resizing to {height}x{width}!")

Impact

This fix eliminates the UnboundLocalError by ensuring proper initialization of the input_image variable.
The logic for resizing non-divisible images is now handled correctly for both RGBA and non-RGBA image modes.
Testing
Verified that the code runs without errors on images with dimensions both divisible and non-divisible by 64.
Tested with images in both RGBA and other modes to ensure correct behavior in resizing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant