Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image, ImageDraw
- #stars
- import random
- star_list = []
- def generate_star_list(nstars, sky_width, sky_height):
- star_list = []
- for i in range(nstars):
- x = random.randint(0, sky_width)
- y = random.randint(0, sky_height)
- star_list.append((x,y))
- return star_list
- star_list = generate_star_list(80, 350, 230)
- def crop_image(img):
- img2 = img.copy()
- color_with_alpha = (0,0,0,0)
- for x in range(img2.width):
- for y in range(img2.height):
- r, g, b, a = img.getpixel((x, y))
- if r == 0 and g == 0 and b == 0:
- img2.putpixel((x,y), color_with_alpha)
- return img2
- # TODO: replace width, height, and bgcolor with correct values
- img = Image.new('RGBA', (350, 300), (3, 20, 41))
- # TODO: create instance of ImageDraw object
- draw = ImageDraw.Draw(img)
- #stars
- n = 0
- for i in range(len(star_list)):
- draw.circle((star_list[n]), 1, fill = (245, 244, 229))
- n += 1
- #house
- draw.rectangle((50,170,149,229), "firebrick")
- #grasslines
- for i in range (0, 350):
- if i % 2 == 0:
- draw.line((i, 230, i, 226), fill = (16,94,12), width = 1)
- #moon
- draw.circle((275, 75), 30, fill = (247, 234, 206))
- #grass
- draw.rectangle((0,230,349,300), "green")
- #brick bg
- draw.rectangle((90, 230, 109, 300), (75, 75, 75))
- # darkbricks
- y1 = 214
- y2 = 221
- for i in range(0, 8):
- y1 += 16
- y2 += 16
- draw.rectangle((90, y1, 93, y2), (50, 50, 50))
- draw.rectangle((98, y1, 101, y2), (50, 50, 50))
- draw.rectangle((106, y1, 109, y2), (50, 50, 50))
- draw.rectangle((94, y1+8, 97, y2+8), (50, 50, 50))
- draw.rectangle((102, y1+8, 105, y2+8), (50, 50, 50))
- #door
- draw.rectangle((90,190,109,229), "blue")
- #roof
- draw.polygon((50,169,149,169,100,140), "blue")
- #image copy
- with img.copy() as im:
- temp_img = im.copy()
- #bird
- with Image.open("bird.png") as im:
- resized_bird_image = im.resize((50,50))
- cropped_bird_img = crop_image(resized_bird_image)
- Image.Image.alpha_composite(img, resized_bird_image, (0, 120))
- #image copy
- with img.copy() as im:
- temp_img = im.copy()
- #bird
- with Image.open("bird.png") as im:
- resized_bird_image = im.resize((50,50))
- cropped_bird_img = crop_image(resized_bird_image)
- frames = []
- for i in range(0, 120):
- frames.append(Image.Image.alpha_composite(img, resized_bird_image, (0, i)))
- # Make sure you keep img.show in this main block
- if __name__ == "__main__":
- img.show()
- # This will save the gif from the frames list
- # DO NOT MODIFY!
- if __name__ == "__main__":
- frames[0].save('house-scene.gif',
- save_all=True,
- append_images=frames[1:],
- disposal=2,
- fps=20,
- loop=0)
Advertisement
Add Comment
Please, Sign In to add comment