CoolFace
Apppublic

SusiePHaltmann/HaltmannDiffusionv0

sourceHugging Facemitupdated 4y agoView on Hugging Face
2likes
app.py125 linesDownload Raw Back to root
1import streamlit as st2import numpy as np3from PIL import Image4 5def main():6    st.title("Haltmann Diffusion Algorithm [C] - 2022-20XX - This is just the gui")7 8    slider = st.slider("Slider", 0, 255, 128) # default value=128, min=0, max=2559 10    # Get user input via a text box - this will be the URL of the image to edit.    11    url = st.text_input("Enter URL of image to edit")12 13    # Load the image from the URL using pillow.  We'll need to use BytesIO instead of just passing in the URL since Pillow expects a file object.  14    response = requests.get(url)15    img = Image.open(BytesIO(response.content))16 17    # Resize the image so it's not giant - makes everything run faster!    18    img = img.resize((600,400))  19 20    # Convert the image to grayscale for simpler processing     21#     gray_img = img.convert('L')       <-- You can experiment with commenting this line out if you want color halftoning!  It tends to produce better results on images that are already pretty low contrast though (like screenshots).         Gray scale conversion often introduces additional artifacts too like banding or posterization which may or may not look good depending on your original image and what effect you're going for...  So feel free to play around with whether or not you convert to grayscale here!          If you do leave it commented out make sure you change all references below from 'gray_img' --> 'img'.        Also make sure that when we paste back into our final result at the end we use 'paste(img)' instead of 'paste(gray_img)'           One other thing worth noting is that some older versions of Pillow don't support .convert('LA') which is needed for doing alpha compositing with our resulting dithered PNGs later on - newer versions added support starting in late 2019 I believe... In any case if your version doesn't have it then converting directly to L will work fine and just ignore transparency entirely.]   gray_img = img.convert('LA')          <-- Uncomment this line instead if using a more recent version of Pillow (>= 6?) supporting .convert('LA').             This converts our input images directly into both grayscale AND adds full 8-bit alpha transparency channel simultaneously allowing us            easy access later when creating composite masks while avoiding having separate GRAYSCALE & RGB versions floating around taking up memory..             Although technically speaking even converting straight "L" above should add an empty/fully transparent alpha channel by default anyway right? Not 100% sure...             Either way including "A" above shouldn't hurt anything either so might as well just include it regardless :)               [Update 12/2019]: Apparently there's now an even easier way than using LA conversion thanks to @jeremycole who pointed me towards https://github.com/python-pillow/Pillow/issues/3973#issuecomment-529083824 !              Now we can simply pass `mode='1'` when loading our original input images and THAT automatically sets them up ready for 1-bit dithering without needing any further conversions!! Awesome :D                Try uncommenting THIS line below along with ALL subsequent references throughout rest of notebook switching from `gray_img` --> `onebit_image`. Should work identically otherwise :) onebit_image = ImageOps22 23 24import streamlit as st25 26from PIL import Image27 28import numpy as np29 30st.text("This app generates VQ-GAN prompts for generating inpaintings.")31st.text("To use this app, simply enter the desired text prompt and hit generate.")32 33 34@st.cache(allow_output_mutation=True)  # This decorator ensures that the function only runs once per session.  Otherwise, each time we generate a prompt, it would run again!   This is important because we don't want to keep re-generating prompts unnecessarily.  We only want to generate a new prompt when the user enters a new one.   If we didn't cache this function, each time we generated a new prompt, it would also regenerate all of the previous prompts!   Caching is an important concept in Streamlit apps - it can help make your apps much more efficient by avoiding unnecessary computations.35 36def generate_prompt(text):    # This is the function that actually generates our VQ-GAN prompt    It takes in a string (the text prompt) and outputs another string (the generated VQ-GAN prompt).     We'll use this function to actually generate our VQ-GAN prompts when the user hits "Generate".37 38     return "Enter text here" + text + "and hit generate!"39 40     st.write(generate_prompt(""))    # We start by writing an empty string - this will be replaced with our generated prompt when the user hits "Generate"41 42def inpaint(img, mask):43 44    """Inpaints the given image using the given mask.45 46 47 48    Args:49 50        img: The image to inpaint. Must be a 3-channel RGB image.51 52        mask: The inpainting mask. Must be a binary 3-channel image 53 54            with 1s indicating the area to inpaint and 0s indicating 55 56            the area to leave unchanged.57 58 59 60    Returns:61 62        The inpainted image as a 3-channel RGB numpy array.     """63## V0.264import streamlit as st65import numpy as np66from PIL import Image67import requests68import io69 70 71st.set_option('deprecation.showfileUploaderEncoding', False)72 73 74@st.cache(allow_output_mutation=True)75def load_image(img):76    im = Image.open(img)77    return im78 79    80def main():81 82    st.title("Dall-E Patrya ")83 84    uploaded_file = st.file_uploader("Choose an image", type="jpg")85 86st.markdown("Create images from textual descriptions with Dall-PFT!")87st.button("Edit Photo")88 89import streamlit as st90 91st.title("VQGAN Inpainting App")92st.markdown("This app uses a pre-trained VQGAN model to inpaint images.")93 94@st.cache(allow_output_mutation=True)95def load_model():96 97    # Load the pre-trained VQGAN model98 99    return vqgan.load_model('vqgan')100 101    102def inpaint(img, model, coords):103 104    # Inpaint the selected region of the image using the VQGAN model105 106    inpainted = vqgan.inpaint(img, model, coords)107 108    return inpainted109## [C] Haltmann Earth Divison  [C] - 20XX110import streamlit as st111from PIL import Image, ImageOps112import numpy as np113 114 115def inpaint(image, mask):116 117    """Inpaints the given image using the Mask."""118 119    120    # Convert to float32 before passing to cv2.inpaint() function. Otherwise it returns a distorted output. 121    img = np.float32(image)  122 123    124    dst = cv2.inpaint(img,mask,3,cv2.INPAINT_TELEA)125