Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. from PIL import Image
  2.  
  3. import random
  4.  
  5. def random_color():
  6. '''Returns a random RGB three-tuple, where each element of
  7. the tuple is a random value from the inclusive interval 0 ... 255.
  8. '''
  9. redness= random.randrange(0,256)
  10. greenness = random.randrange(0,256)
  11. blueness= random.randrange(0,256)
  12. RGB = (redness, greenness, blueness)
  13. return RGB
  14.  
  15. def get_width(drawing):
  16. '''Returns the width of the Image represented by drawing'''
  17. width,height = drawing.size
  18. w = width
  19. return w
  20.  
  21. def get_height(drawing):
  22. '''Returns the height of the Image represented by drawing'''
  23. width,height = drawing.size
  24. h = height
  25. return h
  26.  
  27. def left(spot):
  28. '''Returns the coordinate immediately left of spot;
  29. that is, its x-coordinate is one less than that of spot
  30. and its y-coordinate is the same as that of spot.
  31. '''
  32. (spotx, spoty) = spot
  33. lspotx = spotx - 1
  34. lspoty = spoty
  35. lspot = (lspotx,lspoty)
  36. return lspot
  37.  
  38. def right(spot):
  39. '''Returns the coordinate immediately right of spot;
  40. that is, its x-coordinate is one greater than that of spot
  41. and its y-coordinate is the same as that of spot.
  42. '''
  43. (spotx, spoty) = spot
  44. rspotx = spotx + 1
  45. rspoty = spoty
  46. rspot = (rspotx, rspoty)
  47. return rspot
  48.  
  49. def above(spot):
  50. '''Returns the coordinate immediately above spot;
  51. that is, its x-coordinate is the same as that of spot
  52. and its y-coordinate is one less than that of spot.
  53. '''
  54. (spotx, spoty) = spot
  55. aspotx = spotx
  56. aspoty = spoty - 1
  57. aspot = (aspotx, aspoty)
  58. return aspot
  59.  
  60. def below(spot):
  61. '''Returns the coordinate immediately above spot;
  62. that is, its x-coordinate is the same as that of spot
  63. and its y-coordinate is one greater than that of spot.
  64. '''
  65. (spotx, spoty) = spot
  66. bspotx = spotx
  67. bspoty = spoty + 1
  68. bspot = (bspotx, bspoty)
  69. return bspot
  70.  
  71. def is_inbounds( drawing, spot ):
  72. '''Returns True or False whether coordinate spot is inbounds
  73. on the Image represented by drawing
  74. '''
  75. (spotx, spoty) = spot
  76. if spotx <0 or spoty <0:
  77. return False
  78. if spotx >= get_width(drawing):
  79. return False
  80. if spoty >= get_height(drawing):
  81. return False
  82. else:
  83. return True
  84.  
  85. def get_color( drawing, spot ):
  86. '''If coordinate spot is inbounds on the Image represented by drawing,
  87. the function returns the color at spot in the drawing;
  88. otherwise, the function returns None
  89. '''
  90. if is_inbounds(drawing, spot) == True:
  91. return drawing.getpixel(spot)
  92. else:
  93. return None
  94.  
  95. def is_colorable(drawing, spot, bg=(255,255,255)):
  96. '''Returns True or False whether coordinate spot is a colorable pixel
  97. on the Image represented by drawing.
  98. To be colorable spot must be inbounds and equal to the background color bg.
  99. '''
  100. if is_inbounds(drawing, spot) == True:
  101. if get_color(drawing, spot) == bg:
  102. return True
  103. else:
  104. return False
  105.  
  106. def paint( drawing, spot, c, bg=(255,255,255) ):
  107. if is_colorable(drawing, spot, bg=(255,255,255)) == False:
  108. pass
  109. else:
  110. c = drawing.putpixel(spot, get_color(drawing, spot))
  111. return
  112.  
  113. if __name__ == '__main__':
  114. import url_to_image
  115. link = input( 'Enter web picture location: ' )
  116. original = url_to_image.get( link )
  117. image = paint(original, (6,6), random_color(), bg=(255,255,255) )
  118. image.save( 'img-paint.png', 'PNG' )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement