1717- `resize(image, size)`: Resize the input image to the specified size.
1818- `crop(image, box)`: Crop the input image to the specified rectangular region.
1919- `random_crop(image, size)`: Randomly crop a region from the input image.
20- - `perform_image_augmentation(source_dir, target_dir, flip_horizontal=False, flip_vertical=False, rotate=False, random_rotation=False, resize=False, crop=False, max_angle=30, target_size=(224, 224), crop_size=(150, 150))`: Perform image augmentation on images in the source directory and its subdirectories.
2120"""
2221
23- import os
2422import random
2523import time
2624import nltk
@@ -329,73 +327,4 @@ def random_crop(image, size):
329327 upper = random .randint (0 , height - size [1 ])
330328 right = left + size [0 ]
331329 lower = upper + size [1 ]
332- return crop (image , (left , upper , right , lower ))
333-
334- def perform_image_augmentation (
335- source_dir ,
336- target_dir ,
337- flip_horizontal = False ,
338- flip_vertical = False ,
339- rotate = False ,
340- random_rotation = False ,
341- resize = False ,
342- crop = False ,
343- max_angle = 30 ,
344- target_size = (224 , 224 ),
345- crop_size = (150 , 150 )
346- ):
347- """
348- Perform image augmentation on images in the source directory and its subdirectories.
349-
350- Parameters:
351- - `source_dir` (str): The path to the source directory containing the images.
352- - `target_dir` (str): The path to the target directory where augmented images will be saved.
353- - `flip_horizontal` (bool): Perform horizontal flipping if True. Default is False.
354- - `flip_vertical` (bool): Perform vertical flipping if True. Default is False.
355- - `rotate` (bool): Perform rotation if True. Default is False.
356- - `random_rotation` (bool): Perform random rotation if True. Default is False.
357- - `resize` (bool): Perform resizing if True. Default is False.
358- - `crop` (bool): Perform cropping if True. Default is False.
359- - `max_angle` (float): The maximum absolute angle of rotation in degrees. Default is 30.
360- - `target_size` (tuple): The target size for resizing in the format (width, height). Default is (224, 224).
361- - `crop_size` (tuple): The size of the cropped region in the format (width, height). Default is (150, 150).
362- """
363-
364- def augment_image (image_path ):
365- image = Image .open (image_path )
366-
367- if flip_horizontal and random .random () > 0.5 :
368- image = flip_horizontal (image )
369-
370- if flip_vertical and random .random () > 0.5 :
371- image = flip_vertical (image )
372-
373- if rotate and random .random () > 0.5 :
374- image = rotate (image , max_angle )
375-
376- if random_rotation and random .random () > 0.5 :
377- image = random_rotation (image , max_angle )
378-
379- if resize and random .random () > 0.5 :
380- image = resize (image , target_size )
381-
382- if crop and random .random () > 0.5 :
383- image = random_crop (image , crop_size )
384-
385- target_path = os .path .join (target_dir , os .path .relpath (image_path , source_dir ))
386- os .makedirs (os .path .dirname (target_path ), exist_ok = True )
387- image .save (target_path )
388-
389- num_images = 0
390- num_directories = 0
391-
392- for root , _ , files in os .walk (source_dir ):
393- num_directories += 1
394- for file in files :
395- if file .lower ().endswith (('.jpg' , '.jpeg' , '.png' , '.gif' , '.bmp' )):
396- num_images += 1
397- image_path = os .path .join (root , file )
398- augment_image (image_path )
399- print (f"Processed { num_images } images in { num_directories } directories." , end = '\r ' )
400-
401- print ("\n Image augmentation completed successfully." )
330+ return crop (image , (left , upper , right , lower ))
0 commit comments