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

Does paste_faces_to_input_image support PNG transparency? #13

Open
andreafalzetti opened this issue Jan 22, 2022 · 7 comments
Open

Does paste_faces_to_input_image support PNG transparency? #13

andreafalzetti opened this issue Jan 22, 2022 · 7 comments

Comments

@andreafalzetti
Copy link

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

@woctezuma
Copy link
Contributor

@andreafalzetti
Copy link
Author

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

@woctezuma
Copy link
Contributor

woctezuma commented Jan 27, 2022

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:

@andreafalzetti
Copy link
Author

@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?

@woctezuma
Copy link
Contributor

woctezuma commented Feb 10, 2022

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:

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. efeacb3

@ghitrif
Copy link

ghitrif commented May 14, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@woctezuma @andreafalzetti @ghitrif and others