2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# The Python Imaging Library.
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# PPM support for PIL
|
|
|
|
#
|
|
|
|
# History:
|
|
|
|
# 96-03-24 fl Created
|
|
|
|
# 98-03-06 fl Write RGBA images (as RGB, that is)
|
|
|
|
#
|
|
|
|
# Copyright (c) Secret Labs AB 1997-98.
|
|
|
|
# Copyright (c) Fredrik Lundh 1996.
|
|
|
|
#
|
|
|
|
# See the README file for information on usage and redistribution.
|
|
|
|
#
|
|
|
|
|
|
|
|
|
2017-01-17 16:22:18 +03:00
|
|
|
from . import Image, ImageFile
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2020-12-21 06:56:30 +03:00
|
|
|
B_WHITESPACE = b"\x20\x09\x0a\x0b\x0c\x0d"
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
MODES = {
|
|
|
|
# standard
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
b"P4": "1",
|
|
|
|
b"P5": "L",
|
|
|
|
b"P6": "RGB",
|
2010-07-31 06:52:47 +04:00
|
|
|
# extensions
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
b"P0CMYK": "CMYK",
|
2010-07-31 06:52:47 +04:00
|
|
|
# PIL extensions (for test purposes only)
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
b"PyP": "P",
|
|
|
|
b"PyRGBA": "RGBA",
|
2019-03-21 16:28:20 +03:00
|
|
|
b"PyCMYK": "CMYK",
|
2010-07-31 06:52:47 +04:00
|
|
|
}
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def _accept(prefix):
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
return prefix[0:1] == b"P" and prefix[1] in b"0456y"
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
##
|
|
|
|
# Image plugin for PBM, PGM, and PPM images.
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
class PpmImageFile(ImageFile.ImageFile):
|
|
|
|
|
|
|
|
format = "PPM"
|
|
|
|
format_description = "Pbmplus image"
|
|
|
|
|
2021-01-04 07:49:19 +03:00
|
|
|
def _read_magic(self, s=b""):
|
|
|
|
while True: # read until next whitespace
|
|
|
|
c = self.fp.read(1)
|
|
|
|
if c in B_WHITESPACE:
|
|
|
|
break
|
|
|
|
s = s + c
|
|
|
|
if len(s) > 6: # exceeded max magic number length
|
|
|
|
break
|
|
|
|
return s
|
|
|
|
|
2020-12-21 06:56:30 +03:00
|
|
|
def _read_token(self, token=b""):
|
|
|
|
def _ignore_comment(): # ignores rest of the line; stops at CR, LF or EOF
|
|
|
|
while True:
|
|
|
|
c = self.fp.read(1)
|
|
|
|
if c in b"\r\n":
|
|
|
|
break
|
|
|
|
|
|
|
|
while True: # read until non-whitespace is found
|
|
|
|
c = self.fp.read(1)
|
|
|
|
if c == b"#": # found comment, ignore it
|
|
|
|
_ignore_comment()
|
|
|
|
continue
|
|
|
|
if c in B_WHITESPACE: # found whitespace, ignore it
|
|
|
|
if c == b"": # reached EOF
|
|
|
|
raise EOFError("Reached EOF while reading header")
|
|
|
|
continue
|
|
|
|
break
|
|
|
|
|
|
|
|
token += c
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
while True: # read until next whitespace
|
2010-07-31 06:52:47 +04:00
|
|
|
c = self.fp.read(1)
|
2020-12-21 06:56:30 +03:00
|
|
|
if c == b"#":
|
|
|
|
_ignore_comment()
|
|
|
|
continue
|
|
|
|
if c in B_WHITESPACE: # token ended
|
2010-07-31 06:52:47 +04:00
|
|
|
break
|
2020-12-21 06:56:30 +03:00
|
|
|
token += c
|
|
|
|
return token
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def _open(self):
|
2021-01-04 07:49:19 +03:00
|
|
|
magic_number = self._read_magic()
|
|
|
|
try:
|
|
|
|
mode = MODES[magic_number]
|
|
|
|
except KeyError:
|
|
|
|
raise SyntaxError("Not a PPM image file") from None
|
2019-03-04 10:17:12 +03:00
|
|
|
|
|
|
|
self.custom_mimetype = {
|
|
|
|
b"P4": "image/x-portable-bitmap",
|
|
|
|
b"P5": "image/x-portable-graymap",
|
2019-03-05 16:01:56 +03:00
|
|
|
b"P6": "image/x-portable-pixmap",
|
2019-03-04 10:17:12 +03:00
|
|
|
}.get(magic_number)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
if mode == "1":
|
|
|
|
self.mode = "1"
|
|
|
|
rawmode = "1;I"
|
|
|
|
else:
|
|
|
|
self.mode = rawmode = mode
|
|
|
|
|
|
|
|
for ix in range(3):
|
2020-12-21 06:56:30 +03:00
|
|
|
try: # check token sanity
|
|
|
|
token = int(self._read_token())
|
|
|
|
except ValueError:
|
|
|
|
raise SyntaxError("Non-decimal-ASCII found in header")
|
|
|
|
if ix == 0: # token is the x size
|
|
|
|
xsize = token
|
|
|
|
elif ix == 1: # token is the y size
|
|
|
|
ysize = token
|
2010-07-31 06:52:47 +04:00
|
|
|
if mode == "1":
|
|
|
|
break
|
2020-12-21 06:56:30 +03:00
|
|
|
elif ix == 2: # token is maxval
|
|
|
|
if token > 255:
|
2019-03-21 16:28:20 +03:00
|
|
|
if not mode == "L":
|
2020-12-21 06:56:30 +03:00
|
|
|
raise SyntaxError(f"Too many colors for band: {token}")
|
|
|
|
if token < 2 ** 16:
|
2019-03-21 16:28:20 +03:00
|
|
|
self.mode = "I"
|
|
|
|
rawmode = "I;16B"
|
2014-04-08 09:12:33 +04:00
|
|
|
else:
|
2019-03-21 16:28:20 +03:00
|
|
|
self.mode = "I"
|
|
|
|
rawmode = "I;32B"
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2018-09-30 05:58:02 +03:00
|
|
|
self._size = xsize, ysize
|
2019-03-21 16:28:20 +03:00
|
|
|
self.tile = [("raw", (0, 0, xsize, ysize), self.fp.tell(), (rawmode, 0, 1))]
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def _save(im, fp, filename):
|
|
|
|
if im.mode == "1":
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
rawmode, head = "1;I", b"P4"
|
2010-07-31 06:52:47 +04:00
|
|
|
elif im.mode == "L":
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
rawmode, head = "L", b"P5"
|
2014-04-08 09:23:04 +04:00
|
|
|
elif im.mode == "I":
|
2019-03-21 16:28:20 +03:00
|
|
|
if im.getextrema()[1] < 2 ** 16:
|
2014-04-08 09:23:04 +04:00
|
|
|
rawmode, head = "I;16B", b"P5"
|
|
|
|
else:
|
|
|
|
rawmode, head = "I;32B", b"P5"
|
2010-07-31 06:52:47 +04:00
|
|
|
elif im.mode == "RGB":
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
rawmode, head = "RGB", b"P6"
|
2010-07-31 06:52:47 +04:00
|
|
|
elif im.mode == "RGBA":
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
rawmode, head = "RGB", b"P6"
|
2010-07-31 06:52:47 +04:00
|
|
|
else:
|
2020-07-16 12:43:29 +03:00
|
|
|
raise OSError(f"cannot write mode {im.mode} as PPM")
|
2019-03-21 16:28:20 +03:00
|
|
|
fp.write(head + ("\n%d %d\n" % im.size).encode("ascii"))
|
2014-04-08 09:23:04 +04:00
|
|
|
if head == b"P6":
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
fp.write(b"255\n")
|
2014-04-08 09:23:04 +04:00
|
|
|
if head == b"P5":
|
2014-04-09 09:44:24 +04:00
|
|
|
if rawmode == "L":
|
2014-04-08 09:23:04 +04:00
|
|
|
fp.write(b"255\n")
|
|
|
|
elif rawmode == "I;16B":
|
|
|
|
fp.write(b"65535\n")
|
|
|
|
elif rawmode == "I;32B":
|
|
|
|
fp.write(b"2147483648\n")
|
2019-03-21 16:28:20 +03:00
|
|
|
ImageFile._save(im, fp, [("raw", (0, 0) + im.size, 0, (rawmode, 0, 1))])
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# ALTERNATIVE: save via builtin debug function
|
|
|
|
# im._dump(filename)
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2015-07-04 16:29:58 +03:00
|
|
|
Image.register_open(PpmImageFile.format, PpmImageFile, _accept)
|
|
|
|
Image.register_save(PpmImageFile.format, _save)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2018-12-31 09:41:41 +03:00
|
|
|
Image.register_extensions(PpmImageFile.format, [".pbm", ".pgm", ".ppm", ".pnm"])
|
2019-03-04 10:17:12 +03:00
|
|
|
|
2019-03-06 00:06:19 +03:00
|
|
|
Image.register_mime(PpmImageFile.format, "image/x-portable-anymap")
|