{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"},{"sourceId":7588545,"sourceType":"datasetVersion","datasetId":4417081}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport shutil\nfrom PIL import Image, ImageEnhance\nfrom skimage.transform import rotate\nimport random\n\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n'''\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session\n\n'''","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-02-08T12:39:47.153408Z","iopub.execute_input":"2024-02-08T12:39:47.153762Z","iopub.status.idle":"2024-02-08T12:39:47.162597Z","shell.execute_reply.started":"2024-02-08T12:39:47.153734Z","shell.execute_reply":"2024-02-08T12:39:47.161498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n# Clear output folder\nimport os\n\ndef remove_folder_contents(folder):\n    for the_file in os.listdir(folder):\n        file_path = os.path.join(folder, the_file)\n        try:\n            if os.path.isfile(file_path):\n                os.unlink(file_path)\n            elif os.path.isdir(file_path):\n                remove_folder_contents(file_path)\n                os.rmdir(file_path)\n        except Exception as e:\n            print(e)\n\nfolder_path = '/kaggle/working'\nremove_folder_contents(folder_path)\nos.rmdir(folder_path)\n'''","metadata":{"execution":{"iopub.status.busy":"2024-02-08T12:39:47.17235Z","iopub.execute_input":"2024-02-08T12:39:47.172719Z","iopub.status.idle":"2024-02-08T12:39:47.182027Z","shell.execute_reply.started":"2024-02-08T12:39:47.172689Z","shell.execute_reply":"2024-02-08T12:39:47.180945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nsrc_path = \"/kaggle/input/practice/images\"\ndst_path = \"/kaggle/working/practice1\"\nshutil.copytree(src_path, dst_path)\n'''","metadata":{"execution":{"iopub.status.busy":"2024-02-08T12:39:47.184149Z","iopub.execute_input":"2024-02-08T12:39:47.18527Z","iopub.status.idle":"2024-02-08T12:39:47.194198Z","shell.execute_reply.started":"2024-02-08T12:39:47.185229Z","shell.execute_reply":"2024-02-08T12:39:47.193087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def horizontal_flip_images(folder_path):\n    # Get a list of all files in the folder\n    file_list = os.listdir(folder_path)\n\n    # Iterate through each file in the folder\n    for file_name in file_list:\n        # Check if the file is an image\n        if file_name.lower().endswith('.png'):\n            # Create the full path to the image file\n            image_path = os.path.join(folder_path, file_name)\n\n            # Open the image using Pillow\n            original_image = Image.open(image_path)\n\n            # Perform horizontal flipping\n            flipped_image = original_image.transpose(Image.FLIP_LEFT_RIGHT)\n\n            # Create a new file name for the flipped image\n            flipped_image_path = os.path.join(folder_path, f\"hflipped_{file_name}\")\n\n            # Save the flipped image\n            flipped_image.save(flipped_image_path)\n\n            print(f\"Flipped image saved: {flipped_image_path}\")","metadata":{"execution":{"iopub.status.busy":"2024-02-08T12:39:47.196135Z","iopub.execute_input":"2024-02-08T12:39:47.19657Z","iopub.status.idle":"2024-02-08T12:39:47.20755Z","shell.execute_reply.started":"2024-02-08T12:39:47.196535Z","shell.execute_reply":"2024-02-08T12:39:47.20633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def vertical_flip_images(folder_path):\n    # Get a list of all files in the folder\n    file_list = os.listdir(folder_path)\n\n    # Iterate through each file in the folder\n    for file_name in file_list:\n        # Check if the file is an image\n        if file_name.lower().endswith('.png'):\n            # Create the full path to the image file\n            image_path = os.path.join(folder_path, file_name)\n\n            # Open the image using Pillow\n            original_image = Image.open(image_path)\n\n            # Perform horizontal flipping\n            flipped_image = original_image.transpose(Image.FLIP_TOP_BOTTOM)\n\n\n            # Create a new file name for the flipped image\n            flipped_image_path = os.path.join(folder_path, f\"vflipped_{file_name}\")\n\n            # Save the flipped image\n            flipped_image.save(flipped_image_path)\n\n            print(f\"Flipped image saved: {flipped_image_path}\")","metadata":{"execution":{"iopub.status.busy":"2024-02-08T12:39:47.212948Z","iopub.execute_input":"2024-02-08T12:39:47.213342Z","iopub.status.idle":"2024-02-08T12:39:47.223164Z","shell.execute_reply.started":"2024-02-08T12:39:47.213289Z","shell.execute_reply":"2024-02-08T12:39:47.222361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def randRange(a, b):\n    '''\n    A function to generate random float values in the desired range\n    '''\n    return random.uniform(a, b)\n\ndef random_crop(image):\n    '''\n    Cropping the image in the center from a random margin from the borders\n    '''\n    margin = 1 / 3.5\n    width, height = image.size\n    start_x = int(randRange(0, width * margin))\n    start_y = int(randRange(0, height * margin))\n    end_x = int(randRange(width * (1 - margin), width))\n    end_y = int(randRange(height * (1 - margin), height))\n\n    cropped_image = image.crop((start_x, start_y, end_x, end_y))\n    return cropped_image\n\ndef crop_images(folder_path):\n    # Get a list of all files in the folder\n    file_list = os.listdir(folder_path)\n\n    # Iterate through each file in the folder\n    for file_name in file_list:\n        # Check if the file is an image\n        if file_name.lower().endswith('.png'):\n            # Create the full path to the image file\n            image_path = os.path.join(folder_path, file_name)\n\n            # Open the image using Pillow\n            original_image = Image.open(image_path)\n\n            # Apply random cropping\n            cropped_image = random_crop(original_image)\n\n            # Create a new file name for the cropped images\n            cropped_image_path = os.path.join(folder_path, f\"cropped_{file_name}\")\n\n            # Save the cropped image\n            cropped_image.save(cropped_image_path)\n\n            print(f\"Cropped image saved: {cropped_image_path}\")","metadata":{"execution":{"iopub.status.busy":"2024-02-08T12:39:47.239005Z","iopub.execute_input":"2024-02-08T12:39:47.24031Z","iopub.status.idle":"2024-02-08T12:39:47.250976Z","shell.execute_reply.started":"2024-02-08T12:39:47.240271Z","shell.execute_reply":"2024-02-08T12:39:47.249834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def adjust_random_brightness(image_path):\n    \"\"\"\n    Adjust the brightness of an image to a random factor between 0.5 and 1.5 and save the result.\n    Parameters:\n        -image_path: The file path of the input image.\n        - output_path: The file path for the output image.\n        \"\"\"\n    file_list = os.listdir(folder_path)\n\n    # Iterate through each file in the folder\n    for file_name in file_list:\n        # Check if the file is an image\n        if file_name.lower().endswith('.png'):\n            # Create the full path to the image file\n            image_path = os.path.join(folder_path, file_name)\n\n            # Open the image using Pillow\n            original_image = Image.open(image_path)\n\n            # Perform horizontal flipping\n            # Generate a random brightness factor between 0.5 and 1.5\n            random_factor = random.uniform(0.5, 1.5)\n            print(f\"Adjusting brightness by a factor of {random_factor}\")\n            # Create a brightness enhancer and apply the random factor\n            enhancer = ImageEnhance.Brightness(original_image)\n\n            BA_image = enhancer.enhance(random_factor)\n            # Create a new file name for the flipped image\n            BA_path = os.path.join(folder_path, f\"BA_{file_name}\")\n            # Save the flipped image\n            BA_image.save(BA_path)\n            print(f\"Brightness adjusted image saved: {BA_path}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-02-08T12:39:47.28882Z","iopub.execute_input":"2024-02-08T12:39:47.289199Z","iopub.status.idle":"2024-02-08T12:39:47.29813Z","shell.execute_reply.started":"2024-02-08T12:39:47.289171Z","shell.execute_reply":"2024-02-08T12:39:47.296666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_random_gaussian_noise(folder_path):\n    \"\"\"\n    Add random Gaussian noise to an image and save the result.\n\n    Parameters:\n    - image_path: The file path of the input image.\n    - output_path: The file path for the output image.\n    - min_sigma: Minimum standard deviation of the Gaussian noise.\n    - max_sigma: Maximum standard deviation of the Gaussian noise.\n    \"\"\"\n    # Iterate through each file in the folder\n    file_list = os.listdir(folder_path)\n    for file_name in file_list:\n        # Check if the file is an image\n        if file_name.lower().endswith('.png'):\n            # Create the full path to the image file\n            image_path = os.path.join(folder_path, file_name)\n\n            # Open the image using Pillow\n            original_image = Image.open(image_path)\n\n            #Adding noise\n            image_array = np.array(original_image)\n            # Ensure image is in float format to avoid overflow or underflow\n            image_array = image_array.astype(float)\n\n            # Generate random Gaussian noise\n            sigma = random.uniform(50, 100)\n            noise = np.random.normal(0, sigma, image_array.shape)\n\n            # Add the noise to the image\n            noisy_image = image_array + noise\n\n            # Ensure values remain within the valid range [0, 255]\n            noisy_image = np.clip(noisy_image, 0, 255)\n\n            # Convert back to an image\n            noisy_image = noisy_image.astype(np.uint8)\n            noisy_image_pil = Image.fromarray(noisy_image)\n\n\n            # Create a new file name for the adjusted image\n            noisy_path = os.path.join(folder_path, f\"noisy_{file_name}\")\n\n            # Save the noisy image\n            noisy_image_pil.save(noisy_path)\n            print(f\"Added Gaussian noise with sigma = {sigma}. Saved to {noisy_path}\")","metadata":{"execution":{"iopub.status.busy":"2024-02-08T12:39:47.300525Z","iopub.execute_input":"2024-02-08T12:39:47.300963Z","iopub.status.idle":"2024-02-08T12:39:47.315166Z","shell.execute_reply.started":"2024-02-08T12:39:47.300933Z","shell.execute_reply":"2024-02-08T12:39:47.31426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def adjust_random_contrast(folder_path):\n    \"\"\"\n    Adjust the contrast of an image by a random factor and save the result.\n\n    Parameters:\n    - image_path: The file path of the input image.\n    - output_path: The file path for the output image.\n    - min_factor: Minimum contrast factor (less than 1 reduces contrast, 1 is original contrast).\n    - max_factor: Maximum contrast factor (greater than 1 increases contrast).\n    \"\"\"\n    file_list = os.listdir(folder_path)\n\n    # Iterate through each file in the folder\n    for file_name in file_list:\n        # Check if the file is an image\n        if file_name.lower().endswith('.png'):\n            # Create the full path to the image file\n            image_path = os.path.join(folder_path, file_name)\n\n            # Open the image using Pillow\n            original_image = Image.open(image_path)\n\n            # Generate a random contrast factor\n            random_factor = random.uniform(0.5, 1.5)\n            print(f\"Applying contrast factor: {random_factor}\")\n\n            # Create a brightness enhancer and apply the random factor\n            enhancer = ImageEnhance.Contrast(original_image)\n\n            CA_image = enhancer.enhance(random_factor)\n\n            # Create a new file name for the adjusted image\n            CA_path = os.path.join(folder_path, f\"CA_{file_name}\")\n\n            # Save the adjusted image\n            CA_image.save(CA_path)\n            print(f\"Contrast adjusted image saved: {CA_path}\")","metadata":{"execution":{"iopub.status.busy":"2024-02-08T12:42:29.337884Z","iopub.execute_input":"2024-02-08T12:42:29.33839Z","iopub.status.idle":"2024-02-08T12:42:29.348819Z","shell.execute_reply.started":"2024-02-08T12:42:29.338348Z","shell.execute_reply":"2024-02-08T12:42:29.347382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Calling the functions\nfolder_path = \"/kaggle/working/practice1\"\n\nhorizontal_flip_images(folder_path)\nvertical_flip_images(folder_path)\ncrop_images(folder_path)\nadjust_random_brightness(folder_path)\nadjust_random_contrast(folder_path)\nadd_random_gaussian_noise(folder_path)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"adjust_random_contrast(folder_path)","metadata":{"execution":{"iopub.status.busy":"2024-02-08T12:42:34.250463Z","iopub.execute_input":"2024-02-08T12:42:34.250856Z","iopub.status.idle":"2024-02-08T12:44:56.634955Z","shell.execute_reply.started":"2024-02-08T12:42:34.250825Z","shell.execute_reply":"2024-02-08T12:44:56.633523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_random_gaussian_noise(folder_path)","metadata":{"execution":{"iopub.status.busy":"2024-02-08T12:45:24.340306Z","iopub.execute_input":"2024-02-08T12:45:24.340692Z","iopub.status.idle":"2024-02-08T12:55:42.986643Z","shell.execute_reply.started":"2024-02-08T12:45:24.340664Z","shell.execute_reply":"2024-02-08T12:55:42.984592Z"},"trusted":true},"execution_count":null,"outputs":[]}]}