screenshot-to-code - 截图转代码工具


本文翻译整理自: https://github.com/abi/screenshot-to-code


一、关于 screenshot-to-code

一款基于AI的简单工具,可将截图、线框图和Figma设计稿转换为简洁、可运行的代码。现已支持Claude Sonnet 3.5和 Gemini 2.0 Flash模型!

演示视频


相关链接资源


关键功能特性

  • 支持的技术栈:
    • HTML + Tailwind
    • HTML + CSS
    • React + Tailwind
    • Vue + Tailwind
    • Bootstrap
    • Ionic + Tailwind
    • SVG
  • 支持的AI模型:
    • Claude Sonnet 3.5(最佳推荐)
    • GPT-4o(同样推荐)
    • DALL-E 3或Flux Schnell(通过Replicate实现图像生成)
  • 新增实验性功能:可将网站操作视频/屏幕录像转换为可运行的原型

查看详情:https://github.com/abi/screenshot-to-code/wiki/Screen-Recording-to-Code


二、快速开始

该应用采用React/Vite前端和FastAPI后端架构。

需要准备的密钥:


如需使用Ollama开源模型运行(不推荐,效果较差),查看详情:https://github.com/abi/screenshot-to-code/issues/354#issuecomment-2435479853

运行后端(使用Poetry进行包管理,若无则执行pip install poetry):

cd backend
echo "OPENAI_API_KEY=sk-your-key" > .env
echo "ANTHROPIC_API_KEY=your-key" > .env
poetry install
poetry shell
poetry run uvicorn main:app --reload --port 7001

也可在前端设置对话框中配置密钥(加载前端后点击齿轮图标)。

运行前端:

cd frontend
yarn
yarn dev

访问 http://localhost:5173 使用应用。

如需更改后端端口,修改frontend/.env.local中的VITE_WS_BACKEND_URL

调试模式下运行(避免消耗GPT4-Vision额度,使用预录响应):

MOCK=true poetry run uvicorn main:app --reload --port 7001

三、Docker部署

在项目根目录执行:

echo "OPENAI_API_KEY=sk-your-key" > .env
docker-compose up -d --build

应用将运行在 http://localhost:5173。注意此方式不支持开发模式,文件变更不会触发重建。


四、常见问题

1、后端设置报错如何解决?
参考方案:https://github.com/abi/screenshot-to-code/issues/3#issuecomment-1814777959,若仍无法解决请提交issue

2、如何获取OpenAI API密钥?
查看详情:https://github.com/abi/screenshot-to-code/blob/main/Troubleshooting.md

3、如何配置OpenAI代理?
若因地区限制无法直接访问OpenAI API,可尝试VPN或通过以下方式配置代理:

  • backend/.env中设置OPENAI_BASE_URL
  • 或在前端设置对话框中直接配置
  • 确保URL路径包含"v1",例如:https://xxx.xxxxx.xxx/v1

4、如何修改前端连接的后端地址?
配置front/.env.local中的:

  • VITE_HTTP_BACKEND_URL
  • VITE_WS_BACKEND_URL
    例如:VITE_HTTP_BACKEND_URL=http://124.10.20.1:7001

5、运行后端时出现UTF-8编码错误?
Windows系统请用Notepad++打开.env文件,选择Encoding → UTF-8

6、如何提交反馈?
可通过issue或Twitter联系:https://twitter.com/_abi_


五、示例展示

纽约时报页面

原图生成结果
Screenshot 2023-11-20 at 12 54 03 PMScreenshot 2023-11-20 at 12 59 56 PM

Instagram页面(非Taylor Swift图片)

https://github.com/abi/screenshot-to-code/assets/23818/503eb86a-356e-4dfc-926a-dabdb1ac7ba1

Hacker News页面
初次生成颜色有误,经调整后修正

https://github.com/abi/screenshot-to-code/assets/23818/3fec0f77-44e8-4fb3-a769-ac7410315e5d


伊织 xAI 2025-04-19(六)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EAI工程笔记

请我喝杯伯爵奶茶~!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值