I want to draw random shapes in random colors in an image using python. Is there any module which exists in python to draw random shapes like polygons of different sizes and shapes and mixed together to form a new shape etc
Thanks a lot
Being unsure of what you are trying to accomplish, the following code is a simple example of how to generate random shapes and display them in a window. It create a tkinter root object, sets up a Canvas for display, and randomly creates and shows polygons for each second the program is run.
from tkinter import *
from random import *
class Application(Canvas):
X_OFFSET = 40
Y_OFFSET = 40
FILL = 'blue'
LINE = 'black'
@classmethod
def main(cls):
root = Tk()
surface = cls(root)
surface.grid()
surface.after_idle(surface.draw_shape)
root.mainloop()
def draw_shape(self):
x = randrange(int(self['width']) - self.X_OFFSET)
y = randrange(int(self['height']) - self.Y_OFFSET)
points = [(x + randrange(self.X_OFFSET), y + randrange(self.Y_OFFSET))
for point in range(randint(3, 10))]
self.create_polygon(points, fill=self.FILL, outline=self.LINE)
self.after(1000, self.draw_shape)
if __name__ == '__main__':
Application.main()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With