Created
March 8, 2024 03:14
-
-
Save jcheng5/c90e665a56e951bf86f9eab4eed1b939 to your computer and use it in GitHub Desktop.
Color blindness helper in Shiny Express
This file contains 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
# Live demo at https://shinylive.io/py/app/#code=NobwRAdghgtgpmAXGKAHVA6VBPMAaMAYwHsIAXOcpMAMwCdiYACABQEkAZJgSxlWLpkmbGFADmcADoRe-QUwgBXPtiZQAzgtTTZAoSQA2A9dnXTp9Rk3UALbhFW75dOFEJluANzh4mLgI4WDMy29tgYcAAeqC7qmk5C9qiKZL4uEAAmcHS+itzmStwYSSkA+jTcBnAAFJK0lVL4THUi4nBMFVV1AJTSecUQyWSl6gbcWXS1YDaKjb51ABKz1nY0ZHW+AAy+AEwArHtbvRAFAALpE1hG65lwNCvca3AZ1d2I0kyfTADETBzEUAyTDINnavDaHy+4IkTAAvMJRBIMMRUJRqgFqiUyBhOjVut1gJsALrAOoZKBkNAUmx1IndDAkCDeQTVADkACUAOIAIVZxy+zQgAt+AGFSMyhCCwYj2mRiAplDg1HQ6FBVDQBExRDJkgYKdxSJDPtC4KVBnCtBgoCq1ZiZfyvkaeDLSjZ1J4LYMrTbsNVgIZjKYMHQxAAjUpy13u6qobiROAGQlEgD0+0OTFj8cTAEYU2nfJmE8AdnmDt0OprCwYeEKTWbMLEbGgagBabO+ADM3TpBWFTAAyqshDMpEKoS63Z5gIgtkSLXa2lGpzOmMSmABqJiYwYpDAj15MZNMNP4pgAUiY2adTtF4uyQgW-YAakxQ24ANbA+Vc7lOush0NPUwa1VV9f1iCMOgTHUPd3QjYhSgAmM4yLYkCxQnMiXQrNizpCs6AzDCa2dRdJx7MdPhvJh2TgWxmzUTJrEIKAqiYRRUC-Y8Dj-F0AMQ2im1RIF4QXCRELDYMBObUTTS9OjUXLAAqLi9npDQyGwVFqi9PJyAADgdSiKJ+JgxSZe8FRUZVQNfD9OPYLhWgkHjF2IFIhgtJy4BxYIQNtf8w34+TnmOJ0XDIRQ6FrF03LIIZpDAABfIkgA | |
from PIL import Image | |
import numpy as np | |
import colorsys | |
from shiny import reactive, req | |
from shiny.express import input, render, ui | |
ui.input_file("file", "Image file") | |
ui.input_slider("hue", "Hue shift", 0, 255, 0) | |
@render.plot | |
def shifted(): | |
# Load the image | |
image = Image.open(req(input.file())[0]["datapath"]).convert('RGB') | |
# Convert the image to numpy array for manipulation | |
image_np = np.array(image) | |
image_hsv = np.array([colorsys.rgb_to_hsv(pixel[0]/255, pixel[1]/255, pixel[2]/255) for pixel in image_np.reshape(-1, 3)]) | |
# Shift hue | |
image_hsv[:, 0] = (image_hsv[:, 0] + (input.hue() / 255)) % 1 | |
# Convert HSV back to RGB | |
image_rgb = np.array([colorsys.hsv_to_rgb(pixel[0], pixel[1], pixel[2]) for pixel in image_hsv]) | |
# Reshape and scale up to 255 | |
image_rgb_reshaped = (image_rgb.reshape(image_np.shape) * 255).astype(np.uint8) | |
# Convert numpy array back to PIL Image | |
image_output = Image.fromarray(image_rgb_reshaped) | |
return image_output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment