import cairocffi as cairo import math def draw_cylinders(width, height, filename): # Setup surface and context surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) ctx = cairo.Context(surface) # White background ctx.set_source_rgb(1, 1, 1) ctx.paint() # Cylinder dimensions cyl_width = 80 cyl_height = 200 top_ellipse_height = 30 spacing = 50 # Starting position start_x = 50 y = 50 for i in range(3): x = start_x + i * (cyl_width + spacing) # 1. Draw Side Gradient (Rect + Arc) # Create a vertical linear gradient: White (top) to Blue (bottom) pat = cairo.LinearGradient(x, y, x+cyl_width, y) pat.add_color_stop_rgb(0, 1, 1, 1) # White top pat.add_color_stop_rgb(1, 0.2, 0.2, 1) # Blue bottom # Draw the rectangle body ctx.rectangle(x, y + top_ellipse_height / 2, cyl_width, cyl_height - top_ellipse_height / 2) ctx.set_source(pat) ctx.fill() # 2. Draw Top Ellipse (White) ctx.save() ctx.translate(x + cyl_width / 2, y + top_ellipse_height / 2) ctx.scale(1, top_ellipse_height / (cyl_width / 2) * 0.5) # Ellipse scale ctx.arc(0, 0, cyl_width / 2, 0, 2 * math.pi) ctx.restore() ctx.set_source_rgb(1, 1, 1) # White ctx.fill_preserve() # Outline for better visibility ctx.set_source_rgb(0, 0, 0) ctx.set_line_width(1) ctx.stroke() # Save to PNG surface.write_to_png(filename) print(f"Saved {filename}") if __name__ == "__main__": draw_cylinders(450, 300, "cylinders.png")