这是我的另一个答案中代码的 Python 3 版本。除了必须更改导入以使用pillow
fork之外,它几乎相同PIL
(因为只有它支持 Python 3)。我所做的其他更改是将print
语句更改为函数调用以及map()
函数用于创建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): # file path?
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") # tint color's overall luminosity
if not tl: tl = 1 # avoid division by zero
tl = float(tl) # compute luminosity preserving tint factors
sr, sg, sb = map(lambda tv: tv/tl, (tr, tg, tb)) # per component
# adjustments
# create look-up tables to map luminosity to adjusted tint
# (using floating-point math only to compute table)
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) # 8-bit luminosity version of whole image
if Image.getmodebands(src.mode) < 4:
merge_args = (src.mode, (l, l, l)) # for RGB verion of grayscale
else: # include copy of src image's alpha layer
a = Image.new("L", src.size)
a.putdata(src.getdata(3))
merge_args = (src.mode, (l, l, l, a)) # for RGBA verion of grayscale
luts += tuple(range(256)) # for 1:1 mapping of copied alpha values
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): # delete any previous result file
os.remove(result_image_path)
result.save(result_image_path) # file name's extension determines format
print('done')
这是之前和之后的图像。测试图像和色调颜色与您遇到问题时所说的相同。结果看起来与 Py2 版本非常相似,对你来说,对我来说还可以……我错过了什么吗?
