1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 09:27:59 +00:00

mkicon.py: Write output files as binary.

In Python 3, open(path, "w") defaults to text files encoded using some
default encoding, which of course isn't what we want for image data.
Use open(path, "wb") instead, and adjust the write calls to use forms
that work with Python 3.  bytes() and bytearray() are available as of
Python 2.6.

(This could be simplified to e.g. b"%c%c%c%c" % (r,g,b,a), and similarly
avoiding the manual .encode call; but %-formatting on bytes requires
Python 3.5, and I thought it might be better to be compatible with older
versions of Python 3.)
This commit is contained in:
Colin Watson 2019-08-30 13:41:23 +01:00 committed by Simon Tatham
parent b810de5f3a
commit 9cb587c43a

View File

@ -901,9 +901,9 @@ def testrun(func, fname):
for canvas in canvases:
minx, miny, maxx, maxy = bbox(canvas)
block.extend(render(canvas, minx-2, miny-2, minx-2+wid, maxy+2))
with open(fname, "w") as f:
f.write(("P7\nWIDTH %d\nHEIGHT %d\nDEPTH 3\nMAXVAL 255\n" +
"TUPLTYPE RGB\nENDHDR\n") % (wid, ht))
with open(fname, "wb") as f:
f.write((("P7\nWIDTH %d\nHEIGHT %d\nDEPTH 3\nMAXVAL 255\n" +
"TUPLTYPE RGB\nENDHDR\n") % (wid, ht)).encode('ASCII'))
assert len(block) == ht
for line in block:
assert len(line) == wid
@ -912,7 +912,7 @@ def testrun(func, fname):
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))
f.write("%c%c%c" % (r,g,b))
f.write(bytes(bytearray([r, g, b])))
def drawicon(func, width, fname, orangebackground = 0):
canvas = func(width / 32.0)
@ -921,9 +921,10 @@ def drawicon(func, width, fname, orangebackground = 0):
assert minx >= 0 and miny >= 0 and maxx <= width and maxy <= width
block = render(canvas, 0, 0, width, width)
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))
with open(fname, "wb") as f:
f.write((("P7\nWIDTH %d\nHEIGHT %d\nDEPTH 4\nMAXVAL 255\n" +
"TUPLTYPE RGB_ALPHA\nENDHDR\n") %
(width, width)).encode('ASCII'))
assert len(block) == width
for line in block:
assert len(line) == width
@ -934,7 +935,7 @@ def drawicon(func, width, fname, orangebackground = 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))
f.write(bytes(bytearray([r, g, b, a])))
args = sys.argv[1:]