iuiaoin / wechat-gptbot

A wechat robot based on ChatGPT with no risk, very stable! 🚀
MIT License
583 stars 115 forks source link

[Feat]: any beauty-code2image-generator recommendation #76

Closed QAbot-zh closed 1 year ago

QAbot-zh commented 1 year ago

Search for answers in existing issues

Feature description

Find recommendations for a useful code image generator. It should have similar functionality to this online website: https://carbon.now.sh/. I couldn’t find an official API interface, but I did find an unofficial implementation that works (https://github.com/synaia/beauty-code-snippet). However, the generation time is a bit slow. I’m wondering if any of you have any good recommendations, preferably something that can be generated locally? 兄弟们有好用的代码图片生成器推荐嘛,就是类似这种在线网站的功能:https://carbon.now.sh/ ,没有官方的api接口,找到一个能用的非官方实现(https://github.com/synaia/beauty-code-snippet),但是生成时间确实有点慢,看看兄弟们有没有好的推荐,最好是能本地生成

Motivation

As we all know, WeChat does not support rich text format messages. If ChatGPT’s reply includes some embedded code, the readability on the WeChat page will be very poor. However, if there is a good code image generator, the code structure can be preserved better by sending images, even with syntax highlighting. 众所周知,微信没办法支持富文本格式消息,如果ChatGPT回复内容包括了一些内嵌代码,在微信页面的可读性会非常差,如果有个好的代码图片生成器,就可以通过发送图片的形式比较好的保留代码结构,甚至保留语法高亮效果。

Comparison of the effects is as follows: 对比效果如下:

wechat-code beauty-code

iuiaoin commented 1 year ago

@undefinedcodezhong Here's a possible way(from gpt-4😅): There is a Python library named pygments that supports code syntax highlighting. With it, you can generate HTML and LaTeX with highlighting for code in various programming languages. However, it doesn't natively generate images.

To generate an image from your highlighted code, you could use a combination of tools:

Convert your code to HTML using Pygments Render the HTML to an image using a tool like imgkit, which uses wkhtmltoimage under the hood to convert HTML to images.

Here's an example:

from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
import imgkit

# Your code here
code = """
def hello_world():
    print("Hello, world!")
"""

# Convert to HTML
formatter = HtmlFormatter(style='colorful')
highlighted_code = highlight(code, PythonLexer(), formatter)

# Save the HTML to a file
with open('highlighted_code.html', 'w') as f:
    f.write(highlighted_code)

# Convert HTML to image
imgkit.from_file('highlighted_code.html', 'highlighted_code.jpg')
QAbot-zh commented 1 year ago

@iuiaoin Thank you for helping me find a solution through gpt-4. I have noticed the libraries wkhtml2image and imgkit. I have successfully implemented code image generation in a plain text style. Now I am looking for a suitable CSS template to generate HTML similar to the carbon website style. Additionally, the imgkit provides a way to generate images using the "from_string" method, which allows you to bypass the generation and reading of intermediate HTML files. This can greatly improve the efficiency of generating text images.