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

Save masked image as transparent PNG #18

Closed
cduk opened this issue Jun 2, 2023 · 2 comments
Closed

Save masked image as transparent PNG #18

cduk opened this issue Jun 2, 2023 · 2 comments

Comments

@cduk
Copy link

cduk commented Jun 2, 2023

This tool would be great for selecting parts of the image and then saving as a PNG with the unmasked parts treated as transparent. Would it be possible to add this transparency feature?

@Uminosachi
Copy link
Owner

Uminosachi commented Jun 3, 2023

Supported that feature in v1.4.1. It can be accessed by clicking the Get mask as alpha of image button under the Mask only tab. The PNG image will be saved in the folder corresponding to the current date within the outputs directory.

@cduk
Copy link
Author

cduk commented Jun 22, 2023

I use the following script to create the transparent PNG. It's not as convenient as having the feature within inpaint-anything, but it works:

#!/usr/bin/python3
import sys

total = len(sys.argv)
cmdargs = str(sys.argv)

if total!=3:
print ("Usage: %s image mask " % sys.argv[0])
sys.exit()

from PIL import Image, ImageOps

load images

#img_org = Image.open('target.png')
#img_mask = Image.open('mask.png')
img_org = Image.open(sys.argv[1])
img_mask = Image.open(sys.argv[2])

convert images

img_org = img_org.convert('RGBA') # or 'RGBA'
img_mask = img_mask.convert('L') # grayscale
img_mask = ImageOps.invert(img_mask)

add alpha channel

img_org.putalpha(img_mask)

save as png which keeps alpha channel

img_org.save('output.png')

This issue was closed.
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

2 participants