from PIL import Image | |
image = Image.open('mask2.png') | |
def toBlackWhiteMask(input_pil): | |
if input_pil.mode != 'RGBA': | |
input_pil = input_pil.convert('RGBA') | |
new_image = Image.new('RGBA', input_pil.size, (255, 255, 255, 255)) | |
input_data = input_pil.getdata() | |
new_data = [] | |
for item in input_data: | |
if item[3] == 0: | |
new_data.append((0, 0, 0, 255)) | |
else: | |
new_data.append((255, 255, 255, 255)) | |
new_image.putdata(new_data) | |
return new_image | |
transform_image(image).save('mask2_nice.png') |