Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image
- import random
- def random_color():
- '''Returns a random RGB three-tuple, where each element of
- the tuple is a random value from the inclusive interval 0 ... 255.
- '''
- redness= random.randrange(0,256)
- greenness = random.randrange(0,256)
- blueness= random.randrange(0,256)
- RGB = (redness, greenness, blueness)
- return RGB
- def get_width(drawing):
- '''Returns the width of the Image represented by drawing'''
- width,height = drawing.size
- w = width
- return w
- def get_height(drawing):
- '''Returns the height of the Image represented by drawing'''
- width,height = drawing.size
- h = height
- return h
- def left(spot):
- '''Returns the coordinate immediately left of spot;
- that is, its x-coordinate is one less than that of spot
- and its y-coordinate is the same as that of spot.
- '''
- (spotx, spoty) = spot
- lspotx = spotx - 1
- lspoty = spoty
- lspot = (lspotx,lspoty)
- return lspot
- def right(spot):
- '''Returns the coordinate immediately right of spot;
- that is, its x-coordinate is one greater than that of spot
- and its y-coordinate is the same as that of spot.
- '''
- (spotx, spoty) = spot
- rspotx = spotx + 1
- rspoty = spoty
- rspot = (rspotx, rspoty)
- return rspot
- def above(spot):
- '''Returns the coordinate immediately above spot;
- that is, its x-coordinate is the same as that of spot
- and its y-coordinate is one less than that of spot.
- '''
- (spotx, spoty) = spot
- aspotx = spotx
- aspoty = spoty - 1
- aspot = (aspotx, aspoty)
- return aspot
- def below(spot):
- '''Returns the coordinate immediately above spot;
- that is, its x-coordinate is the same as that of spot
- and its y-coordinate is one greater than that of spot.
- '''
- (spotx, spoty) = spot
- bspotx = spotx
- bspoty = spoty + 1
- bspot = (bspotx, bspoty)
- return bspot
- def is_inbounds( drawing, spot ):
- '''Returns True or False whether coordinate spot is inbounds
- on the Image represented by drawing
- '''
- (spotx, spoty) = spot
- if spotx <0 or spoty <0:
- return False
- if spotx >= get_width(drawing):
- return False
- if spoty >= get_height(drawing):
- return False
- else:
- return True
- def get_color( drawing, spot ):
- '''If coordinate spot is inbounds on the Image represented by drawing,
- the function returns the color at spot in the drawing;
- otherwise, the function returns None
- '''
- if is_inbounds(drawing, spot) == True:
- return drawing.getpixel(spot)
- else:
- return None
- def is_colorable(drawing, spot, bg=(255,255,255)):
- '''Returns True or False whether coordinate spot is a colorable pixel
- on the Image represented by drawing.
- To be colorable spot must be inbounds and equal to the background color bg.
- '''
- if is_inbounds(drawing, spot) == True:
- if get_color(drawing, spot) == bg:
- return True
- else:
- return False
- def paint( drawing, spot, c, bg=(255,255,255) ):
- if is_colorable(drawing, spot, bg=(255,255,255)) == False:
- pass
- else:
- c = drawing.putpixel(spot, get_color(drawing, spot))
- return
- if __name__ == '__main__':
- import url_to_image
- link = input( 'Enter web picture location: ' )
- original = url_to_image.get( link )
- image = paint(original, (6,6), random_color(), bg=(255,255,255) )
- image.save( 'img-paint.png', 'PNG' )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement