Last active
July 15, 2023 01:21
-
-
Save stellasphere/e929aaa893d189a935e521672db5363b to your computer and use it in GitHub Desktop.
convert-color-space.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "provenance": [], | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/stellasphere/e929aaa893d189a935e521672db5363b/convert-color-space.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "# Import PIL (Python Imaging Library)" | |
| ], | |
| "metadata": { | |
| "id": "ZUKeujCMl7bI" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "id": "IVMkzw6plz4Y" | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "from PIL import Image" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "# Helper Functions" | |
| ], | |
| "metadata": { | |
| "id": "fBdahwHnpp_d" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "def change_colorspace_to_rgb(image_path):\n", | |
| " export_dir = os.path.join(os.getcwd(),\"export_images\")\n", | |
| " image = Image.open(image_path)\n", | |
| " image.load()\n", | |
| "\n", | |
| " rgb_image = Image.new('RGB', image.size, (255,255,255))\n", | |
| " rgb_image.paste(image, None)\n", | |
| "\n", | |
| " if not os.path.exists(export_dir):\n", | |
| " os.makedirs(export_dir)\n", | |
| " rgb_image.save(f'{export_dir}/{os.path.basename(image_path)}',\"PNG\")" | |
| ], | |
| "metadata": { | |
| "id": "gd_3KBmLpsd8" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "import os\n", | |
| "print(os.getcwd())" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "cR_WCFWamG-A", | |
| "outputId": "7ac90b59-55ed-4f7e-f7fb-eaf8afbd23f2" | |
| }, | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "/content\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [], | |
| "metadata": { | |
| "id": "QZD5Q2ASyKEv" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "# Convert images" | |
| ], | |
| "metadata": { | |
| "id": "uuUgTyoaqGr3" | |
| } | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "Convert one image" | |
| ], | |
| "metadata": { | |
| "id": "kCLZHXlTyLVt" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "change_colorspace_to_rgb(\"test.png\")" | |
| ], | |
| "metadata": { | |
| "id": "dOBcnmQXoIvu" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "Convert a folder" | |
| ], | |
| "metadata": { | |
| "id": "v6hmlp5_DpaN" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "import os\n", | |
| "\n", | |
| "dir = f'{os.getcwd()}/images' # Change to the folder with your images\n", | |
| "print(f'Converting folder: {dir}')\n", | |
| "\n", | |
| "files = os.listdir(dir)\n", | |
| "\n", | |
| "for file in files:\n", | |
| " full_path = os.path.join(dir, file)\n", | |
| " print(full_path)\n", | |
| " change_colorspace_to_rgb(full_path)\n" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "CAqY-e6bDrSF", | |
| "outputId": "f83cf910-a5ea-4e01-deb6-bc2ffcbdb6d6" | |
| }, | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "Converting folder: /content/images\n", | |
| "/content/images/test.png\n" | |
| ] | |
| } | |
| ] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment