Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to replace gr.ImageEditor uploaded user image and still keep image editing features #9889

Closed
FurkanGozukara opened this issue Nov 1, 2024 · 1 comment · Fixed by #10357
Assignees
Labels
bug Something isn't working 🖼️ ImageEditor

Comments

@FurkanGozukara
Copy link

FurkanGozukara commented Nov 1, 2024

Describe the bug

gradio==5.4.0

I want to auto resize / crop user uploaded image and replace processed image inside in gr.ImageEditor

Here the code I use

So after image edited the image editing features disappears

Here what I mean

imgs = gr.ImageEditor(sources='upload', type="pil", label='Human. Mask with pen or use auto-masking', interactive=True)

    def process_editor_image(image_dict, enable_processing):
        if not enable_processing or image_dict is None:
            return image_dict
        if isinstance(image_dict, dict) and 'background' in image_dict:
            image_dict['background'] = process_image_to_768x1024(image_dict['background'])
        return image_dict

    def process_single_image(image, enable_processing):
        if not enable_processing or image is None:
            return image
        return process_image_to_768x1024(image)

    # Add image processing event handlers
    imgs.upload(
        fn=process_editor_image,
        inputs=[imgs, auto_process],
        outputs=imgs,
    )


def process_image_to_768x1024(img):
    if not isinstance(img, Image.Image):
        return img
        
    # Create a new white background image
    target_width, target_height = 768, 1024
    new_img = Image.new('RGB', (target_width, target_height), 'white')
    
    # Calculate aspect ratios
    aspect_ratio = img.width / img.height
    target_aspect = target_width / target_height
    
    if aspect_ratio > target_aspect:
        # Image is wider than target
        new_width = target_width
        new_height = int(target_width / aspect_ratio)
        resize_img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
        paste_y = (target_height - new_height) // 2
        new_img.paste(resize_img, (0, paste_y))
    else:
        # Image is taller than target
        new_height = target_height
        new_width = int(target_height * aspect_ratio)
        resize_img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
        paste_x = (target_width - new_width) // 2
        new_img.paste(resize_img, (paste_x, 0))
    
    return new_img

def process_uploaded_image(img, enable_processing):
    if not enable_processing or img is None:
        return img
    if isinstance(img, dict):  # For ImageEditor
        if img.get('background'):
            img['background'] = process_image_to_768x1024(img['background'])
        return img
    return process_image_to_768x1024(img)

image

image

@FurkanGozukara FurkanGozukara added the bug Something isn't working label Nov 1, 2024
@abidlabs
Copy link
Member

abidlabs commented Nov 5, 2024

I believe this is the same underlying issue as #9694, cc @hannahblair

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working 🖼️ ImageEditor
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants