Advertisement
Guest User

Untitled

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