{ "cells": [ { "cell_type": "markdown", "id": "77d13a9e", "metadata": {}, "source": [ "224x224 " ] }, { "cell_type": "code", "execution_count": null, "id": "3018959e", "metadata": {}, "outputs": [], "source": [ "import os\n", "from PIL import Image, ImageOps\n", "\n", "input_root = 'Tomato' \n", "output_root = 'Tomato_512' \n", "os.makedirs(output_root, exist_ok=True)\n", "\n", "def process_image(input_path, output_path, size=(512, 512)):\n", " try:\n", " with Image.open(input_path) as img:\n", " img = img.convert(\"RGB\")\n", "\n", " img = ImageOps.fit(img, size, Image.LANCZOS, centering=(0.5, 0.5))\n", " os.makedirs(os.path.dirname(output_path), exist_ok=True)\n", " img.save(output_path, \"JPEG\", quality=95)\n", " except Exception as e:\n", " print(f\"Error processing {input_path}: {e}\")\n", "\n", "for root, _, files in os.walk(input_root):\n", " for file in files:\n", " if file.lower().endswith((\".jpg\", \".jpeg\")):\n", " input_path = os.path.join(root, file)\n", " rel_path = os.path.relpath(input_path, input_root)\n", " output_path = os.path.join(output_root, rel_path)\n", " process_image(input_path, output_path)\n", "\n", "print(\"All images processed and saved in\", output_root)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "27f4b7b5", "metadata": {}, "outputs": [], "source": [ "import os\n", "from PIL import Image, ImageOps\n", "\n", "input_root = 'Onion' \n", "output_root = 'Onion_512' \n", "os.makedirs(output_root, exist_ok=True)\n", "\n", "def process_image(input_path, output_path, size=(512, 512)):\n", " try:\n", " with Image.open(input_path) as img:\n", " img = img.convert(\"RGB\")\n", " img = ImageOps.fit(img, size, Image.LANCZOS, centering=(0.5, 0.5))\n", " os.makedirs(os.path.dirname(output_path), exist_ok=True)\n", " img.save(output_path, \"JPEG\", quality=95)\n", " except Exception as e:\n", " print(f\"Error processing {input_path}: {e}\")\n", "\n", "for root, _, files in os.walk(input_root):\n", " for file in files:\n", " if file.lower().endswith((\".jpg\", \".jpeg\")):\n", " input_path = os.path.join(root, file)\n", " rel_path = os.path.relpath(input_path, input_root)\n", " output_path = os.path.join(output_root, rel_path)\n", " process_image(input_path, output_path)\n", "\n", "print(\"All images processed and saved in\", output_root)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "5a0d918b", "metadata": {}, "outputs": [], "source": [ "import os\n", "from PIL import Image, ImageOps\n", "\n", "input_root = 'Pear' \n", "output_root = 'Pear_512' \n", "os.makedirs(output_root, exist_ok=True)\n", "\n", "def process_image(input_path, output_path, size=(512, 512)):\n", " try:\n", " with Image.open(input_path) as img:\n", " img = img.convert(\"RGB\")\n", " img = ImageOps.fit(img, size, Image.LANCZOS, centering=(0.5, 0.5))\n", " os.makedirs(os.path.dirname(output_path), exist_ok=True)\n", " img.save(output_path, \"JPEG\", quality=95)\n", " except Exception as e:\n", " print(f\"Error processing {input_path}: {e}\")\n", "\n", "for root, _, files in os.walk(input_root):\n", " for file in files:\n", " if file.lower().endswith((\".jpg\", \".jpeg\")):\n", " input_path = os.path.join(root, file)\n", " rel_path = os.path.relpath(input_path, input_root)\n", " output_path = os.path.join(output_root, rel_path)\n", " process_image(input_path, output_path)\n", "\n", "print(\"All images processed and saved in\", output_root)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "f1d5e72a", "metadata": {}, "outputs": [], "source": [ "import os\n", "from PIL import Image, ImageOps\n", "\n", "input_root = 'Strawberry' \n", "output_root = 'Strawberry_512' \n", "os.makedirs(output_root, exist_ok=True)\n", "\n", "def process_image(input_path, output_path, size=(512, 512)):\n", " try:\n", " with Image.open(input_path) as img:\n", " img = img.convert(\"RGB\")\n", " img = ImageOps.fit(img, size, Image.LANCZOS, centering=(0.5, 0.5))\n", " os.makedirs(os.path.dirname(output_path), exist_ok=True)\n", " img.save(output_path, \"JPEG\", quality=95)\n", " except Exception as e:\n", " print(f\"Error processing {input_path}: {e}\")\n", "\n", "\n", "for root, _, files in os.walk(input_root):\n", " for file in files:\n", " if file.lower().endswith((\".jpg\", \".jpeg\")):\n", " input_path = os.path.join(root, file)\n", " rel_path = os.path.relpath(input_path, input_root)\n", " output_path = os.path.join(output_root, rel_path)\n", " process_image(input_path, output_path)\n", "\n", "print(\"All images processed and saved in\", output_root)\n" ] } ], "metadata": { "kernelspec": { "display_name": "myenv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.21" } }, "nbformat": 4, "nbformat_minor": 5 }