Merge pull request #211 from a-l-e-x-d-s-9/main

Refactored the _percent_random_crop function to prevent unnecessary stretch.
This commit is contained in:
Victor Hall 2023-07-04 19:20:42 -04:00 committed by GitHub
commit 4218901a08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 22 deletions

View File

@ -203,36 +203,23 @@ class ImageTrainItem:
randomly crops the image by a percentage of the image size on each of the four sides randomly crops the image by a percentage of the image size on each of the four sides
""" """
width, height = image.size width, height = image.size
max_crop_pixels = min(width, height) * crop_jitter max_crop_pixels = int(min(width, height) * crop_jitter)
left_crop_pixels = random.uniform(0, max_crop_pixels) left_crop_pixels = int(round(random.uniform(0, max_crop_pixels)))
right_crop_pixels = random.uniform(0, max_crop_pixels) right_crop_pixels = int(round(random.uniform(0, max_crop_pixels)))
top_crop_pixels = random.uniform(0, max_crop_pixels) top_crop_pixels = int(round(random.uniform(0, max_crop_pixels)))
bottom_crop_pixels = random.uniform(0, max_crop_pixels) bottom_crop_pixels = int(round(random.uniform(0, max_crop_pixels)))
# print(f"{left_crop_pixels}, {right_crop_pixels}, {top_crop_pixels}, {bottom_crop_pixels}, ")
left = left_crop_pixels left = left_crop_pixels
right = width - right_crop_pixels right = width - right_crop_pixels
top = top_crop_pixels top = top_crop_pixels
bottom = height - bottom_crop_pixels bottom = height - bottom_crop_pixels
#print(f"\n *** jitter l: {left}, t: {top}, r: {right}, b: {bottom}, orig w: {width}, h: {height}, max_crop_pixels: {max_crop_pixels}")
cropped_image = image.crop((left, top, right, bottom)) crop_size = image.crop((left, top, right, bottom))
cropped_width = width - int(left_crop_pixels + right_crop_pixels) return crop_size
cropped_height = height - int(top_crop_pixels + bottom_crop_pixels)
cropped_aspect_ratio = cropped_width / cropped_height
if cropped_aspect_ratio > 1:
new_width = cropped_width
new_height = int(cropped_width / cropped_aspect_ratio)
else:
new_width = int(cropped_height * cropped_aspect_ratio)
new_height = cropped_height
cropped_image = cropped_image.resize((new_width, new_height))
return cropped_image
def _debug_save_image(self, image, folder=""): def _debug_save_image(self, image, folder=""):
base_name = os.path.basename(self.pathname) base_name = os.path.basename(self.pathname)