ctx.save() ctx.translate(1, 2.5) Path(ctx).of(path1).fill(Color('blue')) ctx.restore() ctx.save() ctx.translate(2.5, 0.5) ctx.scale(2, 2) Path(ctx).of(path1).fill(Color('orange')).stroke(Color('black'), 0.05) ctx.restore() # Apply the text in various places ctx.save() ctx.translate(0, 0) Path(ctx).of(path2).fill(Color('black')) ctx.restore() ctx.save() ctx.translate(2, 3) Path(ctx).of(path2).stroke(Color('red'), 0.01) ctx.restore() ctx.save() ctx.translate(2, 4) ctx.scale(2, 2) Path(ctx).of(path2).fill(Color('yellow')).stroke(Color('black'), 0.01) ctx.restore() make_image("/tmp/geometry-path.png", draw, 500, 500)
# Author: Martin McBride # Created: 2022-01-10 # Copyright (C) 2022, Martin McBride # License: MIT from generativepy.drawing import make_image, setup from generativepy.color import Color from generativepy.geometry import Circle, Square, Text, Transform def draw(ctx, width, height, frame_no, frame_count): setup(ctx, width, height, background=Color(0.8)) # Create a circular clip region and draw some squares in it with Transform(ctx): Circle(ctx).of_center_radius((190, 190), 100).clip() Square(ctx).of_corner_size((100, 100), 80).fill(Color('red')) Square(ctx).of_corner_size((100, 200), 80).fill(Color('green')) Square(ctx).of_corner_size((200, 100), 80).fill(Color('blue')) Square(ctx).of_corner_size((200, 200), 80).fill(Color('black')) with Transform(ctx): Text(ctx).of("ABC", (150, 350)).font("Times").size(150).align_left().align_top().clip() circles = [(200, 380, 'orange'), (200, 450, 'cyan'), (300, 380, 'green'), (300, 450, 'purple'), (400, 380, 'yellow'), (400, 450, 'blue')] for x, y, color in circles: Circle(ctx).of_center_radius((x, y), 70).fill(Color(color)) make_image("clip-tutorial.png", draw, 500, 500)
def creator(file): make_image(file, draw, 500, 350, channels=3)
# Same as before, but a hue of 0.33 gives the equivalent green colour ctx.set_source_rgba(*Color.of_hsl(0.33, 0.5, 0.5)) ctx.rectangle(200, 50, 100, 100) ctx.fill() # Same as before, but a hue of 0.66 gives the equivalent blue colour ctx.set_source_rgba(*Color.of_hsl(0.66, 0.5, 0.5)) ctx.rectangle(350, 50, 100, 100) ctx.fill() ctx.set_source_rgba(*Color.of_hsl(0, 0.25, 0.5)) ctx.rectangle(50, 200, 100, 100) ctx.fill() ctx.set_source_rgba(*Color.of_hsl(0, 0.5, 0.25)) ctx.rectangle(200, 200, 100, 100) ctx.fill() ctx.set_source_rgba(*Color.of_hsl(0, 0.5, 0.5)) ctx.rectangle(330, 180, 100, 100) ctx.fill() # Here we create a rectangle with a colour Color.of_hsla(0.5, 0.5, 0.5, 0.3). # That is blue-green with a 0.3 alpha value (30% opacity, ie 70% transparent). # This overlaps the previous rectangle, and you can see the background through it. ctx.set_source_rgba(*Color.of_hsla(0.5, 0.5, 0.5, 0.3)) ctx.rectangle(370, 220, 100, 100) ctx.fill() make_image("/tmp/hslcolor.png", draw, 500, 350)
def creator(file): make_image(file, draw_rgba, 400, 400, channels=4)
from generativepy.drawing import make_image, setup from generativepy.color import Color from generativepy.geometry import Rectangle def draw_rgb(ctx, pixel_width, pixel_height, frame_no, frame_count): setup(ctx, pixel_width, pixel_height, background=Color("cornflowerblue")) pos = [10, 10] w = 100 h = 100 Rectangle(ctx).of_corner_size(pos, w, h).fill(Color(0, 0, 0)) pos[0] += w Rectangle(ctx).of_corner_size(pos, w, h).fill(Color(0.5, 0, 0)) pos[0] += w Rectangle(ctx).of_corner_size(pos, w, h).fill(Color(1, 0, 0)) pos[0] += w Rectangle(ctx).of_corner_size(pos, w, h).fill(Color(0.5, 1, 0)) pos[0] += w Rectangle(ctx).of_corner_size(pos, w, h).fill(Color(0.5, 0, 1)) pos[0] += w Rectangle(ctx).of_corner_size(pos, w, h).fill(Color(0, 1, .5)) pos[0] += w Rectangle(ctx).of_corner_size(pos, w, h).fill(Color(0.5, 0.5, 0.5)) pos[0] += w make_image("colour-rgb.png", draw_rgb, 720, 120)
def creator(file): make_image(file, draw, 600, 500)
from generativepy.geometry import text from generativepy.color import Color from generativepy.utils import temp_file ''' Illustrates simple use of make_image ''' def draw(ctx, pixel_width, pixel_height, frame_no, frame_count): setup(ctx, pixel_width, pixel_height, width=5, background=Color(0.4)) ctx.set_source_rgba(*Color(0.5, 0, 0)) ctx.rectangle(0.5, 0.5, 2.5, 1.5) ctx.fill() ctx.set_source_rgba(*Color(0, 0.75, 0, 0.5)) ctx.rectangle(2, 0.25, 2.5, 1) ctx.fill() text(ctx, "simple-make-image", 1, 3, size=0.2, color=Color('cadetblue'), font='Arial') make_image(temp_file("simple-make-image.png"), draw, 500, 400)
from generativepy.drawing import make_image, setup from generativepy.color import Color from generativepy.geometry import Square, square ''' Create squares using the geometry module. ''' def draw(ctx, width, height, frame_no, frame_count): setup(ctx, width, height, width=5, background=Color(0.8)) # The square function is a convenience function that adds a square as a new the path. # You can fill or stroke it as you wish. square(ctx, (1, 1), 1) ctx.set_source_rgba(*Color(1, 0, 0)) ctx.fill() # Square objects can be filled, stroked, filled and stroked. Square(ctx).of_corner_size((3, 1), 1).fill(Color(0, .5, 0)) Square(ctx).of_corner_size((1, 3), 1).stroke(Color(0, .5, 0), 0.1) Square(ctx).of_corner_size((3, 3), 1).fill(Color(0, 0, 1)).stroke(Color(0), 0.2) make_image("/tmp/geometry-squares.png", draw, 500, 500)
# Author: Martin McBride # Created: 2022-01-10 # Copyright (C) 2022, Martin McBride # License: MIT from generativepy.drawing import make_image, setup from generativepy.color import Color from generativepy.geometry import Rectangle, Circle def draw_rect(ctx, width, height, frame_no, frame_count): setup(ctx, width, height, width=5, background=Color(0.4)) color = Color(1, 0.5, 0) Rectangle(ctx).of_corner_size((1, 1.5), 2.5, 2).fill(color) make_image("user-scale.png", draw_rect, 500, 400)
pixel_width, pixel_height, width=width, startx=-width / 2, starty=-width / 2, background=Color(1)) a = 16 b = 9 d = 6 Polygon(ctx).of_points(create_spiro_square(a, b, d)).fill( Color('gold'), fill_rule=EVEN_ODD).stroke(Color('darkblue'), line_width=0.1) make_image("spirograph-square.png", draw, 600, 600) def draw2(ctx, pixel_width, pixel_height, frame_no, frame_count): width = 32 setup(ctx, pixel_width, pixel_height, width=width, startx=-width / 2, starty=-width / 2, background=Color(1)) a = 16 b = 9
from generativepy.color import Color ''' Create colour squares that mix different amounts if r, g, b ''' def draw(ctx, width, height, frame_no, frame_count): ctx.set_source_rgba(*Color(1)) ctx.paint() for i in range(200): for j in range(200): ctx.set_source_rgba(*Color(i / 200, j / 200, 0)) ctx.rectangle(i + 50, j + 50, 1, 1) ctx.fill() for i in range(200): for j in range(200): ctx.set_source_rgba(*Color(i / 200, 0, j / 200)) ctx.rectangle(i + 50, j + 300, 1, 1) ctx.fill() for i in range(200): for j in range(200): ctx.set_source_rgba(*Color(0, i / 200, j / 200)) ctx.rectangle(i + 50, j + 550, 1, 1) ctx.fill() make_image("/tmp/rgbcolor.png", draw, 300, 800)
# The ellipse function is a convenience function that adds a ellipse as a new the path. # You can fill or stroke it as you wish. ellipse(ctx, (1, 1), 0.7, 1.1) ctx.set_source_rgba(*Color(1, 0, 0)) ctx.fill() # Ellipse objects can be filled, stroked, filled and stroked. Ellipse(ctx).of_center_radius( (2.5, 1), 0.7, 0.3).fill(Color(0, 0, 1)).stroke(Color(0), 0.05) Ellipse(ctx).of_center_radius( (4, 1), 0.7, 0.3).as_arc(0, 1).stroke(Color(0, 0.5, 0), 0.05) Ellipse(ctx).of_center_radius( (1, 2.5), 0.7, 0.3).as_sector(1, 3).stroke(Color('orange'), 0.05) Ellipse(ctx).of_center_radius( (2.5, 2.5), 0.7, 0.3).as_sector(2, 4.5).fill(Color('cadetblue')) Ellipse(ctx).of_center_radius( (4, 2.5), 0.7, 0.3).as_sector(2.5, 6).fill(Color('yellow')).stroke(Color('magenta'), 0.05) Ellipse(ctx).of_center_radius( (1, 4), 0.7, 0.3).as_segment(1, 3).stroke(Color('orange'), 0.05) Ellipse(ctx).of_center_radius( (2.5, 4), 0.7, 0.3).as_segment(2, 4.5).fill(Color('cadetblue')) Ellipse(ctx).of_center_radius((4, 4), 0.7, 0.3).as_segment(2.5, 6).fill( Color('yellow')).stroke(Color('magenta'), 0.05) make_image("/tmp/geometry-ellipses.png", draw, 500, 500)
# Author: Martin McBride # Created: 2021-11-07 # Copyright (C) 2021, Martin McBride # License: MIT # Create a simple linear gradient with several circle shapes from generativepy.drawing import make_image, setup from generativepy.color import Color from generativepy.geometry import Circle, LinearGradient def draw(ctx, pixel_width, pixel_height, frame_no, frame_count): setup(ctx, pixel_width, pixel_height, background=Color(0.4)) gradient = LinearGradient().of_points( (150, 150), (350, 250)).with_start_end(Color('yellow'), Color('red')).build() Circle(ctx).of_center_radius((100, 100), 75).fill(gradient) Circle(ctx).of_center_radius((270, 100), 75).fill(gradient) Circle(ctx).of_center_radius((150, 300), 75).fill(gradient) Circle(ctx).of_center_radius((400, 300), 75).fill(gradient) make_image("circles-linear-gradient.png", draw, 500, 400)
from generativepy import graph from generativepy.drawing import make_image, setup from generativepy.color import Color from generativepy.graph import Axes, Plot ''' Create a simple graph ''' def draw(ctx, width, height, frame_no, frame_count): setup(ctx, width, height, background=Color(1)) # Creates a set of axes. # Use the default size of 10 units, but offset the start toplace the origin inthe centre axes = Axes(ctx, (50, 50), 500, 500).of_start((-5, -5)) axes.draw() # Add various curves axes.clip() Plot(axes).of_function(lambda x: x * x).stroke(pattern=Color('red')) Plot(axes).of_xy_function(lambda x: 1.5**x).stroke(pattern=Color('green')) Plot(axes).of_polar_function(lambda x: 2 * x).stroke(pattern=Color('blue')) axes.unclip() make_image("/tmp/simplegraph.png", draw, 500, 500)
# License: MIT from generativepy.drawing import make_image, setup from generativepy.color import Color from generativepy.geometry import Rectangle def draw_lerp(ctx, pixel_width, pixel_height, frame_no, frame_count): setup(ctx, pixel_width, pixel_height, background=Color('cornflowerblue')) color1 = Color('red') color2 = Color('blue') pos = [10, 10] w = 100 h = 100 Rectangle(ctx).of_corner_size(pos, w, h).fill(color1.lerp(color2, 0)) pos[0] += w Rectangle(ctx).of_corner_size(pos, w, h).fill(color1.lerp(color2, 0.25)) pos[0] += w Rectangle(ctx).of_corner_size(pos, w, h).fill(color1.lerp(color2, 0.5)) pos[0] += w Rectangle(ctx).of_corner_size(pos, w, h).fill(color1.lerp(color2, 0.75)) pos[0] += w Rectangle(ctx).of_corner_size(pos, w, h).fill(color1.lerp(color2, 1)) pos[0] += w make_image("colour-lerp.png", draw_lerp, 520, 120)
from generativepy.drawing import make_image, setup from generativepy.color import Color from generativepy.geometry import triangle, Triangle ''' Create triangles using the geometry module. ''' def draw(ctx, width, height, frame_no, frame_count): setup(ctx, width, height, width=500, background=Color(0.8)) # The triangle function is a convenience function that adds a triangle as a new path. # You can fill or stroke it as you wish. triangle(ctx, (100, 100), (150, 50), (200, 150)) ctx.set_source_rgba(*Color(1, 0, 0)) ctx.fill() Triangle(ctx).of_corners((300, 100), (300, 150), (400, 200)).stroke(Color('orange'), 10) make_image("/tmp/geometry-triangle.png", draw, 500, 500)
from generativepy.geometry import Circle, Square, Text ''' Create bezier curve using the geometry module. ''' def draw(ctx, width, height, frame_no, frame_count): setup(ctx, width, height, width=5, background=Color(0.8)) # Create a circular clip region and draw some squares in it ctx.save() Circle(ctx).of_center_radius((1.9, 1.9), 1).clip() Square(ctx).of_corner_size((1, 1), .8).fill(Color('red')) Square(ctx).of_corner_size((1, 2), .8).fill(Color('green')) Square(ctx).of_corner_size((2, 1), .8).fill(Color('blue')) Square(ctx).of_corner_size((2, 2), .8).fill(Color('black')) ctx.restore() ctx.save() Text(ctx).of( "ABC", (1.5, 3.5)).font("Times").size(1.5).align_left().align_top().clip() circles = [(2, 3.8, 'orange'), (2, 4.5, 'cyan'), (3, 3.8, 'green'), (3, 4.5, 'purple'), (4, 3.8, 'yellow'), (4, 4.5, 'blue')] for x, y, color in circles: Circle(ctx).of_center_radius((x, y), 0.7).fill(Color(color)) ctx.restore() make_image("/tmp/geometry-clip.png", draw, 500, 500)
def creator(file): make_image(file, draw, 800, 800)
# Author: Martin McBride # Created: 2021-11-07 # Copyright (C) 2021, Martin McBride # License: MIT # Create a simple linear gradient from generativepy.drawing import make_image, setup from generativepy.color import Color from generativepy.geometry import Rectangle, LinearGradient def draw(ctx, pixel_width, pixel_height, frame_no, frame_count): setup(ctx, pixel_width, pixel_height, background=Color(0.4)) gradient = LinearGradient().of_points((150, 150), (350, 250)).with_start_end(Color('yellow'), Color('red')).build() Rectangle(ctx).of_corner_size((150, 150), 200, 100).fill(gradient) make_image("simple-linear-gradient.png", draw, 500, 400)
col = Color("cadetblue") pos = [10, 10] w = 100 h = 100 Rectangle(ctx).of_corner_size(pos, w, h).fill(col.with_g_factor(0.8)) pos[0] += w Rectangle(ctx).of_corner_size(pos, w, h).fill(col) pos[0] += w Rectangle(ctx).of_corner_size(pos, w, h).fill(col.with_g_factor(1.2)) pos = [10, 120] Rectangle(ctx).of_corner_size(pos, w, h).fill(col.with_l_factor(0.6)) pos[0] += w Rectangle(ctx).of_corner_size(pos, w, h).fill(col) pos[0] += w Rectangle(ctx).of_corner_size(pos, w, h).fill(col.with_l_factor(1.4)) pos = [10, 230] Rectangle(ctx).of_corner_size(pos, w, h).fill(col.with_s_factor(0.6)) pos[0] += w Rectangle(ctx).of_corner_size(pos, w, h).fill(col) pos[0] += w Rectangle(ctx).of_corner_size(pos, w, h).fill(col.with_s_factor(1.4)) make_image("colour-delta.png", draw_delta, 320, 340)
ctx.save() ctx.translate(0, 200) Circle(ctx).of_center_radius((20, 20), 10).as_arc(math.pi, math.pi * 3 / 2).add() Circle(ctx).of_center_radius((60, 20), 10).as_arc(math.pi * 3 / 2, 0).extend_path().add() Circle(ctx).of_center_radius( (60, 60), 10).as_arc(0, math.pi / 2).extend_path().add() Circle(ctx).of_center_radius( (20, 60), 10).as_arc(math.pi / 2, math.pi).extend_path().fill( Color('red')).stroke(Color('blue'), 5) ctx.restore() # roundrect closed ctx.save() ctx.translate(100, 200) Circle(ctx).of_center_radius((20, 20), 10).as_arc(math.pi, math.pi * 3 / 2).add() Circle(ctx).of_center_radius((60, 20), 10).as_arc(math.pi * 3 / 2, 0).extend_path().add() Circle(ctx).of_center_radius( (60, 60), 10).as_arc(0, math.pi / 2).extend_path().add() Circle(ctx).of_center_radius( (20, 60), 10).as_arc(math.pi / 2, math.pi).extend_path(close=True).fill( Color('red')).stroke(Color('blue'), 5) ctx.restore() make_image("/tmp/geometry-complex-paths.png", draw, 600, 600)
from generativepy.drawing import make_image, setup from generativepy.geometry import text from generativepy.color import Color ''' Create a very simple image ''' def draw(ctx, pixel_width, pixel_height, frame_no, frame_count): setup(ctx, pixel_width, pixel_height, width=5, background=Color(0.4)) ctx.set_source_rgba(*Color(0.5, 0, 0)) ctx.rectangle(0.5, 0.5, 2.5, 1.5) ctx.fill() ctx.set_source_rgba(*Color(0, 0.75, 0, 0.5)) ctx.rectangle(2, 0.25, 2.5, 1) ctx.fill() text(ctx, "Simple Image", 1, 3, size=0.5, color=Color('cadetblue'), font='Arial') make_image("/tmp/simpleimage.png", draw, 500, 400)
from generativepy.drawing import make_image, setup, EVEN_ODD from generativepy.color import Color from generativepy.geometry import Polygon import math def draw(ctx, pixel_width, pixel_height, frame_no, frame_count): setup(ctx, pixel_width, pixel_width, background=Color(0.8)) black = Color(0) red = Color('red') Polygon(ctx).of_points([ (150, 50), (100, 250), (250, 150), (50, 150), (200, 250), ]).fill(red).stroke(black, 5) Polygon(ctx).of_points([ (450, 50), (400, 250), (550, 150), (350, 150), (500, 250), ]).fill(red, fill_rule=EVEN_ODD).stroke(black, 5) make_image("complex-polygon.png", draw, 700, 300)
"Text", (450, 90)).font("Times").size(20).align_right().align_baseline().fill( Color('blue')) Circle(ctx).of_center_radius((190, 200), 2).fill(Color(0, 0, 1)) Text(ctx).of( "gTop", (200, 200)).font("Times").size(20).align_left().align_top().fill( Color('black')) Circle(ctx).of_center_radius((190, 250), 2).fill(Color(0, 0, 1)) Text(ctx).of( "gMid", (200, 250)).font("Times").size(20).align_left().align_middle().fill( Color('black')) Circle(ctx).of_center_radius((190, 300), 2).fill(Color(0, 0, 1)) Text(ctx).of( "gBase", (200, 300)).font("Times").size(20).align_left().align_baseline().fill( Color('black')) Circle(ctx).of_center_radius((190, 350), 2).fill(Color(0, 0, 1)) Text(ctx).of( "gBottom", (200, 350)).font("Times").size(20).align_left().align_bottom().fill( Color('black')) make_image("text-align.png", draw, 500, 400)
from generativepy.geometry import Rectangle, Square, Triangle, Polygon, Line def draw(ctx, pixel_width, pixel_height, frame_no, frame_count): setup(ctx, pixel_width, pixel_height, background=Color(0.8)) red = Color('red') green = Color('green') blue = Color('blue') thickness = 2 Line(ctx).of_start_end((150, 150), (50, 50)).stroke(red, thickness) Line(ctx).of_start_end((300, 150), (200, 50)).as_ray().stroke(red, thickness) Line(ctx).of_start_end((450, 150), (350, 50)).as_line().stroke(red, thickness) Triangle(ctx).of_corners((50, 200), (150, 200), (125, 300)).stroke(green, thickness) Square(ctx).of_corner_size((200, 200), 100).stroke(green, thickness) Rectangle(ctx).of_corner_size((350, 200), 100, 75).stroke(green, thickness) Polygon(ctx).of_points([(50, 350), (250, 400), (250, 500), (50, 375)]).stroke(blue, thickness) Polygon(ctx).of_points([(300, 350), (500, 400), (500, 500), (300, 375)]).open().stroke(blue, thickness) make_image("polygons-tutorial.png", draw, 550, 550)
def creator(file): make_image(file, draw, 600, 700, channels=3)
# License: MIT from generativepy.drawing import make_image, setup from generativepy.color import Color from generativepy.geometry import Rectangle, Transform def draw(ctx, pixel_width, pixel_height, frame_no, frame_count): setup(ctx, pixel_width, pixel_height, background=Color(0.8)) blue = Color('blue') red = Color('red') thickness = 2 Rectangle(ctx).of_corner_size((10, 10), 200, 150).stroke(blue, thickness) with Transform(ctx).translate(0, 200): Rectangle(ctx).of_corner_size((10, 10), 200, 150).stroke(red, thickness) with Transform(ctx) as t: Rectangle(ctx).of_corner_size((250, 100), 50, 150).fill(blue) t.translate(60, 10) Rectangle(ctx).of_corner_size((250, 100), 50, 150).fill(red) t.translate(60, 10) Rectangle(ctx).of_corner_size((250, 100), 50, 150).fill(red) make_image("translate-tutorial.png", draw, 450, 400)
def creator(file): make_image(file, draw, 300, 800, channels=3)
from generativepy.geometry import Rectangle, Transform, Circle def draw(ctx, pixel_width, pixel_height, frame_no, frame_count): setup(ctx, pixel_width, pixel_height, background=Color(0.8)) blue = Color('blue') red = Color('red') green = Color('green') thickness = 8 Rectangle(ctx).of_corner_size((50, 40), 100, 30).fill(blue) with Transform(ctx).scale(1.5, 2): Rectangle(ctx).of_corner_size((50, 40), 100, 30).fill(red) with Transform(ctx) as t: Circle(ctx).of_center_radius((220, 260), 5).fill(green) Rectangle(ctx).of_corner_size((20, 160), 400, 200).stroke(blue, thickness) t.scale(0.5, 0.5, (220, 260)) Rectangle(ctx).of_corner_size((20, 160), 400, 200).stroke(red, thickness) t.scale(0.5, 0.5, (220, 260)) Rectangle(ctx).of_corner_size((20, 160), 400, 200).stroke(red, thickness) make_image("scale-tutorial.png", draw, 450, 400)