创建PNG图像在Python中

huangapple go评论88阅读模式
英文:

Creating png images in python

问题

我建议使用Python创建PNG图像,以绘制类似智能地图的流程图,其中包括文本、矩形和连接线。对于这个目的,您可以考虑使用pypng模块。这是一个适用于创建PNG图像的Python库。您可以使用它来生成图像,包括文本、矩形和线条。以下是使用pypng创建PNG图像的一般步骤:

  1. 安装pypng库(如果尚未安装):

    pip install pypng
    
  2. 编写Python代码,使用pypng创建图像。以下是一个简单示例:

    import png
    
    # 创建一个空白图像
    width = 800
    height = 600
    image = png.Writer(width, height)
    
    # 在图像上绘制矩形、文本和线条
    # 这里需要自己编写代码来实现绘制流程图的逻辑
    
    # 保存图像到文件
    with open('flowchart.png', 'wb') as f:
        image.write(f, your_image_data)
    

    在上面的代码中,您需要编写代码来绘制矩形、文本和线条,以满足您的具体需求。pypng提供了生成PNG图像的基本工具,但您需要自己编写代码来构建流程图的外观和结构。

请注意,还有其他Python库可以用于绘制流程图,例如matplotlibgraphviz等。您可以根据自己的需求选择适合您的库。

英文:

Tell me which library is better to use? I want to create a png image with a flowchart in python. Something like an intelligence map, where there will be text, rectangles and lines connecting them.

I googled and found the pypng module. Is it suitable for this purpose? Or are there better and more universal solutions? How do I create the png images I need using python

答案1

得分: 1

首先,你的需求描述和研究如何实现它的内容应该包括在问题中,这有助于社区更好地回答你的问题 创建PNG图像在Python中

对于你的问题,有一些可能的结论。

将图表转换为 .png

如果你想使用某个库创建图表,其中一个最佳方法是使用 matplotlib,这次我将展示如何使用 pyplot 模块来实现。你可以创建一个绘图,然后将其保存为 .png

from matplotlib import pyplot as plt

fig, ax = plt.subplots(nrows=1, ncols=1)  # 创建图形和一个轴
ax.plot([0, 1, 2], [10, 20, 3])

fig.savefig('flowchart.png')  # 将绘图保存为图像并使用 .png

plt.close(fig)  # 关闭图形窗口

从头开始创建图表

有一些很好的库可以做到这一点。

正如你提到的 pypng 是其中之一,你可以在 https://pypng.readthedocs.io/en/latest/ex.html 上查看一些代码示例。

但我也进行了关于 pygame 的研究,你可以绘制图形,使用文本和颜色,所以这两个选项都很有趣。https://www.pygame.org/wiki/about

我将转贴与此相关的有趣代码:

''' pg_draw_circle_save101.py
绘制一个蓝色的实心圆在白色背景上
将绘图保存为图像文件
结果请参见 http://prntscr.com/156wxi
经由 vegaseat 于 2013 年 5 月 16 日测试,适用于 Python 2.7 和 PyGame 1.9.2
'''

import pygame as pg

# Pygame 使用 (r, g, b) 颜色元组
white = (255, 255, 255)
blue = (0, 0, 255)

width = 300
height = 300

# 创建显示窗口
win = pg.display.set_mode((width, height))
# 可选的标题栏标题
pg.display.set_caption("Pygame draw circle and save")
# 默认背景是黑色,所以将其改为白色
win.fill(white)

# 绘制一个蓝色圆圈
# 中心坐标 (x, y)
center = (width//2, height//2)
radius = min(center)
# 宽度为 0(默认)会填充圆
# 否则为轮廓的厚度
width = 0
# draw.circle(Surface, color, pos, radius, width)
pg.draw.circle(win, blue, center, radius, width)

# 现在保存绘图
# 可以保存为 .bmp、.tga、.png 或 .jpg
fname = "circle_blue.png"
pg.image.save(win, fname)
print("文件 {} 已保存".format(fname))

# 更新显示窗口以显示绘图
pg.display.flip()

# 事件循环和退出条件
# (按下 Esc 键或单击窗口标题栏的 x 以退出)
while True:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            # 单击 x 时最可靠的退出方式
            pg.quit()
            raise SystemExit
        elif event.type == pg.KEYDOWN:
            # 使用 Esc 键可选退出
            if event.key == pg.K_ESCAPE:
                pg.quit()
                raise SystemExit

注意:我已经将代码部分翻译为中文,而不是直接翻译成中文字符,以保持代码的准确性。

英文:

First of all, the description of your needs and your researches to try to find out how you can get it should be in the question. Just to help the community to answer you 创建PNG图像在Python中

There are some possible conclusions of your question.

Convert a chart into a .png

If you are thinking to create a chart with some library, one of the best way to do it is using matplotlib, this time i show a use with the module pyplot. You can create a plot, and then save it as .png

from matplotlib import pyplot as plt

fig, ax = plt.subplots( nrows=1, ncols=1 )  # create figure & 1 axis
ax.plot([0,1,2], [10,20,3])

fig.savefig('flowchart.png') # Save your plot as an image and use .png

plt.close(fig) #Close the figure window

Create a chart from scratch

There are some good libraries to do this.

As you told pypng is one of them, you can check some code examples where they work on https://pypng.readthedocs.io/en/latest/ex.html

But i have been researching about pygame, you can draw figures, use text, colours, so both options are interesting https://www.pygame.org/wiki/about.

i´ll repost interesting code related:

''' pg_draw_circle_save101.py
draw a blue solid circle on a white background
save the drawing to an image file
for result see http://prntscr.com/156wxi
tested with Python 2.7 and PyGame 1.9.2 by vegaseat  16may2013
'''

import pygame as pg

# pygame uses (r, g, b) color tuples
white = (255, 255, 255)
blue = (0, 0, 255)

width = 300
height = 300

# create the display window
win = pg.display.set_mode((width, height))
# optional title bar caption
pg.display.set_caption("Pygame draw circle and save")
# default background is black, so make it white
win.fill(white)

# draw a blue circle
# center coordinates (x, y)
center = (width//2, height//2)
radius = min(center)
# width of 0 (default) fills the circle
# otherwise it is thickness of outline
width = 0
# draw.circle(Surface, color, pos, radius, width)
pg.draw.circle(win, blue, center, radius, width)

# now save the drawing
# can save as .bmp .tga .png or .jpg
fname = "circle_blue.png"
pg.image.save(win, fname)
print("file {} has been saved".format(fname))

# update the display window to show the drawing
pg.display.flip()

# event loop and exit conditions
# (press escape key or click window title bar x to exit)
while True:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            # most reliable exit on x click
            pg.quit()
            raise SystemExit
        elif event.type == pg.KEYDOWN:
            # optional exit with escape key
            if event.key == pg.K_ESCAPE:
                pg.quit()
                raise SystemExit

huangapple
  • 本文由 发表于 2023年3月9日 18:50:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定