jcjohnson / neural-style

Torch implementation of neural style algorithm
MIT License
18.3k stars 2.71k forks source link

Styling a Transparent Image #484

Open spot92 opened 4 years ago

spot92 commented 4 years ago

Is anyone familiar with a way to style a transparent image? I've tried passing the .png content and doing a .png output, but the transparent background gets styled.

hikmet-demir commented 4 years ago

@spot92

There is a easy solution for it @spot92 , I don't think you can prevent transparent places of images to get styled but you can make them re-transparent again.

Every image colorful image have 3 or 4 image channel ( RGB and A) A is the alpha channel and determines opacity of the pixels. What you need to is before feeding style image to network you save the Alpha channel of the image in a numpy array and after image being style you added back to image.

Example code that I use for some other implementation

 t_data = np.array(target)
 target_alpha = t_data[:,:,3:]

#deeplearning
#deeplearning

out_img_nparr = np.array(out_img)
final_hopefully = np.concatenate((out_img_nparr, target_alpha), axis=2)
image2 = Image.fromarray(final_hopefully)
image2.save(output_image_path)

I'm basically extracting the alpha channel and adding it back to image