File size: 1,922 Bytes
ed531c8
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: color_picker"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio Pillow"]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["# Downloading files from the demo repo\n", "import os\n", "!wget -q https://github.com/gradio-app/gradio/raw/main/demo/color_picker/rabbit.png"]}, {"cell_type": "code", "execution_count": null, "id": "44380577570523278879349135829904343037", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import numpy as np\n", "from PIL import Image, ImageColor\n", "\n", "def change_color(icon, color):\n", "\n", "    \"\"\"\n", "    Function that given an icon in .png format changes its color\n", "    Args:\n", "        icon: Icon whose color needs to be changed.\n", "        color: Chosen color with which to edit the input icon.\n", "    Returns:\n", "        edited_image: Edited icon.\n", "    \"\"\"\n", "    img = icon.convert(\"LA\")\n", "    img = img.convert(\"RGBA\")\n", "    image_np = np.array(icon)\n", "    _, _, _, alpha = image_np.T\n", "    mask = alpha > 0\n", "    image_np[..., :-1][mask.T] = ImageColor.getcolor(color, \"RGB\")\n", "    edited_image = Image.fromarray(image_np)\n", "    return edited_image\n", "\n", "inputs = [\n", "    gr.Image(label=\"icon\", type=\"pil\", image_mode=\"RGBA\"),\n", "    gr.ColorPicker(label=\"color\"),\n", "]\n", "outputs = gr.Image(label=\"colored icon\")\n", "\n", "demo = gr.Interface(\n", "    fn=change_color,\n", "    inputs=inputs,\n", "    outputs=outputs\n", ")\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}