facexlib icon indicating copy to clipboard operation
facexlib copied to clipboard

Does paste_faces_to_input_image support PNG transparency?

Open andreafalzetti opened this issue 2 years ago • 7 comments

I am using this image in https://github.com/TencentARC/GFPGAN and the background turns black after calling paste_faces_to_input_image. Is there a way to implement support for PNG transparent background?

c30b14894f8c4767e667e393f2ca2678_4f4de8bf8817aede213d2412aa2b53f4

Thanks

andreafalzetti avatar Jan 22 '22 19:01 andreafalzetti

Linked issue:

  • https://github.com/TencentARC/GFPGAN/issues/151

woctezuma avatar Jan 22 '22 19:01 woctezuma

@woctezuma do you have any idea if this is even possible?

andreafalzetti avatar Jan 27 '22 07:01 andreafalzetti

I have not checked the source code, but this should be possible a priori: in the worst case, it should be a matter of keeping a mask of the transparent area in the input, and using it in the output.

References to other repositories (with commits) and documentation:

  • https://github.com/lucidrains/stylegan2-pytorch/issues/82#issuecomment-650678578
  • https://pillow.readthedocs.io/en/stable/handbook/concepts.html#modes

woctezuma avatar Jan 27 '22 07:01 woctezuma

@woctezuma i tried looking at the code but it's not easy to understand without much context and experience in this field - I'm looking for someone with knowledge of the code base to provide some support either explaining to me how to implement this or to be paid to implement it. Would you be available for a chat?

andreafalzetti avatar Feb 10 '22 18:02 andreafalzetti

either explaining to me how to implement this

My suggestion would be to look for solutions with the keywords "transparency", "mask" and "merge", similar to:

  • https://stackoverflow.com/questions/5324647/

What you want to do is merge a part of the output (improved faces) into the input (original image with transparent background).

or to be paid to implement it

Sorry, this could require too much time, as I am not familiar with the code base to be able to quickly help on this matter. The solution could be very simple, or involve a bit of debugging (and thus take more time). I cannot judge the task a priori.

For info, I have only contributed in a minor way to this project by replacing a function with a call to a built-in function. https://github.com/xinntao/facexlib/commit/efeacb3c01d654ce0fe25237ea1b2abfcbcf343f

woctezuma avatar Feb 10 '22 19:02 woctezuma

Thank's to chat-gpt. you can restore bg from original image. i think this should be added to the Model args

`from PIL import Image def apply_transparency(source_image_path, target_image_path): # Load the source image source_img = Image.open(source_image_path)

# Load the target image
target_img = Image.open(target_image_path).convert("RGBA")

# Get the pixel values as a 2D array for both images
source_pixels = source_img.load()
target_pixels = target_img.load()

# Loop through all the pixels in the source image
for x in range(source_img.width):
    for y in range(source_img.height):
        
        # Get the pixel value from the source image
        source_pixel = source_pixels[x, y]
        
        # Check if the pixel is transparent
        if len(source_pixel) == 4 and source_pixel[3] == 0:
            # The pixel is transparent, apply the transparency to the target image
            target_pixels[x, y] = (0, 0, 0, 0)

# Save the modified target image
target_img.save('output.png')

apply_transparency("./org.png", "./res.png")`

In this example, the apply_transparency() function takes two arguments - the path to the source image file that contains the transparency data, and the path to the target image file that you want to apply the transparency to. The function loads both images using the Image.open() method, and gets the pixel values as 2D arrays using the load() method.

Then, the function loops through all the pixels in the source image, checks if each pixel is transparent, and if it is, applies the transparency to the corresponding pixel in the target image by setting its RGBA value to (0, 0, 0, 0) - i.e., fully transparent.

Finally, the function saves the modified target image to a new file named 'output.png'. You can modify this code to suit your specific needs - for example, you can pass the output filename as an argument to the function, or modify the transparency value to make it less opaque, etc.

ghitrif avatar May 14 '23 17:05 ghitrif