- p = os.popen("convert -depth 8 -size %dx%d rgba:- %s" % (width,width,fname), "w")
- assert len(block) == width
- for line in block:
- assert len(line) == width
- for r, g, b, a in line:
- if orangebackground:
- # Composite on to orange.
- r = int(round((r * a + 255 * (255-a)) / 255.0))
- g = int(round((g * a + 128 * (255-a)) / 255.0))
- b = int(round((b * a + 0 * (255-a)) / 255.0))
- a = 255
- p.write("%c%c%c%c" % (r,g,b,a))
- p.close()
+ with open(fname, "w") as f:
+ f.write(("P7\nWIDTH %d\nHEIGHT %d\nDEPTH 4\nMAXVAL 255\n" +
+ "TUPLTYPE RGB_ALPHA\nENDHDR\n") % (width, width))
+ assert len(block) == width
+ for line in block:
+ assert len(line) == width
+ for r, g, b, a in line:
+ if orangebackground:
+ # Composite on to orange.
+ r = int(round((r * a + 255 * (255-a)) / 255.0))
+ g = int(round((g * a + 128 * (255-a)) / 255.0))
+ b = int(round((b * a + 0 * (255-a)) / 255.0))
+ a = 255
+ f.write("%c%c%c%c" % (r,g,b,a))