Computer Graphics with Pycairo Lecture 2: Setting up Pycairo
Objectives - Install Pycairo and verify installation - Understand Surfaces and Contexts - Write a basic Pycairo program - Test output with a simple drawing
Installing Pycairo 1. Ensure Python 3 is installed 2. Install pip if needed 3. Run: pip install pycairo 4. Verify installation: python -m pip show pycairo
Understanding Contexts - Context is the drawing environment - Provides commands: move_to, line_to, arc, stroke, fill - Created from a surface: ctx = cairo.Context(surface)
Example Program: Drawing a Line import cairo WIDTH, HEIGHT = 300, 300 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT) ctx = cairo.Context(surface) # Draw diagonal line ctx.move_to(50,50) ctx.line_to(250,250) ctx.set_source_rgb(1,0,0) # red ctx.set_line_width(5) ctx.stroke() surface.write_to_png('line.png')
Expected Output - Creates a 300x300 PNG image - Red diagonal line from top-left to bottom-right - Saved as 'line.png'
Summary - Installed Pycairo and verified installation - Learned about Surfaces and Contexts - Ran a basic drawing program - Ready for more complex shapes in next lectures