import gradio as gr from PIL import Image def mergeimages(i1,i2,i3,i4,i5,i6,i7,i8,i9):…
- User
import gradio as gr from PIL import Image
def _merge_images(i1,i2,i3,i4,i5,i6,i7,i8,i9): merge_images([i1,i2,i3,i4,i5,i6,i7,i8,i9])
def merge_images(images): image_size = (16, 16) # Define the size for each image empty_block = Image.new("RGB", (16, 16), (255, 255, 255))
# Create a blank canvas for the final 3x3 grid grid_image = Image.new("RGB", (16*3, 16*3), (255, 255, 255)) for idx, img in enumerate(images): row, col = divmod(idx, 3) # Resize each image to fit 256x256, or use the empty block directly if img == None: img = empty_block # Paste the image into the grid grid_image.paste(img, (col * image_size[0], row * image_size[1])) return grid_imagewith gr.Blocks() as iface: images = [] with gr.Column(): for i in range(3): with gr.Row(): for i in range(3): images.append(gr.Image(type="pil"))
iface = gr.Interface( fn=_merge_images, inputs=[gr.Image(type="pil", label=f"Image {i+1}") for i in range(9)], outputs="image", title="3x3 Image Grid", description="Upload up to 9 images to merge them into a 3x3 grid. Missing images will be replaced by a 16x16 white block." ) with gr.Column(): with gr.Row(): result = gr.Image() button = gr.Button("Generate") button.click(_merge_images, inputs=images, outputs=result)iface.launch(debug=True)
fix errors
- Assistant
- User
it just stuck at loading
- Assistant
- User
how to disable interpolation so i see normal 16x16 and not blurry
- Assistant
To prevent interpolation during image resizing, which might lead to blurriness, you can use the
Image.NEARESTresampling filter from the Pillow library for resizing. This filter uses the nearest neighbor approach, which doesn't interpolate between pixels and keeps the image sharp, especially at lower resolutions.Here's how you can adjust your code to use
Image.NEARESTwhen resizing:import gradio as gr from PIL import Image def merge_images(i1, i2, i3, i4, i5, i6, i7, i8, i9): images = [i1, i2, i3, i4, i5, i6, i7, i8, i9] image_size = (16, 16) # Smaller size for testing with nearest neighbor empty_block = Image.new("RGB", image_size, (255, 255, 255)) # Create a blank canvas for the final 3x3 grid grid_image = Image.new("RGB", (image_size[0] * 3, image_size[1] * 3), (255, 255, 255)) for idx, img in enumerate(images): row, col = divmod(idx, 3) # Use nearest neighbor resizing to prevent blurring if img is None: img = empty_block else: img = img.resize(image_size, Image.NEAREST) # Paste the image into the grid grid_image.paste(img, (col * image_size[0], row * image_size[1])) return grid_image with gr.Blocks() as iface: images = [gr.Image(type="pil", label=f"Image {i+1}") for i in range(9)] with gr.Row(): generate_button = gr.Button("Generate") with gr.Row(): result = gr.Image() # Connect the button with the merge_images function generate_button.click(merge_images, inputs=images, outputs=result) iface.launch(debug=True) - User
not resising problem but gradio upscales image in web browser
- Assistant