Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image
- from PIL.ImageColor import getcolor, getrgb
- from PIL.ImageOps import grayscale
- import time
- for i in range(1,40):
- time.sleep(0.2)
- 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
- potato = Image.merge(*merge_args).point(luts)
- src.close()
- return potato
- if __name__ == '__main__':
- objgraph.show_most_common_types(limit=5)
- print("-----")
- import os
- input_image_path = 'headbase.png'
- root, ext = os.path.splitext(input_image_path)
- result_image_path = root+'_result_py3'+ext
- 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
- input("test")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement