mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-27 17:54:32 +03:00
pep8
This commit is contained in:
parent
e835dd70a1
commit
f4071ade0a
|
@ -34,7 +34,8 @@
|
||||||
|
|
||||||
__version__ = "0.6"
|
__version__ = "0.6"
|
||||||
|
|
||||||
import array, struct
|
import array
|
||||||
|
import struct
|
||||||
from PIL import Image, ImageFile, _binary
|
from PIL import Image, ImageFile, _binary
|
||||||
from PIL.JpegPresets import presets
|
from PIL.JpegPresets import presets
|
||||||
from PIL._util import isStringType
|
from PIL._util import isStringType
|
||||||
|
@ -44,6 +45,7 @@ o8 = _binary.o8
|
||||||
i16 = _binary.i16be
|
i16 = _binary.i16be
|
||||||
i32 = _binary.i32be
|
i32 = _binary.i32be
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Parser
|
# Parser
|
||||||
|
|
||||||
|
@ -51,6 +53,7 @@ def Skip(self, marker):
|
||||||
n = i16(self.fp.read(2))-2
|
n = i16(self.fp.read(2))-2
|
||||||
ImageFile._safe_read(self.fp, n)
|
ImageFile._safe_read(self.fp, n)
|
||||||
|
|
||||||
|
|
||||||
def APP(self, marker):
|
def APP(self, marker):
|
||||||
#
|
#
|
||||||
# Application marker. Store these in the APP dictionary.
|
# Application marker. Store these in the APP dictionary.
|
||||||
|
@ -59,14 +62,14 @@ def APP(self, marker):
|
||||||
n = i16(self.fp.read(2))-2
|
n = i16(self.fp.read(2))-2
|
||||||
s = ImageFile._safe_read(self.fp, n)
|
s = ImageFile._safe_read(self.fp, n)
|
||||||
|
|
||||||
app = "APP%d" % (marker&15)
|
app = "APP%d" % (marker & 15)
|
||||||
|
|
||||||
self.app[app] = s # compatibility
|
self.app[app] = s # compatibility
|
||||||
self.applist.append((app, s))
|
self.applist.append((app, s))
|
||||||
|
|
||||||
if marker == 0xFFE0 and s[:4] == b"JFIF":
|
if marker == 0xFFE0 and s[:4] == b"JFIF":
|
||||||
# extract JFIF information
|
# extract JFIF information
|
||||||
self.info["jfif"] = version = i16(s, 5) # version
|
self.info["jfif"] = version = i16(s, 5) # version
|
||||||
self.info["jfif_version"] = divmod(version, 256)
|
self.info["jfif_version"] = divmod(version, 256)
|
||||||
# extract JFIF properties
|
# extract JFIF properties
|
||||||
try:
|
try:
|
||||||
|
@ -81,10 +84,10 @@ def APP(self, marker):
|
||||||
self.info["jfif_density"] = jfif_density
|
self.info["jfif_density"] = jfif_density
|
||||||
elif marker == 0xFFE1 and s[:5] == b"Exif\0":
|
elif marker == 0xFFE1 and s[:5] == b"Exif\0":
|
||||||
# extract Exif information (incomplete)
|
# extract Exif information (incomplete)
|
||||||
self.info["exif"] = s # FIXME: value will change
|
self.info["exif"] = s # FIXME: value will change
|
||||||
elif marker == 0xFFE2 and s[:5] == b"FPXR\0":
|
elif marker == 0xFFE2 and s[:5] == b"FPXR\0":
|
||||||
# extract FlashPix information (incomplete)
|
# extract FlashPix information (incomplete)
|
||||||
self.info["flashpix"] = s # FIXME: value will change
|
self.info["flashpix"] = s # FIXME: value will change
|
||||||
elif marker == 0xFFE2 and s[:12] == b"ICC_PROFILE\0":
|
elif marker == 0xFFE2 and s[:12] == b"ICC_PROFILE\0":
|
||||||
# Since an ICC profile can be larger than the maximum size of
|
# Since an ICC profile can be larger than the maximum size of
|
||||||
# a JPEG marker (64K), we need provisions to split it into
|
# a JPEG marker (64K), we need provisions to split it into
|
||||||
|
@ -108,16 +111,17 @@ def APP(self, marker):
|
||||||
else:
|
else:
|
||||||
self.info["adobe_transform"] = adobe_transform
|
self.info["adobe_transform"] = adobe_transform
|
||||||
|
|
||||||
|
|
||||||
def COM(self, marker):
|
def COM(self, marker):
|
||||||
#
|
#
|
||||||
# Comment marker. Store these in the APP dictionary.
|
# Comment marker. Store these in the APP dictionary.
|
||||||
|
|
||||||
n = i16(self.fp.read(2))-2
|
n = i16(self.fp.read(2))-2
|
||||||
s = ImageFile._safe_read(self.fp, n)
|
s = ImageFile._safe_read(self.fp, n)
|
||||||
|
|
||||||
self.app["COM"] = s # compatibility
|
self.app["COM"] = s # compatibility
|
||||||
self.applist.append(("COM", s))
|
self.applist.append(("COM", s))
|
||||||
|
|
||||||
|
|
||||||
def SOF(self, marker):
|
def SOF(self, marker):
|
||||||
#
|
#
|
||||||
# Start of frame marker. Defines the size and mode of the
|
# Start of frame marker. Defines the size and mode of the
|
||||||
|
@ -149,21 +153,22 @@ def SOF(self, marker):
|
||||||
|
|
||||||
if self.icclist:
|
if self.icclist:
|
||||||
# fixup icc profile
|
# fixup icc profile
|
||||||
self.icclist.sort() # sort by sequence number
|
self.icclist.sort() # sort by sequence number
|
||||||
if i8(self.icclist[0][13]) == len(self.icclist):
|
if i8(self.icclist[0][13]) == len(self.icclist):
|
||||||
profile = []
|
profile = []
|
||||||
for p in self.icclist:
|
for p in self.icclist:
|
||||||
profile.append(p[14:])
|
profile.append(p[14:])
|
||||||
icc_profile = b"".join(profile)
|
icc_profile = b"".join(profile)
|
||||||
else:
|
else:
|
||||||
icc_profile = None # wrong number of fragments
|
icc_profile = None # wrong number of fragments
|
||||||
self.info["icc_profile"] = icc_profile
|
self.info["icc_profile"] = icc_profile
|
||||||
self.icclist = None
|
self.icclist = None
|
||||||
|
|
||||||
for i in range(6, len(s), 3):
|
for i in range(6, len(s), 3):
|
||||||
t = s[i:i+3]
|
t = s[i:i+3]
|
||||||
# 4-tuples: id, vsamp, hsamp, qtable
|
# 4-tuples: id, vsamp, hsamp, qtable
|
||||||
self.layer.append((t[0], i8(t[1])//16, i8(t[1])&15, i8(t[2])))
|
self.layer.append((t[0], i8(t[1])//16, i8(t[1]) & 15, i8(t[2])))
|
||||||
|
|
||||||
|
|
||||||
def DQT(self, marker):
|
def DQT(self, marker):
|
||||||
#
|
#
|
||||||
|
@ -181,10 +186,10 @@ def DQT(self, marker):
|
||||||
raise SyntaxError("bad quantization table marker")
|
raise SyntaxError("bad quantization table marker")
|
||||||
v = i8(s[0])
|
v = i8(s[0])
|
||||||
if v//16 == 0:
|
if v//16 == 0:
|
||||||
self.quantization[v&15] = array.array("b", s[1:65])
|
self.quantization[v & 15] = array.array("b", s[1:65])
|
||||||
s = s[65:]
|
s = s[65:]
|
||||||
else:
|
else:
|
||||||
return # FIXME: add code to read 16-bit tables!
|
return # FIXME: add code to read 16-bit tables!
|
||||||
# raise SyntaxError, "bad quantization table element size"
|
# raise SyntaxError, "bad quantization table element size"
|
||||||
|
|
||||||
|
|
||||||
|
@ -261,6 +266,7 @@ MARKER = {
|
||||||
def _accept(prefix):
|
def _accept(prefix):
|
||||||
return prefix[0:1] == b"\377"
|
return prefix[0:1] == b"\377"
|
||||||
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# Image plugin for JPEG and JFIF images.
|
# Image plugin for JPEG and JFIF images.
|
||||||
|
|
||||||
|
@ -284,7 +290,7 @@ class JpegImageFile(ImageFile.ImageFile):
|
||||||
self.huffman_dc = {}
|
self.huffman_dc = {}
|
||||||
self.huffman_ac = {}
|
self.huffman_ac = {}
|
||||||
self.quantization = {}
|
self.quantization = {}
|
||||||
self.app = {} # compatibility
|
self.app = {} # compatibility
|
||||||
self.applist = []
|
self.applist = []
|
||||||
self.icclist = []
|
self.icclist = []
|
||||||
|
|
||||||
|
@ -348,7 +354,8 @@ class JpegImageFile(ImageFile.ImageFile):
|
||||||
|
|
||||||
# ALTERNATIVE: handle JPEGs via the IJG command line utilities
|
# ALTERNATIVE: handle JPEGs via the IJG command line utilities
|
||||||
|
|
||||||
import tempfile, os
|
import tempfile
|
||||||
|
import os
|
||||||
f, path = tempfile.mkstemp()
|
f, path = tempfile.mkstemp()
|
||||||
os.close(f)
|
os.close(f)
|
||||||
if os.path.exists(self.filename):
|
if os.path.exists(self.filename):
|
||||||
|
@ -359,8 +366,10 @@ class JpegImageFile(ImageFile.ImageFile):
|
||||||
try:
|
try:
|
||||||
self.im = Image.core.open_ppm(path)
|
self.im = Image.core.open_ppm(path)
|
||||||
finally:
|
finally:
|
||||||
try: os.unlink(path)
|
try:
|
||||||
except: pass
|
os.unlink(path)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
self.mode = self.im.mode
|
self.mode = self.im.mode
|
||||||
self.size = self.im.size
|
self.size = self.im.size
|
||||||
|
@ -377,6 +386,7 @@ def _getexif(self):
|
||||||
# version.
|
# version.
|
||||||
from PIL import TiffImagePlugin
|
from PIL import TiffImagePlugin
|
||||||
import io
|
import io
|
||||||
|
|
||||||
def fixup(value):
|
def fixup(value):
|
||||||
if len(value) == 1:
|
if len(value) == 1:
|
||||||
return value[0]
|
return value[0]
|
||||||
|
@ -427,7 +437,7 @@ RAWMODE = {
|
||||||
"RGB": "RGB",
|
"RGB": "RGB",
|
||||||
"RGBA": "RGB",
|
"RGBA": "RGB",
|
||||||
"RGBX": "RGB",
|
"RGBX": "RGB",
|
||||||
"CMYK": "CMYK;I", # assume adobe conventions
|
"CMYK": "CMYK;I", # assume adobe conventions
|
||||||
"YCbCr": "YCbCr",
|
"YCbCr": "YCbCr",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -446,16 +456,19 @@ samplings = {
|
||||||
(2, 2, 1, 1, 1, 1): 2,
|
(2, 2, 1, 1, 1, 1): 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def convert_dict_qtables(qtables):
|
def convert_dict_qtables(qtables):
|
||||||
qtables = [qtables[key] for key in range(len(qtables)) if key in qtables]
|
qtables = [qtables[key] for key in range(len(qtables)) if key in qtables]
|
||||||
for idx, table in enumerate(qtables):
|
for idx, table in enumerate(qtables):
|
||||||
qtables[idx] = [table[i] for i in zigzag_index]
|
qtables[idx] = [table[i] for i in zigzag_index]
|
||||||
return qtables
|
return qtables
|
||||||
|
|
||||||
|
|
||||||
def get_sampling(im):
|
def get_sampling(im):
|
||||||
sampling = im.layer[0][1:3] + im.layer[1][1:3] + im.layer[2][1:3]
|
sampling = im.layer[0][1:3] + im.layer[1][1:3] + im.layer[2][1:3]
|
||||||
return samplings.get(sampling, -1)
|
return samplings.get(sampling, -1)
|
||||||
|
|
||||||
|
|
||||||
def _save(im, fp, filename):
|
def _save(im, fp, filename):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -568,12 +581,11 @@ def _save(im, fp, filename):
|
||||||
info.get("exif", b"")
|
info.get("exif", b"")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# if we optimize, libjpeg needs a buffer big enough to hold the whole image
|
||||||
# if we optimize, libjpeg needs a buffer big enough to hold the whole image in a shot.
|
# in a shot. Guessing on the size, at im.size bytes. (raw pizel size is
|
||||||
# Guessing on the size, at im.size bytes. (raw pizel size is channels*size, this
|
# channels*size, this is a value that's been used in a django patch.
|
||||||
# is a value that's been used in a django patch.
|
|
||||||
# https://github.com/jdriscoll/django-imagekit/issues/50
|
# https://github.com/jdriscoll/django-imagekit/issues/50
|
||||||
bufsize=0
|
bufsize = 0
|
||||||
if "optimize" in info or "progressive" in info or "progression" in info:
|
if "optimize" in info or "progressive" in info or "progression" in info:
|
||||||
if quality >= 95:
|
if quality >= 95:
|
||||||
bufsize = 2 * im.size[0] * im.size[1]
|
bufsize = 2 * im.size[0] * im.size[1]
|
||||||
|
@ -582,17 +594,20 @@ def _save(im, fp, filename):
|
||||||
|
|
||||||
# The exif info needs to be written as one block, + APP1, + one spare byte.
|
# The exif info needs to be written as one block, + APP1, + one spare byte.
|
||||||
# Ensure that our buffer is big enough
|
# Ensure that our buffer is big enough
|
||||||
bufsize = max(ImageFile.MAXBLOCK, bufsize, len(info.get("exif",b"")) + 5 )
|
bufsize = max(ImageFile.MAXBLOCK, bufsize, len(info.get("exif", b"")) + 5)
|
||||||
|
|
||||||
|
ImageFile._save(im, fp, [("jpeg", (0, 0)+im.size, 0, rawmode)], bufsize)
|
||||||
|
|
||||||
ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)], bufsize)
|
|
||||||
|
|
||||||
def _save_cjpeg(im, fp, filename):
|
def _save_cjpeg(im, fp, filename):
|
||||||
# ALTERNATIVE: handle JPEGs via the IJG command line utilities.
|
# ALTERNATIVE: handle JPEGs via the IJG command line utilities.
|
||||||
import os
|
import os
|
||||||
file = im._dump()
|
file = im._dump()
|
||||||
os.system("cjpeg %s >%s" % (file, filename))
|
os.system("cjpeg %s >%s" % (file, filename))
|
||||||
try: os.unlink(file)
|
try:
|
||||||
except: pass
|
os.unlink(file)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
# -------------------------------------------------------------------q-
|
# -------------------------------------------------------------------q-
|
||||||
# Registry stuff
|
# Registry stuff
|
||||||
|
|
Loading…
Reference in New Issue
Block a user