Advertisement
Guest User

Colors

a guest
Sep 22nd, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. '''
  2. A couple of simple image filter demos using PIL/Pillow.
  3. '''
  4.  
  5. from PIL import Image, ImageOps, ImageFilter
  6. from PIL.Image import BILINEAR
  7. from math import sqrt, sin, cos, atan2
  8. import dialogs
  9. import photos
  10.  
  11. def sketch(img):
  12. edge_img = img.filter(ImageFilter.CONTOUR)
  13. return ImageOps.grayscale(edge_img)
  14.  
  15. def emboss(img):
  16. edge_img = img.filter(ImageFilter.EMBOSS)
  17. return ImageOps.grayscale(edge_img)
  18.  
  19. def color_tiles(img):
  20. size = img.size
  21. small_img = img.resize((int(size[0]/2), int(size[1]/2)), BILINEAR)
  22. bw_img = small_img.convert('1', dither=False)
  23. gray_img = bw_img.convert('L')
  24. result = Image.new('RGB', size)
  25. tile1 = ImageOps.colorize(gray_img, 'green', 'red')
  26. tile2 = ImageOps.colorize(gray_img, 'purple', 'yellow')
  27. tile3 = ImageOps.colorize(gray_img, 'yellow', 'brown')
  28. tile4 = ImageOps.colorize(gray_img, 'red', 'cyan')
  29. result.paste(tile1, (0, 0))
  30. result.paste(tile2, (int(size[0]/2), 0))
  31. result.paste(tile3, (0, int(size[1]/2)))
  32. result.paste(tile4, (int(size[0]/2), int(size[1]/2)))
  33. return result
  34.  
  35. def main():
  36. effect = dialogs.alert('Select Effect', '', 'Sketch', 'Emboss', 'Color Tiles')
  37. i = dialogs.alert('Image', '', 'Demo Image', 'Select from Photos')
  38. if i == 1:
  39. img = Image.open('test:Lenna')
  40. else:
  41. img = photos.pick_image()
  42. if effect == 1:
  43. sketch(img).show()
  44. elif effect == 2:
  45. emboss(img).show()
  46. else:
  47. color_tiles(img).show()
  48. print('Tip: You can tap and hold the image to save it to your photo library.')
  49.  
  50. if __name__ == '__main__':
  51. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement