Skip to content

Commit

Permalink
Merge pull request #1281 from bghira/bugfix/square-image-no-crop
Browse files Browse the repository at this point in the history
add a special case for square input images where we need to resize to the target as intermediary, which can be considered a safe operation
  • Loading branch information
bghira authored Jan 18, 2025
2 parents d049c7a + c283a4c commit bfff2ed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion helpers/image_manipulation/training_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ def prepare(self, return_tensor: bool = False):
if not self.crop_enabled:
self.save_debug_image(f"images/{time.time()}-1b-nocrop-resize.png")
self.resize()
self.save_debug_image(f"images/{time.time()}-2-final-output.png")

image = self.image
if return_tensor:
Expand Down Expand Up @@ -512,7 +513,9 @@ def calculate_target_size(self):
self.aspect_ratio, self.resolution, self.original_size
)
)
if self.crop_aspect != "random" or not self.valid_metadata:
if (
self.crop_enabled and self.crop_aspect != "random"
) or not self.valid_metadata:
self.intermediary_size = calculated_intermediary_size
self.aspect_ratio = MultiaspectImage.calculate_image_aspect_ratio(
self.target_size
Expand Down
7 changes: 7 additions & 0 deletions helpers/multiaspect/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ def calculate_new_size_by_pixel_area(
logger.debug(
f"Returning the square edge {target_pixel_edge}x{target_pixel_edge} as the target size and original size as intermediary."
)
if W_initial == H_initial:
# if we have squares, resizing straight to the target is alright.
return (
(target_pixel_edge, target_pixel_edge),
(target_pixel_edge, target_pixel_edge),
aspect_ratio,
)
return (
(target_pixel_edge, target_pixel_edge),
(W_initial, H_initial),
Expand Down

0 comments on commit bfff2ed

Please sign in to comment.