Advertisement
Guest User

Untitled

a guest
Apr 13th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. from PIL import Image
  2. from PIL.ImageColor import getcolor, getrgb
  3. from PIL.ImageOps import grayscale
  4. import time
  5.  
  6. for i in range(1,40):
  7. time.sleep(0.2)
  8. def image_tint(src, tint='#ffffff'):
  9. if Image.isStringType(src): # file path?
  10. src = Image.open(src)
  11. if src.mode not in ['RGB', 'RGBA']:
  12. raise TypeError('Unsupported source image mode: {}'.format(src.mode))
  13. src.load()
  14.  
  15. tr, tg, tb = getrgb(tint)
  16. tl = getcolor(tint, "L") # tint color's overall luminosity
  17. if not tl: tl = 1 # avoid division by zero
  18. tl = float(tl) # compute luminosity preserving tint factors
  19. sr, sg, sb = map(lambda tv: tv/tl, (tr, tg, tb)) # per component
  20. # adjustments
  21. # create look-up tables to map luminosity to adjusted tint
  22. # (using floating-point math only to compute table)
  23. luts = (tuple(map(lambda lr: int(lr*sr + 0.5), range(256))) +
  24. tuple(map(lambda lg: int(lg*sg + 0.5), range(256))) +
  25. tuple(map(lambda lb: int(lb*sb + 0.5), range(256))))
  26. l = grayscale(src) # 8-bit luminosity version of whole image
  27. if Image.getmodebands(src.mode) < 4:
  28. merge_args = (src.mode, (l, l, l)) # for RGB verion of grayscale
  29. else: # include copy of src image's alpha layer
  30. a = Image.new("L", src.size)
  31. a.putdata(src.getdata(3))
  32. merge_args = (src.mode, (l, l, l, a)) # for RGBA verion of grayscale
  33. luts += tuple(range(256)) # for 1:1 mapping of copied alpha values
  34.  
  35. return Image.merge(*merge_args).point(luts)
  36.  
  37.  
  38. if __name__ == '__main__':
  39. objgraph.show_most_common_types(limit=5)
  40. print("-----")
  41. import os
  42.  
  43. input_image_path = 'headbase.png'
  44.  
  45. root, ext = os.path.splitext(input_image_path)
  46. result_image_path = root+'_result_py3'+ext
  47.  
  48. result = image_tint(input_image_path, '#383D2D')
  49. if os.path.exists(result_image_path): # delete any previous result file
  50. os.remove(result_image_path)
  51. result.save(result_image_path) # file name's extension determines format
  52. objgraph.show_most_common_types(limit=5)
  53.  
  54. input("test")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement