Fix: UnboundLocalError on input_image variable assignment #413
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix:
UnboundLocalError
on input_image variable assignmentSummary
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.
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.