mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-26 18:06:18 +03:00
flake8
This commit is contained in:
parent
4b40839970
commit
17223001df
|
@ -23,13 +23,13 @@ from PIL import Image, ImageColor
|
||||||
class ImagePalette:
|
class ImagePalette:
|
||||||
"Color palette for palette mapped images"
|
"Color palette for palette mapped images"
|
||||||
|
|
||||||
def __init__(self, mode = "RGB", palette = None, size = 0):
|
def __init__(self, mode="RGB", palette=None, size=0):
|
||||||
self.mode = mode
|
self.mode = mode
|
||||||
self.rawmode = None # if set, palette contains raw data
|
self.rawmode = None # if set, palette contains raw data
|
||||||
self.palette = palette or list(range(256))*len(self.mode)
|
self.palette = palette or list(range(256))*len(self.mode)
|
||||||
self.colors = {}
|
self.colors = {}
|
||||||
self.dirty = None
|
self.dirty = None
|
||||||
if ((size == 0 and len(self.mode)*256 != len(self.palette)) or
|
if ((size == 0 and len(self.mode)*256 != len(self.palette)) or
|
||||||
(size != 0 and size != len(self.palette))):
|
(size != 0 and size != len(self.palette))):
|
||||||
raise ValueError("wrong palette size")
|
raise ValueError("wrong palette size")
|
||||||
|
|
||||||
|
@ -55,7 +55,7 @@ class ImagePalette:
|
||||||
return self.palette
|
return self.palette
|
||||||
arr = array.array("B", self.palette)
|
arr = array.array("B", self.palette)
|
||||||
if hasattr(arr, 'tobytes'):
|
if hasattr(arr, 'tobytes'):
|
||||||
#py3k has a tobytes, tostring is deprecated.
|
# py3k has a tobytes, tostring is deprecated.
|
||||||
return arr.tobytes()
|
return arr.tobytes()
|
||||||
return arr.tostring()
|
return arr.tostring()
|
||||||
|
|
||||||
|
@ -109,6 +109,7 @@ class ImagePalette:
|
||||||
fp.write("\n")
|
fp.write("\n")
|
||||||
fp.close()
|
fp.close()
|
||||||
|
|
||||||
|
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
# Internal
|
# Internal
|
||||||
|
|
||||||
|
@ -119,6 +120,7 @@ def raw(rawmode, data):
|
||||||
palette.dirty = 1
|
palette.dirty = 1
|
||||||
return palette
|
return palette
|
||||||
|
|
||||||
|
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
# Factories
|
# Factories
|
||||||
|
|
||||||
|
@ -128,23 +130,27 @@ def _make_linear_lut(black, white):
|
||||||
for i in range(256):
|
for i in range(256):
|
||||||
lut.append(white*i//255)
|
lut.append(white*i//255)
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError # FIXME
|
raise NotImplementedError # FIXME
|
||||||
return lut
|
return lut
|
||||||
|
|
||||||
|
|
||||||
def _make_gamma_lut(exp, mode="RGB"):
|
def _make_gamma_lut(exp, mode="RGB"):
|
||||||
lut = []
|
lut = []
|
||||||
for i in range(256):
|
for i in range(256):
|
||||||
lut.append(int(((i / 255.0) ** exp) * 255.0 + 0.5))
|
lut.append(int(((i / 255.0) ** exp) * 255.0 + 0.5))
|
||||||
return lut
|
return lut
|
||||||
|
|
||||||
|
|
||||||
def new(mode, data):
|
def new(mode, data):
|
||||||
return Image.core.new_palette(mode, data)
|
return Image.core.new_palette(mode, data)
|
||||||
|
|
||||||
|
|
||||||
def negative(mode="RGB"):
|
def negative(mode="RGB"):
|
||||||
palette = list(range(256))
|
palette = list(range(256))
|
||||||
palette.reverse()
|
palette.reverse()
|
||||||
return ImagePalette(mode, palette * len(mode))
|
return ImagePalette(mode, palette * len(mode))
|
||||||
|
|
||||||
|
|
||||||
def random(mode="RGB"):
|
def random(mode="RGB"):
|
||||||
from random import randint
|
from random import randint
|
||||||
palette = []
|
palette = []
|
||||||
|
@ -152,6 +158,7 @@ def random(mode="RGB"):
|
||||||
palette.append(randint(0, 255))
|
palette.append(randint(0, 255))
|
||||||
return ImagePalette(mode, palette)
|
return ImagePalette(mode, palette)
|
||||||
|
|
||||||
|
|
||||||
def sepia(white="#fff0c0"):
|
def sepia(white="#fff0c0"):
|
||||||
r, g, b = ImageColor.getrgb(white)
|
r, g, b = ImageColor.getrgb(white)
|
||||||
r = _make_linear_lut(0, r)
|
r = _make_linear_lut(0, r)
|
||||||
|
@ -159,9 +166,11 @@ def sepia(white="#fff0c0"):
|
||||||
b = _make_linear_lut(0, b)
|
b = _make_linear_lut(0, b)
|
||||||
return ImagePalette("RGB", r + g + b)
|
return ImagePalette("RGB", r + g + b)
|
||||||
|
|
||||||
|
|
||||||
def wedge(mode="RGB"):
|
def wedge(mode="RGB"):
|
||||||
return ImagePalette(mode, list(range(256)) * len(mode))
|
return ImagePalette(mode, list(range(256)) * len(mode))
|
||||||
|
|
||||||
|
|
||||||
def load(filename):
|
def load(filename):
|
||||||
|
|
||||||
# FIXME: supports GIMP gradients only
|
# FIXME: supports GIMP gradients only
|
||||||
|
@ -177,8 +186,8 @@ def load(filename):
|
||||||
p = GimpPaletteFile.GimpPaletteFile(fp)
|
p = GimpPaletteFile.GimpPaletteFile(fp)
|
||||||
lut = p.getpalette()
|
lut = p.getpalette()
|
||||||
except (SyntaxError, ValueError):
|
except (SyntaxError, ValueError):
|
||||||
#import traceback
|
# import traceback
|
||||||
#traceback.print_exc()
|
# traceback.print_exc()
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if not lut:
|
if not lut:
|
||||||
|
@ -188,8 +197,8 @@ def load(filename):
|
||||||
p = GimpGradientFile.GimpGradientFile(fp)
|
p = GimpGradientFile.GimpGradientFile(fp)
|
||||||
lut = p.getpalette()
|
lut = p.getpalette()
|
||||||
except (SyntaxError, ValueError):
|
except (SyntaxError, ValueError):
|
||||||
#import traceback
|
# import traceback
|
||||||
#traceback.print_exc()
|
# traceback.print_exc()
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if not lut:
|
if not lut:
|
||||||
|
@ -206,4 +215,4 @@ def load(filename):
|
||||||
if not lut:
|
if not lut:
|
||||||
raise IOError("cannot load palette")
|
raise IOError("cannot load palette")
|
||||||
|
|
||||||
return lut # data, rawmode
|
return lut # data, rawmode
|
||||||
|
|
Loading…
Reference in New Issue
Block a user