mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-26 09:56:17 +03:00
Merge pull request #809 from hugovk/public_gamma_lut
Make _make_gamma_lut() public
This commit is contained in:
commit
7155919b5b
|
@ -17,19 +17,20 @@
|
|||
#
|
||||
|
||||
import array
|
||||
import warnings
|
||||
from PIL import Image, ImageColor
|
||||
|
||||
|
||||
class ImagePalette:
|
||||
"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.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.colors = {}
|
||||
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))):
|
||||
raise ValueError("wrong palette size")
|
||||
|
||||
|
@ -55,7 +56,7 @@ class ImagePalette:
|
|||
return self.palette
|
||||
arr = array.array("B", self.palette)
|
||||
if hasattr(arr, 'tobytes'):
|
||||
#py3k has a tobytes, tostring is deprecated.
|
||||
# py3k has a tobytes, tostring is deprecated.
|
||||
return arr.tobytes()
|
||||
return arr.tostring()
|
||||
|
||||
|
@ -109,6 +110,7 @@ class ImagePalette:
|
|||
fp.write("\n")
|
||||
fp.close()
|
||||
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
# Internal
|
||||
|
||||
|
@ -119,32 +121,57 @@ def raw(rawmode, data):
|
|||
palette.dirty = 1
|
||||
return palette
|
||||
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
# Factories
|
||||
|
||||
def _make_linear_lut(black, white):
|
||||
warnings.warn(
|
||||
'_make_linear_lut() is deprecated. '
|
||||
'Please call make_linear_lut() instead.',
|
||||
DeprecationWarning,
|
||||
stacklevel=2
|
||||
)
|
||||
return make_linear_lut(black, white)
|
||||
|
||||
|
||||
def _make_gamma_lut(exp):
|
||||
warnings.warn(
|
||||
'_make_gamma_lut() is deprecated. '
|
||||
'Please call make_gamma_lut() instead.',
|
||||
DeprecationWarning,
|
||||
stacklevel=2
|
||||
)
|
||||
return make_gamma_lut(exp)
|
||||
|
||||
|
||||
def make_linear_lut(black, white):
|
||||
lut = []
|
||||
if black == 0:
|
||||
for i in range(256):
|
||||
lut.append(white*i//255)
|
||||
else:
|
||||
raise NotImplementedError # FIXME
|
||||
raise NotImplementedError # FIXME
|
||||
return lut
|
||||
|
||||
def _make_gamma_lut(exp, mode="RGB"):
|
||||
|
||||
def make_gamma_lut(exp):
|
||||
lut = []
|
||||
for i in range(256):
|
||||
lut.append(int(((i / 255.0) ** exp) * 255.0 + 0.5))
|
||||
return lut
|
||||
|
||||
|
||||
def new(mode, data):
|
||||
return Image.core.new_palette(mode, data)
|
||||
|
||||
|
||||
def negative(mode="RGB"):
|
||||
palette = list(range(256))
|
||||
palette.reverse()
|
||||
return ImagePalette(mode, palette * len(mode))
|
||||
|
||||
|
||||
def random(mode="RGB"):
|
||||
from random import randint
|
||||
palette = []
|
||||
|
@ -152,16 +179,19 @@ def random(mode="RGB"):
|
|||
palette.append(randint(0, 255))
|
||||
return ImagePalette(mode, palette)
|
||||
|
||||
|
||||
def sepia(white="#fff0c0"):
|
||||
r, g, b = ImageColor.getrgb(white)
|
||||
r = _make_linear_lut(0, r)
|
||||
g = _make_linear_lut(0, g)
|
||||
b = _make_linear_lut(0, b)
|
||||
r = make_linear_lut(0, r)
|
||||
g = make_linear_lut(0, g)
|
||||
b = make_linear_lut(0, b)
|
||||
return ImagePalette("RGB", r + g + b)
|
||||
|
||||
|
||||
def wedge(mode="RGB"):
|
||||
return ImagePalette(mode, list(range(256)) * len(mode))
|
||||
|
||||
|
||||
def load(filename):
|
||||
|
||||
# FIXME: supports GIMP gradients only
|
||||
|
@ -177,8 +207,8 @@ def load(filename):
|
|||
p = GimpPaletteFile.GimpPaletteFile(fp)
|
||||
lut = p.getpalette()
|
||||
except (SyntaxError, ValueError):
|
||||
#import traceback
|
||||
#traceback.print_exc()
|
||||
# import traceback
|
||||
# traceback.print_exc()
|
||||
pass
|
||||
|
||||
if not lut:
|
||||
|
@ -188,8 +218,8 @@ def load(filename):
|
|||
p = GimpGradientFile.GimpGradientFile(fp)
|
||||
lut = p.getpalette()
|
||||
except (SyntaxError, ValueError):
|
||||
#import traceback
|
||||
#traceback.print_exc()
|
||||
# import traceback
|
||||
# traceback.print_exc()
|
||||
pass
|
||||
|
||||
if not lut:
|
||||
|
@ -206,4 +236,4 @@ def load(filename):
|
|||
if not lut:
|
||||
raise IOError("cannot load palette")
|
||||
|
||||
return lut # data, rawmode
|
||||
return lut # data, rawmode
|
||||
|
|
|
@ -44,6 +44,74 @@ class TestImagePalette(PillowTestCase):
|
|||
self.assertIsInstance(p, ImagePalette)
|
||||
self.assertEqual(p.palette, palette.tobytes())
|
||||
|
||||
def test_make_linear_lut(self):
|
||||
# Arrange
|
||||
from PIL.ImagePalette import make_linear_lut
|
||||
black = 0
|
||||
white = 255
|
||||
|
||||
# Act
|
||||
lut = make_linear_lut(black, white)
|
||||
|
||||
# Assert
|
||||
self.assertIsInstance(lut, list)
|
||||
self.assertEqual(len(lut), 256)
|
||||
# Check values
|
||||
for i in range(0, len(lut)):
|
||||
self.assertEqual(lut[i], i)
|
||||
|
||||
def test_make_linear_lut_not_yet_implemented(self):
|
||||
# Update after FIXME
|
||||
# Arrange
|
||||
from PIL.ImagePalette import make_linear_lut
|
||||
black = 1
|
||||
white = 255
|
||||
|
||||
# Act
|
||||
self.assertRaises(
|
||||
NotImplementedError,
|
||||
lambda: make_linear_lut(black, white))
|
||||
|
||||
def test_make_gamma_lut(self):
|
||||
# Arrange
|
||||
from PIL.ImagePalette import make_gamma_lut
|
||||
exp = 5
|
||||
|
||||
# Act
|
||||
lut = make_gamma_lut(exp)
|
||||
|
||||
# Assert
|
||||
self.assertIsInstance(lut, list)
|
||||
self.assertEqual(len(lut), 256)
|
||||
# Check a few values
|
||||
self.assertEqual(lut[0], 0)
|
||||
self.assertEqual(lut[63], 0)
|
||||
self.assertEqual(lut[127], 8)
|
||||
self.assertEqual(lut[191], 60)
|
||||
self.assertEqual(lut[255], 255)
|
||||
|
||||
def test_private_make_linear_lut_warning(self):
|
||||
# Arrange
|
||||
from PIL.ImagePalette import _make_linear_lut
|
||||
black = 0
|
||||
white = 255
|
||||
|
||||
# Act / Assert
|
||||
self.assert_warning(
|
||||
DeprecationWarning,
|
||||
lambda: _make_linear_lut(black, white))
|
||||
|
||||
def test_private_make_gamma_lut_warning(self):
|
||||
# Arrange
|
||||
from PIL.ImagePalette import _make_gamma_lut
|
||||
exp = 5
|
||||
|
||||
# Act / Assert
|
||||
self.assert_warning(
|
||||
DeprecationWarning,
|
||||
lambda: _make_gamma_lut(exp))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user