Merge pull request #809 from hugovk/public_gamma_lut

Make _make_gamma_lut() public
This commit is contained in:
wiredfool 2014-07-20 11:37:41 -07:00
commit 7155919b5b
2 changed files with 112 additions and 14 deletions

View File

@ -17,13 +17,14 @@
#
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.palette = palette or list(range(256))*len(self.mode)
@ -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,10 +121,31 @@ 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):
@ -131,20 +154,24 @@ def _make_linear_lut(black, white):
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:

View File

@ -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()