On 19/05/2018 20:47, Dennis Lee Bieber wrote:
On Sat, 19 May 2018 13:28:41 +0100, bartc <b...@freeuk.com> declaimed the
following:
Out of interest, how would Python handle the headers for binary file
formats P4, P5, P6? I'd have a go but I don't want to waste half the day
trying to get past the language.
Based upon http://netpbm.sourceforge.net/doc/ppm.html
P6 1024 768 255
and
P6
# random comment
1024
768
# another random comment
255
are both valid headers.
The comments and examples here:
https://en.wikipedia.org/wiki/Netpbm_format, and all actual ppm files
I've come across, suggest the 3 parts of the header (2 parts for P1/P4)
are on separate lines. That is, separated by newlines. The comments are
a small detail that is not hard to deal with.
I think if ppm readers expect the 2-3 line format then generators will
be less tempted to either stick everything on one line or stretch it
across half a dozen. The point of ppm is simplicity after all.
And actually, a ppm reader I've just downloaded, an image viewer that
deals with dozens of formats, had problems when I tried to put
everything on one line. (I think it needs the signature on its own line.)
Reading an arbitrary PPM thereby is going to be tedious.
PPM was intended to be simple to read and to write (try TIFF, or JPEG,
for something that is going to be a lot more work).
ppmfil = open("junk.ppm", "wb")
(ppmfil? We don't have have 6-character limits any more.)
header = struct.pack("3s27s",
... b"P6 ",
... bytes("%8s %8s %8s\n" %
... (width, height, maxval),
... "ASCII"))
header
b'P6 1024 768 255\n'
Hmm, I'd write this elsewhere, if it's going to be one line, as just:
println @f,"P6",width,height,"255"
I'm sure Python must be able to do something along these lines, even if
it's:
f.write("P6 "+str(width)+" "+str(height)+" 255\n")
with whatever is needed to make that string compatible with a binary
file. I don't know what the struct.pack stuff is for; the header can
clearly be free-format text.
And how would that language handle Unicode text?
That's not relevant here. (Where it might be relevant, then Unicode must
be encoded as UTF8 within an 8-bit string.)
--
bartc
--
https://mail.python.org/mailman/listinfo/python-list