Pillow/PIL/PpmImagePlugin.py

168 lines
4.4 KiB
Python
Raw Normal View History

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.
#
import string
from . import Image, ImageFile
2010-07-31 06:52:47 +04:00
2015-08-25 15:27:18 +03:00
__version__ = "0.2"
2010-07-31 06:52:47 +04:00
#
# --------------------------------------------------------------------
b_whitespace = string.whitespace.strip()
try:
import locale
2014-08-26 17:47:10 +04:00
locale_lang, locale_enc = locale.getlocale()
if locale_enc is None:
2014-08-26 17:47:10 +04:00
locale_lang, locale_enc = locale.getdefaultlocale()
b_whitespace = b_whitespace.decode(locale_enc)
2014-08-26 17:47:10 +04:00
except:
pass
b_whitespace = b_whitespace.encode('ascii', 'ignore')
2010-07-31 06:52:47 +04:00
MODES = {
# standard
b"P4": "1",
b"P5": "L",
b"P6": "RGB",
2010-07-31 06:52:47 +04:00
# extensions
b"P0CMYK": "CMYK",
2010-07-31 06:52:47 +04:00
# PIL extensions (for test purposes only)
b"PyP": "P",
b"PyRGBA": "RGBA",
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):
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.
class PpmImageFile(ImageFile.ImageFile):
format = "PPM"
format_description = "Pbmplus image"
2014-08-26 17:47:10 +04:00
def _token(self, s=b""):
while True: # read until next whitespace
2010-07-31 06:52:47 +04:00
c = self.fp.read(1)
if not c or c in b_whitespace:
2010-07-31 06:52:47 +04:00
break
2014-04-09 09:43:57 +04:00
if c > b'\x79':
raise ValueError("Expected ASCII value, found binary")
2010-07-31 06:52:47 +04:00
s = s + c
2014-04-09 09:43:57 +04:00
if (len(s) > 9):
raise ValueError("Expected int, got > 9 digits")
2010-07-31 06:52:47 +04:00
return s
def _open(self):
# check magic
s = self.fp.read(1)
if s != b"P":
raise SyntaxError("not a PPM file")
2010-07-31 06:52:47 +04:00
mode = MODES[self._token(s)]
if mode == "1":
self.mode = "1"
rawmode = "1;I"
else:
self.mode = rawmode = mode
for ix in range(3):
while True:
while True:
2010-07-31 06:52:47 +04:00
s = self.fp.read(1)
if s not in b_whitespace:
2010-07-31 06:52:47 +04:00
break
2015-08-28 19:05:08 +03:00
if s == b"":
raise ValueError("File does not extend beyond magic number")
if s != b"#":
2010-07-31 06:52:47 +04:00
break
s = self.fp.readline()
s = int(self._token(s))
if ix == 0:
xsize = s
elif ix == 1:
ysize = s
if mode == "1":
break
2014-04-08 09:12:33 +04:00
elif ix == 2:
# maxgrey
if s > 255:
if not mode == 'L':
2014-08-26 17:47:10 +04:00
raise ValueError("Too many colors for band: %s" % s)
2014-04-08 09:24:48 +04:00
if s < 2**16:
2014-04-08 09:12:33 +04:00
self.mode = 'I'
rawmode = 'I;16B'
else:
2014-08-26 17:47:10 +04:00
self.mode = 'I'
2014-04-08 09:12:33 +04:00
rawmode = 'I;32B'
2014-08-26 17:47:10 +04:00
2010-07-31 06:52:47 +04:00
self.size = xsize, ysize
self.tile = [("raw",
(0, 0, xsize, ysize),
self.fp.tell(),
(rawmode, 0, 1))]
2014-08-26 17:47:10 +04:00
2010-07-31 06:52:47 +04:00
#
# --------------------------------------------------------------------
def _save(im, fp, filename):
if im.mode == "1":
rawmode, head = "1;I", b"P4"
2010-07-31 06:52:47 +04:00
elif im.mode == "L":
rawmode, head = "L", b"P5"
2014-04-08 09:23:04 +04:00
elif im.mode == "I":
if im.getextrema()[1] < 2**16:
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":
rawmode, head = "RGB", b"P6"
2010-07-31 06:52:47 +04:00
elif im.mode == "RGBA":
rawmode, head = "RGB", b"P6"
2010-07-31 06:52:47 +04:00
else:
raise IOError("cannot write mode %s as PPM" % im.mode)
fp.write(head + ("\n%d %d\n" % im.size).encode('ascii'))
2014-04-08 09:23:04 +04:00
if head == b"P6":
fp.write(b"255\n")
2014-04-08 09:23:04 +04:00
if head == b"P5":
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")
2014-08-26 17:47:10 +04: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)
#
# --------------------------------------------------------------------
Image.register_open(PpmImageFile.format, PpmImageFile, _accept)
Image.register_save(PpmImageFile.format, _save)
2010-07-31 06:52:47 +04:00
2016-04-25 07:59:02 +03:00
Image.register_extensions(PpmImageFile.format, [".pbm", ".pgm", ".ppm"])