Python Cairo庫(kù)的使用教程及代碼案例
一、Cairo 簡(jiǎn)介
Cairo 是一個(gè)2D矢量圖形庫(kù),支持多種輸出設(shè)備(如PNG、PDF、SVG、窗口系統(tǒng))。PyCairo 是 Cairo 的 Python 綁定,允許通過(guò) Python 代碼實(shí)現(xiàn)高質(zhì)量的圖形繪制。
二、安裝 PyCairo
# 通過(guò) pip 安裝(注意包名可能是 pycairo 或 cairo) pip install pycairo
三、基礎(chǔ)概念與核心類
Surface(表面)
表示繪圖的目標(biāo)介質(zhì)(如內(nèi)存、文件、窗口)。常見(jiàn)類型:ImageSurface
: 位圖圖像(如PNG)PDFSurface
: PDF 文件SVGSurface
: SVG 矢量圖
Context(上下文)
控制繪圖操作的核心對(duì)象,管理路徑、顏色、變換等繪圖狀態(tài)。
四、基礎(chǔ)繪圖方法
示例1:繪制簡(jiǎn)單圖形(PNG)
import cairo # 1. 創(chuàng)建 Surface 和 Context width, height = 400, 300 surface = cairo.ImageSurface(cairo.Format.ARGB32, width, height) ctx = cairo.Context(surface) # 2. 設(shè)置背景色 ctx.set_source_rgb(0.9, 0.9, 0.9) # RGB 范圍 0~1 ctx.paint() # 填充整個(gè)表面 # 3. 繪制紅色矩形 ctx.set_source_rgb(1, 0, 0) # 紅色 ctx.rectangle(50, 50, 100, 80) # (x, y, width, height) ctx.fill() # 填充形狀 # 4. 繪制藍(lán)色邊框圓形 ctx.arc(250, 150, 40, 0, 2 * 3.14159) # (x, y, radius, start_angle, end_angle) ctx.set_source_rgb(0, 0, 1) # 藍(lán)色 ctx.set_line_width(3) # 線寬 ctx.stroke() # 描邊路徑 # 5. 保存為 PNG surface.write_to_png("basic_shapes.png")
注釋說(shuō)明:
set_source_rgb()
: 設(shè)置繪圖顏色(RGB值)。rectangle()
和arc()
: 定義路徑形狀。fill()
和stroke()
: 分別表示填充路徑和繪制路徑邊框。write_to_png()
: 將結(jié)果保存為PNG文件。
五、高級(jí)特性
1. 坐標(biāo)變換
# 平移、旋轉(zhuǎn)、縮放示例 ctx.save() # 保存當(dāng)前狀態(tài) ctx.translate(200, 150) # 移動(dòng)原點(diǎn)至 (200,150) ctx.rotate(0.25 * 3.14159) # 旋轉(zhuǎn)45度(弧度制) ctx.scale(2, 2) # 縮放2倍 ctx.rectangle(-20, -20, 40, 40) # 繪制正方形 ctx.set_source_rgb(0, 1, 0) ctx.fill() ctx.restore() # 恢復(fù)之前保存的狀態(tài)
2. 漸變填充
# 線性漸變 linear = cairo.LinearGradient(0, 0, 300, 300) linear.add_color_stop_rgba(0, 1, 0, 0, 1) # 起點(diǎn)紅色 linear.add_color_stop_rgba(1, 0, 0, 1, 0.5) # 終點(diǎn)半透明藍(lán)色 ctx.set_source(linear) ctx.rectangle(50, 50, 200, 200) ctx.fill()
3. 路徑操作與曲線
# 繪制貝塞爾曲線 ctx.move_to(50, 200) # 起點(diǎn) ctx.curve_to(100, 50, 200, 350, 350, 200) # 控制點(diǎn)1, 控制點(diǎn)2, 終點(diǎn) ctx.set_source_rgb(0.5, 0, 0.5) ctx.set_line_width(4) ctx.stroke()
4. 文本渲染
# 設(shè)置字體并繪制文本 ctx.select_font_face("Arial", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD) ctx.set_font_size(30) ctx.move_to(50, 100) ctx.set_source_rgb(0, 0, 0) ctx.show_text("Hello Cairo!") # 繪制文本
六、綜合案例:繪制復(fù)雜圖形
import cairo # 初始化 surface = cairo.ImageSurface(cairo.Format.ARGB32, 600, 400) ctx = cairo.Context(surface) ctx.set_source_rgb(1, 1, 1) ctx.paint() # 繪制漸變背景 gradient = cairo.RadialGradient(300, 200, 10, 300, 200, 300) gradient.add_color_stop_rgba(0, 0.8, 0.8, 1, 1) gradient.add_color_stop_rgba(1, 0.2, 0.2, 0.5, 1) ctx.set_source(gradient) ctx.rectangle(0, 0, 600, 400) ctx.fill() # 繪制旋轉(zhuǎn)矩形 ctx.save() ctx.translate(300, 200) for i in range(12): ctx.rotate(3.14159 / 6) # 每次旋轉(zhuǎn)30度 ctx.set_source_rgba(0, 0.5, 0, 0.5) ctx.rectangle(-30, -30, 60, 60) ctx.fill() ctx.restore() # 保存結(jié)果 surface.write_to_png("advanced_demo.png")
七、關(guān)鍵注意事項(xiàng)
- 坐標(biāo)系:原點(diǎn)
(0,0)
在左上角,x向右增長(zhǎng),y向下增長(zhǎng)。 - 路徑操作:所有路徑需通過(guò)
fill()
或stroke()
才會(huì)生效。 - 狀態(tài)堆棧:
save()
和restore()
用于管理繪圖狀態(tài)(如顏色、變換)。
通過(guò)結(jié)合基礎(chǔ)方法和高級(jí)特性,Cairo 能實(shí)現(xiàn)復(fù)雜的矢量圖形繪制。建議參考 Cairo官方文檔 深入探索更多功能。
到此這篇關(guān)于Python Cairo庫(kù)的使用教程及代碼案例的文章就介紹到這了,更多相關(guān)Python Cairo庫(kù)使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
numpy.random.seed()的使用實(shí)例解析
這篇文章主要介紹了numpy.random.seed()的使用實(shí)例解析,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02快速解決jupyter notebook啟動(dòng)需要密碼的問(wèn)題
這篇文章主要介紹了快速解決jupyter notebook啟動(dòng)需要密碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04python paramiko遠(yuǎn)程服務(wù)器終端操作過(guò)程解析
這篇文章主要介紹了python paramiko遠(yuǎn)程服務(wù)器終端操作過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12Python截圖的五個(gè)方法實(shí)例總結(jié)
學(xué)習(xí)一門語(yǔ)言最好的方法便是實(shí)踐,想要拿Python寫一個(gè)截圖工具,下面這篇文章主要給大家介紹了關(guān)于Python截圖的五個(gè)方法,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12Python實(shí)現(xiàn)基本Socket服務(wù)端與客戶端通信的完整代碼
這篇文章主要介紹了Python實(shí)現(xiàn)基本Socket服務(wù)端與客戶端通信,分步詳解與完整代碼都有,按需所求即可,對(duì)Python Socket服務(wù)端與客戶端通信相關(guān)知識(shí)感興趣的朋友一起看看吧2023-06-06python?matplotlib繪畫十一種常見(jiàn)數(shù)據(jù)分析圖
這篇文章主要介紹了python?matplotlib繪畫十一種常見(jiàn)數(shù)據(jù)分析圖,文章主要繪制折線圖、散點(diǎn)圖、直方圖、餅圖等需要的小伙伴可以參考一下文章具體內(nèi)容2022-06-06