Skip to content

Repository files navigation

Python 3.8+MIT LicensePyPIDownloads23 Themes500+ Languages

CodeCanvas

Lightweight Code Screenshot Beautification Engine
轻量级代码截图美化引擎 · 輕量級程式碼截圖美化工具

简体中文 · 繁體中文 · English


CodeCanvas

把代码变成精美的截图,让每一行代码都值得分享。

CodeCanvas 是一个轻量级的代码截图美化引擎,用纯 Python 编写。它能把枯燥的源代码转化为精美的、可直接用于社交媒体分享的截图。灵感来源于 carbon.now.shray.so,但不需要 Node.js 环境,零配置即可使用。

与同类工具相比,CodeCanvas 有几个显著的不同之处:

  • 纯 Python 实现 -- 不依赖 Node.js,Python 开发者无需额外安装运行时
  • 零配置开箱即用 -- 安装后直接使用,所有参数都有合理默认值
  • 内置 Web UI -- 提供可视化编辑界面,所见即所得
  • 批量处理 -- 支持目录级别的批量渲染,适合团队文档自动化
  • 模板系统 -- 预设配置模板,确保团队输出风格统一

✨ 核心特性

🎨 23 款内置主题

涵盖主流编辑器和社区热门配色方案,从经典到潮流一应俱全:

主题风格背景色
monokai经典暗色#272822
dracula流行暗色#282a36
one-darkAtom 风格#282c34
github-darkGitHub 暗色#0d1117
github-lightGitHub 亮色#ffffff
nord北极冷色调#2e3440
solarized-darkSolarized 暗色#002b36
solarized-lightSolarized 亮色#fdf6e3
vs-code-darkVS Code Dark+#1e1e1e
vs-code-lightVS Code Light+#ffffff
ayu-darkAyu 暗色#0a0e14
ayu-lightAyu 亮色#fafafa
gruvbox-darkGruvbox 暗色#282828
gruvbox-lightGruvbox 亮色#fbf1c7
tokyo-night东京夜色#1a1b26
catppuccin-mochaCatppuccin 摩卡#1e1e2e
catppuccin-latteCatppuccin 拿铁#eff1f5
rose-pineRose Pine 暗色#191724
rose-pine-dawnRose Pine 晨曦#faf4ed
night-owlNight Owl#011627
shades-of-purple紫色系#2d2b55
vitesse-darkVitesse 暗色#121212
vitesse-lightVitesse 亮色#ffffff

🪟 4 种窗口装饰风格

风格说明
macosmacOS 红绿灯按钮,经典三色圆点
windowsWindows 风格,最小化/最大化/关闭按钮
linuxLinux 风格,简洁三色圆点
none无窗口装饰,纯净代码展示

🔧 更多特性

  • 500+ 语言语法高亮 -- 基于 Pygments,覆盖几乎所有编程语言
  • 内置 Web UI -- 可视化编辑界面,实时预览效果
  • 批量处理 -- 支持目录级别的批量渲染,自动遍历子目录
  • 模板系统 -- 预设配置模板,团队协作风格统一
  • 多格式导出 -- 支持 PNG / JPEG / PDF / SVG
  • Retina 适配 -- 支持 2x、3x 高清导出,适配 Retina 屏幕
  • 水印支持 -- 可添加自定义水印文字
  • 行高亮 -- 支持指定行号高亮,突出关键代码
  • 自动语言检测 -- 根据文件扩展名或内容自动识别编程语言
  • 剪贴板读取 -- 直接从剪贴板读取代码并渲染
  • 零配置 -- 所有参数都有合理默认值,开箱即用

🚀 快速开始

环境要求

  • Python 3.8 或更高版本

安装

pip install codecanvas

基本用法

# 从文件渲染
codecanvas render main.py -o screenshot.png
# 指定主题和窗口风格
codecanvas render main.py --theme dracula --window-style macos
# 从剪贴板读取代码并渲染
codecanvas render --clipboard -o clipboard.png
# 渲染后自动复制到剪贴板
codecanvas render main.py -o screenshot.png --copy

启动 Web UI

codecanvas web
# 访问 http://127.0.0.1:8420

📖 使用指南

CLI 命令一览

CodeCanvas 提供以下子命令:

render -- 渲染代码截图

# 基本渲染
codecanvas render main.py -o output.png
# 完整参数示例
codecanvas render main.py \
--theme dracula \
--window-style macos \
--window-title "app.py" \
--font-size 16 \
--font-family "JetBrains Mono" \
--width 800 \
--padding 40 \
--scale 2 \
--format png \
--watermark "CONFIDENTIAL" \
--highlight-lines "3,5-8,12" \
--language python \
-o output.png

batch -- 批量渲染

# 批量渲染目录下所有 Python 文件
codecanvas batch ./src --output ./screenshots
# 递归渲染,匹配多种文件类型
codecanvas batch ./src --output ./screenshots --pattern "*.py" --recursive
# 使用模板批量渲染
codecanvas batch ./src --output ./screenshots --template "team-dark"

web -- 启动 Web UI

# 默认启动
codecanvas web
# 指定端口,不自动打开浏览器
codecanvas web --port 9000 --no-browser

themes -- 主题管理

# 列出所有可用主题
codecanvas themes --list
# 查看主题详情
codecanvas themes --info dracula

templates -- 模板管理

# 列出所有模板
codecanvas templates --list
# 应用模板
codecanvas templates --apply "team-dark"# 创建新模板
codecanvas templates --create "my-style"

配置选项一览

参数说明默认值
--theme / -t颜色主题monokai
--background自定义背景色(十六进制)主题背景色
--padding内边距(像素)40
--window-style窗口装饰风格macos
--window-title窗口标题文字文件名
--font-size字体大小16
--font-family字体族JetBrains Mono
--width最大宽度(像素)800
--scale导出缩放倍数(Retina)2
--format导出格式png
--watermark水印文字
--highlight-lines高亮行号(如 3,5-8,12
--language编程语言(默认自动检测)auto
--no-line-numbers隐藏行号显示
--clipboard从剪贴板读取关闭
--copy渲染后复制到剪贴板关闭
--template应用预设模板

Python API 调用

除了命令行工具,CodeCanvas 也可以作为 Python 库直接调用:

fromcodecanvas.rendererimportCodeCanvas# 创建渲染器实例canvas=CodeCanvas(config={
"theme": "dracula",
"window_style": "macos",
"padding": 40,
"font_size": 16,
"export_scale": 2,
})
# 从文件渲染output=canvas.render_from_file("main.py", "screenshot.png")
print(f"截图已保存: {output}")
# 从字符串渲染code='print("Hello, CodeCanvas!")'output=canvas.render_from_string(code, "hello.png")
# 从剪贴板渲染output=canvas.render_from_clipboard("clipboard.png")

🎨 主题展示

所有 23 款内置主题的效果预览存放在 examples/output/ 目录下。

📋 查看完整主题列表与背景色
主题名称显示名称背景色预览
monokaiMonokai#272822经典 Sublime Text 配色,暖色调暗色主题
draculaDracula#282a36社区最受欢迎的暗色主题之一
one-darkOne Dark#282c34Atom 编辑器默认暗色主题
github-darkGitHub Dark#0d1117GitHub 官方暗色模式
github-lightGitHub Light#ffffffGitHub 官方亮色模式
nordNord#2e3440来自北极的冷色调配色方案
solarized-darkSolarized Dark#002b36Solarized 经典暗色变体
solarized-lightSolarized Light#fdf6e3Solarized 经典亮色变体
vs-code-darkVS Code Dark+#1e1e1eVisual Studio Code 默认暗色主题
vs-code-lightVS Code Light+#ffffffVisual Studio Code 默认亮色主题
ayu-darkAyu Dark#0a0e14Ayu 系列暗色主题
ayu-lightAyu Light#fafafaAyu 系列亮色主题
gruvbox-darkGruvbox Dark#282828复古暖色调暗色主题
gruvbox-lightGruvbox Light#fbf1c7复古暖色调亮色主题
tokyo-nightTokyo Night#1a1b26灵感来自东京夜景的暗色主题
catppuccin-mochaCatppuccin Mocha#1e1e2eCatppuccin 系列深色变体
catppuccin-latteCatppuccin Latte#eff1f5Catppuccin 系列亮色变体
rose-pineRose Pine#191724优雅的玫瑰松暗色主题
rose-pine-dawnRose Pine Dawn#faf4edRose Pine 晨曦亮色变体
night-owlNight Owl#011627Sarah Drasner 设计的夜猫子主题
shades-of-purpleShades of Purple#2d2b55紫色系暗色主题
vitesse-darkVitesse Dark#121212Vite 生态暗色主题
vitesse-lightVitesse Light#ffffffVite 生态亮色主题

💡 设计思路

为什么选择纯 Python?

市面上主流的代码截图工具(如 Carbon)大多基于 Node.js 生态。对于 Python 开发者来说,为了一个截图工具去安装 Node.js 运行时并不划算。CodeCanvas 用纯 Python 实现,与 Python 项目的工具链无缝集成,CI/CD 流水线中也能直接调用。

为什么选择 Pillow + Pygments?

  • Pillow -- Python 图像处理的事实标准,成熟稳定,API 设计优雅,足以胜任代码截图的渲染需求
  • Pygments -- Python 语法高亮的事实标准,支持 500+ 种编程语言,词法分析准确可靠

这两个库的组合既轻量又强大,不需要 Chromium 或其他重量级依赖,安装简单、运行快速。

架构概览

codecanvas/
├── cli.py # 命令行入口,参数解析与命令分发
├── renderer.py # 核心渲染引擎,语法高亮与图像绘制
├── themes.py # 主题管理系统,23 款内置主题
├── templates.py # 模板系统,预设配置管理
├── batch.py # 批量处理模块
├── web.py # Web UI 服务端
└── utils.py # 工具函数(字体查找、语言检测等)

渲染管线流程:

  1. 语法高亮 -- Pygments 对源代码进行词法分析,生成带颜色标记的 token 流
  2. 布局计算 -- 根据字体、行高、内边距等参数计算画布尺寸
  3. 背景绘制 -- 绘制圆角矩形背景,可选高斯模糊阴影
  4. 窗口装饰 -- 根据 window_style 绘制标题栏和控制按钮
  5. 代码绘制 -- 逐 token 绘制语法高亮后的代码文本
  6. 行号绘制 -- 在代码左侧绘制带对齐的行号
  7. 行高亮 -- 对指定行添加半透明高亮背景
  8. 水印叠加 -- 可选地在画面上叠加水印文字
  9. 图像导出 -- 按 PNG/JPEG/PDF/SVG 格式输出,支持 Retina 缩放

未来规划

  • 自定义主题编辑器(Web UI 内置)
  • 渐变背景支持
  • 代码片段分享平台集成
  • VS Code / JetBrains 插件
  • Figma / Sketch 插件导出

📦 安装与部署

从 PyPI 安装(推荐)

pip install codecanvas

从源码安装

git clone https://github.com/gitstq/codecanvas.git
cd codecanvas
pip install -e .

安装开发依赖

pip install -e ".[dev]"

Web 服务部署

# 启动 Web UI(默认端口 8420)
codecanvas web --host 0.0.0.0 --port 8420
# 后台运行
nohup codecanvas web --host 0.0.0.0 --port 8420 --no-browser &

🤝 贡献指南

欢迎任何形式的贡献!无论是提交 Bug、改进文档还是贡献代码。

提交 Issue

  • 使用 GitHub Issues 提交 Bug 报告或功能建议
  • 描述问题时请附上操作系统版本、Python 版本和复现步骤

提交 PR

  1. Fork 本仓库
  2. 创建特性分支:git checkout -b feature/your-feature
  3. 提交改动:git commit -m "feat: add your feature"
  4. 推送分支:git push origin feature/your-feature
  5. 提交 Pull Request

代码规范

  • 遵循 PEP 8 编码规范
  • 为新增功能编写单元测试
  • 确保所有测试通过:pytest

📄 开源协议

本项目基于 MIT License 开源。

Copyright (c) 2024 gitstq

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.


Made with ❤️ by gitstq



CodeCanvas

把程式碼變成精美的截圖,讓每一行程式碼都值得分享。

CodeCanvas 是一個輕量級的程式碼截圖美化引擎,用純 Python 撰寫。它能將枯燥的原始碼轉化為精美的、可直接用於社群媒體分享的截圖。靈感來自於 carbon.now.shray.so,但不需要 Node.js 環境,零設定即可使用。

與同類工具相比,CodeCanvas 有幾個顯著的不同之處:

  • 純 Python 實作 -- 不依賴 Node.js,Python 開發者無需額外安裝執行環境
  • 零設定開箱即用 -- 安裝後直接使用,所有參數都有合理預設值
  • 內建 Web UI -- 提供視覺化編輯介面,所見即所得
  • 批次處理 -- 支援目錄級別的批次渲染,適合團隊文件自動化
  • 範本系統 -- 預設配置範本,確保團隊輸出風格統一

✨ 核心特性

🎨 23 款內建主題

涵蓋主流編輯器與社群熱門配色方案,從經典到潮流一應俱全:

主題風格背景色
monokai經典暗色#272822
dracula流行暗色#282a36
one-darkAtom 風格#282c34
github-darkGitHub 暗色#0d1117
github-lightGitHub 亮色#ffffff
nord北極冷色調#2e3440
solarized-darkSolarized 暗色#002b36
solarized-lightSolarized 亮色#fdf6e3
vs-code-darkVS Code Dark+#1e1e1e
vs-code-lightVS Code Light+#ffffff
ayu-darkAyu 暗色#0a0e14
ayu-lightAyu 亮色#fafafa
gruvbox-darkGruvbox 暗色#282828
gruvbox-lightGruvbox 亮色#fbf1c7
tokyo-night東京夜色#1a1b26
catppuccin-mochaCatppuccin 摩卡#1e1e2e
catppuccin-latteCatppuccin 拿鐵#eff1f5
rose-pineRose Pine 暗色#191724
rose-pine-dawnRose Pine 晨曦#faf4ed
night-owlNight Owl#011627
shades-of-purple紫色系#2d2b55
vitesse-darkVitesse 暗色#121212
vitesse-lightVitesse 亮色#ffffff

🪟 4 種視窗裝飾風格

風格說明
macosmacOS 紅綠燈按鈕,經典三色圓點
windowsWindows 風格,最小化/最大化/關閉按鈕
linuxLinux 風格,簡潔三色圓點
none無視窗裝飾,純淨程式碼展示

🔧 更多特性

  • 500+ 語言語法高亮 -- 基於 Pygments,涵蓋幾乎所有程式語言
  • 內建 Web UI -- 視覺化編輯介面,即時預覽效果
  • 批次處理 -- 支援目錄級別的批次渲染,自動遞迴子目錄
  • 範本系統 -- 預設配置範本,團隊協作風格統一
  • 多格式匯出 -- 支援 PNG / JPEG / PDF / SVG
  • Retina 适配 -- 支援 2x、3x 高清匯出,適配 Retina 螢幕
  • 浮水印支援 -- 可新增自訂浮水印文字
  • 行高亮 -- 支援指定行號高亮,突出關鍵程式碼
  • 自動語言偵測 -- 根據副檔名或內容自動識別程式語言
  • 剪貼簿讀取 -- 直接從剪貼簿讀取程式碼並渲染
  • 零設定 -- 所有參數都有合理預設值,開箱即用

🚀 快速開始

環境需求

  • Python 3.8 或更高版本

安裝

pip install codecanvas

基本用法

# 從檔案渲染
codecanvas render main.py -o screenshot.png
# 指定主題和視窗風格
codecanvas render main.py --theme dracula --window-style macos
# 從剪貼簿讀取程式碼並渲染
codecanvas render --clipboard -o clipboard.png
# 渲染後自動複製到剪貼簿
codecanvas render main.py -o screenshot.png --copy

啟動 Web UI

codecanvas web
# 前往 http://127.0.0.1:8420

📖 使用指南

CLI 指令一覽

CodeCanvas 提供以下子指令:

render -- 渲染程式碼截圖

# 基本渲染
codecanvas render main.py -o output.png
# 完整參數範例
codecanvas render main.py \
--theme dracula \
--window-style macos \
--window-title "app.py" \
--font-size 16 \
--font-family "JetBrains Mono" \
--width 800 \
--padding 40 \
--scale 2 \
--format png \
--watermark "CONFIDENTIAL" \
--highlight-lines "3,5-8,12" \
--language python \
-o output.png

batch -- 批次渲染

# 批次渲染目錄下所有 Python 檔案
codecanvas batch ./src --output ./screenshots
# 遞迴渲染,匹配多種檔案類型
codecanvas batch ./src --output ./screenshots --pattern "*.py" --recursive
# 使用範本批次渲染
codecanvas batch ./src --output ./screenshots --template "team-dark"

web -- 啟動 Web UI

# 預設啟動
codecanvas web
# 指定連接埠,不自動開啟瀏覽器
codecanvas web --port 9000 --no-browser

themes -- 主題管理

# 列出所有可用主題
codecanvas themes --list
# 查看主題詳情
codecanvas themes --info dracula

templates -- 範本管理

# 列出所有範本
codecanvas templates --list
# 套用範本
codecanvas templates --apply "team-dark"# 建立新範本
codecanvas templates --create "my-style"

設定選項一覽

參數說明預設值
--theme / -t顏色主題monokai
--background自訂背景色(十六進位)主題背景色
--padding內邊距(像素)40
--window-style視窗裝飾風格macos
--window-title視窗標題文字檔案名稱
--font-size字型大小16
--font-family字型族JetBrains Mono
--width最大寬度(像素)800
--scale匯出縮放倍數(Retina)2
--format匯出格式png
--watermark浮水印文字
--highlight-lines高亮行號(如 3,5-8,12
--language程式語言(預設自動偵測)auto
--no-line-numbers隱藏行號顯示
--clipboard從剪貼簿讀取關閉
--copy渲染後複製到剪貼簿關閉
--template套用預設範本

Python API 呼叫

除了命令列工具,CodeCanvas 也可以作為 Python 函式庫直接呼叫:

fromcodecanvas.rendererimportCodeCanvas# 建立渲染器實例canvas=CodeCanvas(config={
"theme": "dracula",
"window_style": "macos",
"padding": 40,
"font_size": 16,
"export_scale": 2,
})
# 從檔案渲染output=canvas.render_from_file("main.py", "screenshot.png")
print(f"截圖已儲存: {output}")
# 從字串渲染code='print("Hello, CodeCanvas!")'output=canvas.render_from_string(code, "hello.png")
# 從剪貼簿渲染output=canvas.render_from_clipboard("clipboard.png")

🎨 主題展示

所有 23 款內建主題的效果預覽存放在 examples/output/ 目錄下。

📋 查看完整主題列表與背景色
主題名稱顯示名稱背景色預覽
monokaiMonokai#272822經典 Sublime Text 配色,暖色調暗色主題
draculaDracula#282a36社群最受歡迎的暗色主題之一
one-darkOne Dark#282c34Atom 編輯器預設暗色主題
github-darkGitHub Dark#0d1117GitHub 官方暗色模式
github-lightGitHub Light#ffffffGitHub 官方亮色模式
nordNord#2e3440來自北極的冷色調配色方案
solarized-darkSolarized Dark#002b36Solarized 經典暗色變體
solarized-lightSolarized Light#fdf6e3Solarized 經典亮色變體
vs-code-darkVS Code Dark+#1e1e1eVisual Studio Code 預設暗色主題
vs-code-lightVS Code Light+#ffffffVisual Studio Code 預設亮色主題
ayu-darkAyu Dark#0a0e14Ayu 系列暗色主題
ayu-lightAyu Light#fafafaAyu 系列亮色主題
gruvbox-darkGruvbox Dark#282828復古暖色調暗色主題
gruvbox-lightGruvbox Light#fbf1c7復古暖色調亮色主題
tokyo-nightTokyo Night#1a1b26靈感來自東京夜景的暗色主題
catppuccin-mochaCatppuccin Mocha#1e1e2eCatppuccin 系列深色變體
catppuccin-latteCatppuccin Latte#eff1f5Catppuccin 系列亮色變體
rose-pineRose Pine#191724優雅的玫瑰松暗色主題
rose-pine-dawnRose Pine Dawn#faf4edRose Pine 晨曦亮色變體
night-owlNight Owl#011627Sarah Drasner 設計的夜貓子主題
shades-of-purpleShades of Purple#2d2b55紫色系暗色主題
vitesse-darkVitesse Dark#121212Vite 生態暗色主題
vitesse-lightVitesse Light#ffffffVite 生態亮色主題

💡 設計思路

為什麼選擇純 Python?

市面上主流的程式碼截圖工具(如 Carbon)大多基於 Node.js 生態。對於 Python 開發者來說,為了一個截圖工具去安裝 Node.js 執行環境並不划算。CodeCanvas 用純 Python 實作,與 Python 專案的工具鏈無縫整合,CI/CD 流水線中也能直接呼叫。

為什麼選擇 Pillow + Pygments?

  • Pillow -- Python 影像處理的事實標準,成熟穩定,API 設計優雅,足以勝任程式碼截圖的渲染需求
  • Pygments -- Python 語法高亮的事實標準,支援 500+ 種程式語言,詞法分析準確可靠

這兩個函式庫的組合既輕量又強大,不需要 Chromium 或其他重量級依賴,安裝簡單、執行快速。

架構概覽

codecanvas/
├── cli.py # 命令列入口,參數解析與指令分發
├── renderer.py # 核心渲染引擎,語法高亮與影像繪製
├── themes.py # 主題管理系統,23 款內建主題
├── templates.py # 範本系統,預設配置管理
├── batch.py # 批次處理模組
├── web.py # Web UI 伺服端
└── utils.py # 工具函式(字型查找、語言偵測等)

渲染管線流程:

  1. 語法高亮 -- Pygments 對原始碼進行詞法分析,產生帶顏色標記的 token 串流
  2. 版面計算 -- 根據字型、行高、內邊距等參數計算畫布尺寸
  3. 背景繪製 -- 繪製圓角矩形背景,可選高斯模糊陰影
  4. 視窗裝飾 -- 根據 window_style 繪製標題列和控制按鈕
  5. 程式碼繪製 -- 逐 token 繪製語法高亮後的程式碼文字
  6. 行號繪製 -- 在程式碼左側繪製帶對齊的行號
  7. 行高亮 -- 對指定行新增半透明高亮背景
  8. 浮水印疊加 -- 可選地在畫面上疊加浮水印文字
  9. 影像匯出 -- 按 PNG/JPEG/PDF/SVG 格式輸出,支援 Retina 縮放

未來規劃

  • 自訂主題編輯器(Web UI 內建)
  • 漸層背景支援
  • 程式碼片段分享平台整合
  • VS Code / JetBrains 外掛
  • Figma / Sketch 外掛匯出

📦 安裝與部署

從 PyPI 安裝(推薦)

pip install codecanvas

從原始碼安裝

git clone https://github.com/gitstq/codecanvas.git
cd codecanvas
pip install -e .

安裝開發依賴

pip install -e ".[dev]"

Web 服務部署

# 啟動 Web UI(預設連接埠 8420)
codecanvas web --host 0.0.0.0 --port 8420
# 背景執行
nohup codecanvas web --host 0.0.0.0 --port 8420 --no-browser &

🤝 貢獻指南

歡迎任何形式的貢獻!無論是回報 Bug、改善文件還是貢獻程式碼。

回報 Issue

  • 使用 GitHub Issues 回報 Bug 或提出功能建議
  • 描述問題時請附上作業系統版本、Python 版本與重現步驟

提交 PR

  1. Fork 本儲存庫
  2. 建立特性分支:git checkout -b feature/your-feature
  3. 提交變更:git commit -m "feat: add your feature"
  4. 推送分支:git push origin feature/your-feature
  5. 提交 Pull Request

程式碼規範

  • 遵循 PEP 8 編碼規範
  • 為新增功能撰寫單元測試
  • 確保所有測試通過:pytest

📄 開源協議

本專案基於 MIT License 開源。

Copyright (c) 2024 gitstq

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.


Made with ❤️ by gitstq



CodeCanvas

Turn your code into beautiful, shareable screenshots.

CodeCanvas is a lightweight code screenshot beautification engine written in pure Python. It transforms raw source code into stunning, publication-ready screenshots perfect for social media sharing. Inspired by carbon.now.sh and ray.so, but with zero Node.js dependency and zero configuration required.

What sets CodeCanvas apart from similar tools:

  • Pure Python -- No Node.js runtime needed, seamlessly integrates into Python workflows
  • Zero config -- Sensible defaults for everything, works right out of the box
  • Built-in Web UI -- Visual editing interface with live preview
  • Batch processing -- Directory-level batch rendering for documentation automation
  • Template system -- Predefined configuration templates for team consistency

✨ Core Features

🎨 23 Built-in Themes

A curated collection covering mainstream editors and community favorites, from timeless classics to trending palettes:

ThemeStyleBackground
monokaiClassic Dark#272822
draculaPopular Dark#282a36
one-darkAtom Style#282c34
github-darkGitHub Dark#0d1117
github-lightGitHub Light#ffffff
nordArctic Cool#2e3440
solarized-darkSolarized Dark#002b36
solarized-lightSolarized Light#fdf6e3
vs-code-darkVS Code Dark+#1e1e1e
vs-code-lightVS Code Light+#ffffff
ayu-darkAyu Dark#0a0e14
ayu-lightAyu Light#fafafa
gruvbox-darkGruvbox Dark#282828
gruvbox-lightGruvbox Light#fbf1c7
tokyo-nightTokyo Night#1a1b26
catppuccin-mochaCatppuccin Mocha#1e1e2e
catppuccin-latteCatppuccin Latte#eff1f5
rose-pineRose Pine#191724
rose-pine-dawnRose Pine Dawn#faf4ed
night-owlNight Owl#011627
shades-of-purpleShades of Purple#2d2b55
vitesse-darkVitesse Dark#121212
vitesse-lightVitesse Light#ffffff

🪟 4 Window Decoration Styles

StyleDescription
macosmacOS traffic-light buttons with classic red/yellow/green dots
windowsWindows-style minimize/maximize/close buttons
linuxLinux-style minimal three-dot buttons
noneNo window chrome, clean code-only display

🔧 More Features

  • 500+ language syntax highlighting -- Powered by Pygments, covering virtually every programming language
  • Built-in Web UI -- Visual editor with real-time preview
  • Batch processing -- Directory-level batch rendering with recursive traversal
  • Template system -- Predefined configuration templates for consistent team output
  • Multi-format export -- PNG / JPEG / PDF / SVG
  • Retina / HiDPI support -- 2x and 3x export scaling for high-DPI displays
  • Watermark support -- Add custom watermark text to your screenshots
  • Line highlighting -- Highlight specific lines to draw attention to key code
  • Auto language detection -- Automatically identifies the programming language from file extension or content
  • Clipboard support -- Read code directly from the system clipboard
  • Zero configuration -- Sensible defaults for all parameters, ready to use immediately

🚀 Quick Start

Requirements

  • Python 3.8 or later

Installation

pip install codecanvas

Basic Usage

# Render from a file
codecanvas render main.py -o screenshot.png
# Specify theme and window style
codecanvas render main.py --theme dracula --window-style macos
# Read from clipboard and render
codecanvas render --clipboard -o clipboard.png
# Copy the result back to clipboard
codecanvas render main.py -o screenshot.png --copy

Start the Web UI

codecanvas web
# Visit http://127.0.0.1:8420

📖 Usage Guide

CLI Commands

CodeCanvas provides the following subcommands:

render -- Render a code screenshot

# Basic render
codecanvas render main.py -o output.png
# Full parameter example
codecanvas render main.py \
--theme dracula \
--window-style macos \
--window-title "app.py" \
--font-size 16 \
--font-family "JetBrains Mono" \
--width 800 \
--padding 40 \
--scale 2 \
--format png \
--watermark "CONFIDENTIAL" \
--highlight-lines "3,5-8,12" \
--language python \
-o output.png

batch -- Batch render multiple files

# Batch render all Python files in a directory
codecanvas batch ./src --output ./screenshots
# Recursive rendering with file pattern matching
codecanvas batch ./src --output ./screenshots --pattern "*.py" --recursive
# Batch render with a template
codecanvas batch ./src --output ./screenshots --template "team-dark"

web -- Start the Web UI

# Default launch
codecanvas web
# Custom port, no auto-open browser
codecanvas web --port 9000 --no-browser

themes -- Theme management

# List all available themes
codecanvas themes --list
# Show theme details
codecanvas themes --info dracula

templates -- Template management

# List all templates
codecanvas templates --list
# Apply a template
codecanvas templates --apply "team-dark"# Create a new template
codecanvas templates --create "my-style"

Configuration Options

ParameterDescriptionDefault
--theme / -tColor thememonokai
--backgroundCustom background color (hex)Theme background
--paddingPadding in pixels40
--window-styleWindow decoration stylemacos
--window-titleWindow title textFilename
--font-sizeFont size16
--font-familyFont familyJetBrains Mono
--widthMaximum width in pixels800
--scaleExport scale factor (Retina)2
--formatExport formatpng
--watermarkWatermark textNone
--highlight-linesLines to highlight (e.g. 3,5-8,12)None
--languageProgramming language (auto-detect by default)auto
--no-line-numbersHide line numbersShow
--clipboardRead from clipboardOff
--copyCopy result to clipboard after renderOff
--templateApply a named templateNone

Python API

Beyond the CLI, CodeCanvas can be used as a Python library:

fromcodecanvas.rendererimportCodeCanvas# Create a renderer instancecanvas=CodeCanvas(config={
"theme": "dracula",
"window_style": "macos",
"padding": 40,
"font_size": 16,
"export_scale": 2,
})
# Render from a fileoutput=canvas.render_from_file("main.py", "screenshot.png")
print(f"Screenshot saved: {output}")
# Render from a stringcode='print("Hello, CodeCanvas!")'output=canvas.render_from_string(code, "hello.png")
# Render from clipboardoutput=canvas.render_from_clipboard("clipboard.png")

🎨 Theme Showcase

Preview images for all 23 built-in themes are available in the examples/output/ directory.

📋 Full Theme List with Background Colors
Theme KeyDisplay NameBackgroundDescription
monokaiMonokai#272822Classic Sublime Text palette, warm dark theme
draculaDracula#282a36One of the most popular dark themes in the community
one-darkOne Dark#282c34Default dark theme from Atom editor
github-darkGitHub Dark#0d1117Official GitHub dark mode
github-lightGitHub Light#ffffffOfficial GitHub light mode
nordNord#2e3440Arctic-inspired cool-toned palette
solarized-darkSolarized Dark#002b36Classic Solarized dark variant
solarized-lightSolarized Light#fdf6e3Classic Solarized light variant
vs-code-darkVS Code Dark+#1e1e1eDefault dark theme from Visual Studio Code
vs-code-lightVS Code Light+#ffffffDefault light theme from Visual Studio Code
ayu-darkAyu Dark#0a0e14Dark variant of the Ayu theme family
ayu-lightAyu Light#fafafaLight variant of the Ayu theme family
gruvbox-darkGruvbox Dark#282828Retro warm-toned dark theme
gruvbox-lightGruvbox Light#fbf1c7Retro warm-toned light theme
tokyo-nightTokyo Night#1a1b26Dark theme inspired by the Tokyo night skyline
catppuccin-mochaCatppuccin Mocha#1e1e2eDeep variant of the Catppuccin family
catppuccin-latteCatppuccin Latte#eff1f5Light variant of the Catppuccin family
rose-pineRose Pine#191724Elegant rose-pine dark theme
rose-pine-dawnRose Pine Dawn#faf4edSoft dawn variant of Rose Pine
night-owlNight Owl#011627Dark theme designed by Sarah Drasner
shades-of-purpleShades of Purple#2d2b55Purple-toned dark theme
vitesse-darkVitesse Dark#121212Dark theme from the Vite ecosystem
vitesse-lightVitesse Light#ffffffLight theme from the Vite ecosystem

💡 Design Philosophy

Why Pure Python?

Most code screenshot tools on the market (like Carbon) are built on the Node.js ecosystem. For Python developers, installing an entire Node.js runtime just for a screenshot tool is overkill. CodeCanvas is implemented in pure Python, integrating seamlessly into Python project toolchains and CI/CD pipelines without any additional runtime.

Why Pillow + Pygments?

  • Pillow -- The de facto standard for image processing in Python. Mature, stable, and with an elegant API that is more than capable of handling code screenshot rendering.
  • Pygments -- The de facto standard for syntax highlighting in Python. Supports 500+ programming languages with accurate and reliable lexical analysis.

This combination is both lightweight and powerful. No Chromium or other heavyweight dependencies are needed -- installation is simple and rendering is fast.

Architecture Overview

codecanvas/
├── cli.py # CLI entry point, argument parsing and command dispatch
├── renderer.py # Core rendering engine, syntax highlighting and image drawing
├── themes.py # Theme management system, 23 built-in themes
├── templates.py # Template system, predefined configuration management
├── batch.py # Batch processing module
├── web.py # Web UI server
└── utils.py # Utilities (font discovery, language detection, etc.)

Rendering pipeline:

  1. Syntax highlighting -- Pygments performs lexical analysis on source code, producing a color-annotated token stream
  2. Layout calculation -- Canvas dimensions are computed based on font, line height, padding, and other parameters
  3. Background drawing -- Rounded rectangle background with optional Gaussian blur shadow
  4. Window decorations -- Title bar and control buttons drawn according to window_style
  5. Code drawing -- Syntax-highlighted code text drawn token by token
  6. Line numbers -- Aligned line numbers drawn to the left of the code area
  7. Line highlighting -- Semi-transparent highlight backgrounds applied to specified lines
  8. Watermark overlay -- Optional watermark text overlaid on the canvas
  9. Image export -- Output in PNG/JPEG/PDF/SVG format with Retina scaling support

Roadmap

  • Custom theme editor (built into Web UI)
  • Gradient background support
  • Code snippet sharing platform integration
  • VS Code / JetBrains plugin
  • Figma / Sketch plugin export

📦 Installation & Deployment

Install from PyPI (Recommended)

pip install codecanvas

Install from Source

git clone https://github.com/gitstq/codecanvas.git
cd codecanvas
pip install -e .

Install Development Dependencies

pip install -e ".[dev]"

Web Server Deployment

# Start the Web UI (default port 8420)
codecanvas web --host 0.0.0.0 --port 8420
# Run in the background
nohup codecanvas web --host 0.0.0.0 --port 8420 --no-browser &

🤝 Contributing

Contributions of all kinds are welcome! Whether it is filing a bug, improving documentation, or contributing code.

Filing Issues

  • Use GitHub Issues to submit bug reports or feature requests
  • When describing a problem, please include your OS version, Python version, and steps to reproduce

Submitting PRs

  1. Fork this repository
  2. Create a feature branch: git checkout -b feature/your-feature
  3. Commit your changes: git commit -m "feat: add your feature"
  4. Push the branch: git push origin feature/your-feature
  5. Submit a Pull Request

Code Style

  • Follow PEP 8 conventions
  • Write unit tests for new features
  • Ensure all tests pass: pytest

📄 License

This project is released under the MIT License.

Copyright (c) 2024 gitstq

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.


Made with ❤️ by gitstq

About

🎨 CodeCanvas - Lightweight Code Screenshot Beautification & Social Sharing Engine | 轻量级代码截图美化与社交分享引擎 - 23 Themes, 4 Window Styles, Web UI, Batch Processing, Zero Config

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages