Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Draw a collection of rectangles.
- from PIL import Image, ImageDraw
- GRID_SIZE = 15
- LINE_WIDTH = 2
- OUT_FILE = "out.png"
- def main():
- rects = map(lambda x: x if len(x) == 4 else x + (1,1), input())
- min_x, max_x, min_y, max_y = 0, 0, 0, 0
- for (x, y, w, h) in rects:
- min_x = min(min_x, x)
- max_x = max(max_x, x+w)
- min_y = min(min_y, y)
- max_y = max(max_y, y+h)
- width = max_x - min_x + 2
- height = max_y - min_y + 2
- im = Image.new("RGB", (GRID_SIZE*width, GRID_SIZE*height), "white")
- draw = ImageDraw.Draw(im)
- for x in range(1,width):
- for i in range(LINE_WIDTH):
- draw.line([GRID_SIZE*x+i,0,GRID_SIZE*x+i,im.size[1]], fill=(0,0,0))
- for y in range(1,height):
- for j in range(LINE_WIDTH):
- draw.line([0,GRID_SIZE*y+j,im.size[0],GRID_SIZE*y+j], fill=(0,0,0))
- for (x, y, w, h) in rects:
- draw.rectangle([(x-min_x+1)*GRID_SIZE+LINE_WIDTH,
- (y-min_y+1)*GRID_SIZE+LINE_WIDTH,
- (x+w-min_x+1)*GRID_SIZE-1,
- (y+h-min_y+1)*GRID_SIZE-1], fill=(255,0,0))
- im = im.transpose(Image.FLIP_TOP_BOTTOM)
- im.save(OUT_FILE)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement