gradio icon indicating copy to clipboard operation
gradio copied to clipboard

Center an output `gr.Image` when the width and height and are set

Open apolinario opened this issue 1 year ago • 8 comments

Is your feature request related to a problem? Please describe.
Sometimes I may want to output images with a particular width or width/height combination. If not, the image can get way too big, even bigger than the actual image resolution. Currently I have to do it via CSS css="#output_image{width: 256px}" as in here

Describe the solution you'd like
gr.Image(shape=[256,256]) when the component is used as an output could be used to determine the output width and height (alternatively a new parameter could be used)

apolinario avatar Jul 26 '22 09:07 apolinario

And example when no CSS is used. The output should be a 256x256 image but this happens: image

apolinario avatar Jul 26 '22 10:07 apolinario

Hi @apolinario, thanks for creating the issue! Just to clarify, is this the same issue as in #1561 or are you getting at something else?

abidlabs avatar Jul 26 '22 22:07 abidlabs

I did see that issue, but to me it seemed like that issue was still related to the image as an input field - while I'm getting at it as an output field - but maybe the problem is the same all across

@osanseviero has also mentioned the possibility that the output image inherits the input image (or the real output dimensions) internally - which I think would make sense as well

apolinario avatar Jul 28 '22 14:07 apolinario

We do have a way to do this with the Python API using the .style() method on the output image. Something like this:

import gradio as gr

gr.Interface(
    lambda x:x,
    inputs=gr.Image(),
    outputs=gr.Image(shape=[256,256], elem_id="output_image").style(width=256, height=256),
    title="ddpm-celebahq-256 diffusion - 🧨 diffusers library",
    description="This Spaces contains an unconditional diffusion process for the <a href=\"https://huggingface.co/google/ddpm-celebahq-256\">ddpm-celebahq-256</a> face generator model by <a href=\"https://huggingface.co/google\">Google</a> using the <a href=\"https://github.com/huggingface/diffusers\">diffusers library</a>. You can try the diffusion process not only with the default <code>ddpm</code> scheduler but also with <code>ddim</code> and <code>pndm</code>, showcasing the modularity of the library. <a href=\"https://github.com/huggingface/diffusers/tree/main/src/diffusers/schedulers\">Learn more about schedulers here.</a>",
).launch()

But the output image alignment is to the left, which looks weird:

image

Would be better if it was centered. Let me rename this issue to reflect that

abidlabs avatar Feb 23 '23 14:02 abidlabs

This is still an issue even though style has been deprecated. Here's an example that shows the issue:

import gradio as gr

gr.Interface(
    lambda x:x,
    inputs=gr.Image(),
    outputs=gr.Image(shape=[256,256], width=256, height=256, elem_id="output_image"),
    title="ddpm-celebahq-256 diffusion - 🧨 diffusers library",
    description="This Spaces contains an unconditional diffusion process for the <a href=\"https://huggingface.co/google/ddpm-celebahq-256\">ddpm-celebahq-256</a> face generator model by <a href=\"https://huggingface.co/google\">Google</a> using the <a href=\"https://github.com/huggingface/diffusers\">diffusers library</a>. You can try the diffusion process not only with the default <code>ddpm</code> scheduler but also with <code>ddim</code> and <code>pndm</code>, showcasing the modularity of the library. <a href=\"https://github.com/huggingface/diffusers/tree/main/src/diffusers/schedulers\">Learn more about schedulers here.</a>",
).launch()

abidlabs avatar Sep 29 '23 17:09 abidlabs

I've found a quick solution, for anyone with the same issue. Following the elem_id attribute, make sure you add a style element that links to that ID.

gr.HTML("""<div id='output_image' style='display:block; margin-left: auto; margin-right: auto></div>""")
result = gr.Image(label="output", show_label=True, witdh=768, height=768, elem_id="output_image")

The CSS necessary in the div are all necessary in order to align the image properly. I hope this helps!

moebius2033 avatar Oct 02 '23 03:10 moebius2033

looking into this. reference to internal discussion: https://huggingface.slack.com/archives/C03JMRM52C9/p1707179711883409?thread_ts=1707100387.003039&cid=C03JMRM52C9

aliabid94 avatar Feb 06 '24 00:02 aliabid94