MCPcopy Create free account
hub / github.com/Apress/python-for-matlab-development / png

Function png

code/matlab_py/make_transparent.py:33–49  ·  view source on GitHub ↗
(in_png, out_png)

Source from the content-addressed store, hash-verified

31import sys
32from PIL import Image
33def png(in_png, out_png):
34 try:
35 rgba = Image.open(in_png).convert('RGBA')
36 except:
37 err = sys.exc_info()[1]
38 print(f'make_transparent.save({in_png}) error: {err}')
39 return
40 z = np.array(rgba.getdata())
41 white_pixel = (z[:,0]==255) * (z[:,1]==255) * (z[:,2]==255)
42 z[white_pixel,3] = 0 # set alpha to zero = transparent
43 rgba.putdata([tuple(_) for _ in z])
44 try:
45 rgba.save(out_png, 'PNG')
46 except:
47 err = sys.exc_info()[1]
48 print(f'make_transparent.save({in_png}) error: {err}')
49 return

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected