1

I have a png image with transparency on it and I would like to change its opacity keeping the transparency of the pixel just add a percentage or something. I tried using putalpha but it just destroys the transparency in the image.

What I want is something like the opacity property in css.

Thank you.

img{opacity:.2}
<img src="http://i.imgur.com/2zGGyYB.png"/>
shadownrun
  • 364
  • 6
  • 15

2 Answers2

0

found a way to do it.

image=Image.open("star_blue.png")
opacity=0.5
bands=list(self.image.split())
if len(bands)==4:
    bands[3]=bands[3].point(lambda x:x*opacity)
    new_image=Image.merge(image.mode,bands)

found the code here

thanks mmgp

Community
  • 1
  • 1
shadownrun
  • 364
  • 6
  • 15
0

png image is transparent where all channels are zero. The 4'th channel is the opacity. so:

import matplotlib.pyplot as plt

im = plt.imread("http://i.imgur.com/2zGGyYB.png")
plt.imshow(im)
im[:,:,3]=np.where(im[:,:,2]>0,0.2,0)
plt.imshow(im)
Naomi Fridman
  • 2,095
  • 2
  • 25
  • 36