add example component for color picker (#1779)

This commit is contained in:
pngwn 2022-07-13 16:35:43 +01:00 committed by GitHub
parent 260fbb3564
commit c323aac274
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 8 deletions

BIN
demo/color_picker/lion.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

@ -1,12 +1,11 @@
import gradio as gr
import numpy as np
import os
from PIL import Image, ImageColor
def change_color(icon, color):
"""
Function that given an icon in .png format changes its color
Args:
@ -19,24 +18,27 @@ def change_color(icon, color):
img = img.convert("RGBA")
image_np = np.array(icon)
_, _, _, alpha = image_np.T
mask = (alpha > 0)
mask = alpha > 0
image_np[..., :-1][mask.T] = ImageColor.getcolor(color, "RGB")
edited_image = Image.fromarray(image_np)
return edited_image
inputs = [
gr.Image(label="icon", type="pil", image_mode="RGBA"),
gr.ColorPicker(label="color")
]
gr.Image(label="icon", type="pil", image_mode="RGBA"),
gr.ColorPicker(label="color"),
]
outputs = gr.Image(label="colored icon")
demo = gr.Interface(
fn=change_color,
inputs=inputs,
outputs=outputs,
examples=[
[os.path.join(os.path.dirname(__file__), "lion.jpg"), "#ff0000"],
[os.path.join(os.path.dirname(__file__), "lion.jpg"), "#0000FF"],
],
)
if __name__ == "__main__":
demo.launch()

View File

@ -0,0 +1,8 @@
<script lang="ts">
export let value: string;
</script>
<div
class="w-10 h-10 border dark:border-slate-300"
style="background-color: {value}"
/>

View File

@ -11,6 +11,7 @@ import ExampleVideo from "./ExampleComponents/Video.svelte";
import ExampleFile from "./ExampleComponents/File.svelte";
import ExampleDataframe from "./ExampleComponents/Dataframe.svelte";
import ExampleModel3D from "./ExampleComponents/Model3D.svelte";
import ExampleColorPicker from "./ExampleComponents/ColorPicker.svelte";
export const component_map = {
dropdown: ExampleDropdown,
@ -25,5 +26,6 @@ export const component_map = {
video: ExampleVideo,
file: ExampleFile,
dataframe: ExampleDataframe,
model3d: ExampleModel3D
model3d: ExampleModel3D,
colorpicker: ExampleColorPicker
};