Lecture2_Setup_Pycairo_Detailed----.pptx

kamalsmail1 0 views 8 slides Oct 08, 2025
Slide 1
Slide 1 of 8
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8

About This Presentation

Lecture2


Slide Content

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 Surfaces - Surfaces are drawing targets - Types of surfaces: • ImageSurface (PNG) • PDFSurface (PDF) • SVGSurface (SVG) • RecordingSurface (memory) - Example: cairo.ImageSurface(cairo.FORMAT_ARGB32, 300, 300)

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
Tags