Here is the version of Python 3 code in my other answer . It is almost identical, except for the import, which had to be modified in order to use pillowfork PIL(because only it supports Python 3). Other changes I made were to change statements printinto function calls and where the function is map()used to create a lookup table variable luts.
from PIL import Image
from PIL.ImageColor import getcolor, getrgb
from PIL.ImageOps import grayscale
def image_tint(src, tint='#ffffff'):
if Image.isStringType(src):
src = Image.open(src)
if src.mode not in ['RGB', 'RGBA']:
raise TypeError('Unsupported source image mode: {}'.format(src.mode))
src.load()
tr, tg, tb = getrgb(tint)
tl = getcolor(tint, "L")
if not tl: tl = 1
tl = float(tl)
sr, sg, sb = map(lambda tv: tv/tl, (tr, tg, tb))
luts = (tuple(map(lambda lr: int(lr*sr + 0.5), range(256))) +
tuple(map(lambda lg: int(lg*sg + 0.5), range(256))) +
tuple(map(lambda lb: int(lb*sb + 0.5), range(256))))
l = grayscale(src)
if Image.getmodebands(src.mode) < 4:
merge_args = (src.mode, (l, l, l))
else:
a = Image.new("L", src.size)
a.putdata(src.getdata(3))
merge_args = (src.mode, (l, l, l, a))
luts += tuple(range(256))
return Image.merge(*merge_args).point(luts)
if __name__ == '__main__':
import os
import sys
input_image_path = 'Dn3CeZB.png'
print('tinting "{}"'.format(input_image_path))
root, ext = os.path.splitext(input_image_path)
suffix = '_result_py{}'.format(sys.version_info[0])
result_image_path = root+suffix+ext
print('creating "{}"'.format(result_image_path))
result = image_tint(input_image_path, '#383D2D')
if os.path.exists(result_image_path):
os.remove(result_image_path)
result.save(result_image_path)
print('done')
Here before and after the images. The test image and hue color match what you say you use when you encounter a problem. The results look very similar to the version of Py2, yours, and OK for me ... am I missing something?

source
share