mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-28 02:04:36 +03:00
Merge pull request #2690 from neiljp/typing_patches
[WIP] Code alteration supplementing annotation in #2687
This commit is contained in:
commit
8600f1840d
56
PIL/Image.py
56
PIL/Image.py
|
@ -270,12 +270,11 @@ _MODE_CONV = {
|
||||||
|
|
||||||
|
|
||||||
def _conv_type_shape(im):
|
def _conv_type_shape(im):
|
||||||
shape = im.size[1], im.size[0]
|
|
||||||
typ, extra = _MODE_CONV[im.mode]
|
typ, extra = _MODE_CONV[im.mode]
|
||||||
if extra is None:
|
if extra is None:
|
||||||
return shape, typ
|
return (im.size[1], im.size[0]), typ
|
||||||
else:
|
else:
|
||||||
return shape+(extra,), typ
|
return (im.size[1], im.size[0], extra), typ
|
||||||
|
|
||||||
|
|
||||||
MODES = sorted(_MODEINFO)
|
MODES = sorted(_MODEINFO)
|
||||||
|
@ -583,24 +582,31 @@ class Image(object):
|
||||||
|
|
||||||
def _dump(self, file=None, format=None, **options):
|
def _dump(self, file=None, format=None, **options):
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
suffix = ''
|
suffix = ''
|
||||||
if format:
|
if format:
|
||||||
suffix = '.'+format
|
suffix = '.'+format
|
||||||
|
|
||||||
if not file:
|
if not file:
|
||||||
f, file = tempfile.mkstemp(suffix)
|
f, filename = tempfile.mkstemp(suffix)
|
||||||
os.close(f)
|
os.close(f)
|
||||||
|
else:
|
||||||
|
filename = file
|
||||||
|
if not filename.endswith(suffix):
|
||||||
|
filename = filename + suffix
|
||||||
|
|
||||||
self.load()
|
self.load()
|
||||||
|
|
||||||
if not format or format == "PPM":
|
if not format or format == "PPM":
|
||||||
self.im.save_ppm(file)
|
self.im.save_ppm(filename)
|
||||||
else:
|
else:
|
||||||
if not file.endswith(format):
|
self.save(filename, format, **options)
|
||||||
file = file + "." + format
|
|
||||||
self.save(file, format, **options)
|
return filename
|
||||||
return file
|
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
return (self.__class__.__name__ == other.__class__.__name__ and
|
return (isinstance(other, Image) and
|
||||||
|
self.__class__.__name__ == other.__class__.__name__ and
|
||||||
self.mode == other.mode and
|
self.mode == other.mode and
|
||||||
self.size == other.size and
|
self.size == other.size and
|
||||||
self.info == other.info and
|
self.info == other.info and
|
||||||
|
@ -1618,16 +1624,18 @@ class Image(object):
|
||||||
|
|
||||||
if source_palette is None:
|
if source_palette is None:
|
||||||
if self.mode == "P":
|
if self.mode == "P":
|
||||||
source_palette = self.im.getpalette("RGB")[:768]
|
real_source_palette = self.im.getpalette("RGB")[:768]
|
||||||
else: # L-mode
|
else: # L-mode
|
||||||
source_palette = bytearray(i//3 for i in range(768))
|
real_source_palette = bytearray(i//3 for i in range(768))
|
||||||
|
else:
|
||||||
|
real_source_palette = source_palette
|
||||||
|
|
||||||
palette_bytes = b""
|
palette_bytes = b""
|
||||||
new_positions = [0]*256
|
new_positions = [0]*256
|
||||||
|
|
||||||
# pick only the used colors from the palette
|
# pick only the used colors from the palette
|
||||||
for i, oldPosition in enumerate(dest_map):
|
for i, oldPosition in enumerate(dest_map):
|
||||||
palette_bytes += source_palette[oldPosition*3:oldPosition*3+3]
|
palette_bytes += real_source_palette[oldPosition*3:oldPosition*3+3]
|
||||||
new_positions[oldPosition] = i
|
new_positions[oldPosition] = i
|
||||||
|
|
||||||
# replace the palette color id of all pixel with the new id
|
# replace the palette color id of all pixel with the new id
|
||||||
|
@ -1783,9 +1791,13 @@ class Image(object):
|
||||||
w, h = self.size
|
w, h = self.size
|
||||||
|
|
||||||
if translate is None:
|
if translate is None:
|
||||||
translate = [0, 0]
|
post_trans = (0, 0)
|
||||||
|
else:
|
||||||
|
post_trans = translate
|
||||||
if center is None:
|
if center is None:
|
||||||
center = [w / 2.0, h / 2.0]
|
rotn_center = (w / 2.0, h / 2.0) # FIXME These should be rounded to ints?
|
||||||
|
else:
|
||||||
|
rotn_center = center
|
||||||
|
|
||||||
angle = - math.radians(angle)
|
angle = - math.radians(angle)
|
||||||
matrix = [
|
matrix = [
|
||||||
|
@ -1797,10 +1809,10 @@ class Image(object):
|
||||||
(a, b, c, d, e, f) = matrix
|
(a, b, c, d, e, f) = matrix
|
||||||
return a*x + b*y + c, d*x + e*y + f
|
return a*x + b*y + c, d*x + e*y + f
|
||||||
|
|
||||||
matrix[2], matrix[5] = transform(-center[0] - translate[0],
|
matrix[2], matrix[5] = transform(-rotn_center[0] - post_trans[0],
|
||||||
-center[1] - translate[1], matrix)
|
-rotn_center[1] - post_trans[1], matrix)
|
||||||
matrix[2] += center[0]
|
matrix[2] += rotn_center[0]
|
||||||
matrix[5] += center[1]
|
matrix[5] += rotn_center[1]
|
||||||
|
|
||||||
if expand:
|
if expand:
|
||||||
# calculate output size
|
# calculate output size
|
||||||
|
@ -2641,10 +2653,10 @@ def merge(mode, bands):
|
||||||
|
|
||||||
if getmodebands(mode) != len(bands) or "*" in mode:
|
if getmodebands(mode) != len(bands) or "*" in mode:
|
||||||
raise ValueError("wrong number of bands")
|
raise ValueError("wrong number of bands")
|
||||||
for im in bands[1:]:
|
for band in bands[1:]:
|
||||||
if im.mode != getmodetype(mode):
|
if band.mode != getmodetype(mode):
|
||||||
raise ValueError("mode mismatch")
|
raise ValueError("mode mismatch")
|
||||||
if im.size != bands[0].size:
|
if band.size != bands[0].size:
|
||||||
raise ValueError("size mismatch")
|
raise ValueError("size mismatch")
|
||||||
for band in bands:
|
for band in bands:
|
||||||
band.load()
|
band.load()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user