python使用PyGame绘制图像并保存为图片文件的方法

作者:feiwen 时间:2023-05-13 16:17:03 

本文实例讲述了python使用PyGame绘制图像并保存为图片文件的方法。分享给大家供大家参考。具体实现方法如下:


''' 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

希望本文所述对大家的Python程序设计有所帮助。

标签:python,PyGame,图片
0
投稿

猜你喜欢

  • IIS+PHP添加对webp格式图像的支持配置方法

    2023-05-28 11:20:11
  • 使用python根据端口号关闭进程的方法

    2022-12-26 01:58:41
  • ES6深入理解之“let”能替代”var“吗?

    2024-05-28 15:41:33
  • 深入理解JavaScript系列(10) JavaScript核心(晋级高手必读篇)

    2024-04-22 13:24:54
  • python+mysql实现简单的web程序

    2024-01-15 12:31:58
  • golang基于websocket实现的简易聊天室程序

    2023-06-15 00:27:01
  • 详解Go中Map类型和Slice类型的传递

    2024-04-23 09:47:03
  • Request.ServerVariables("HTTP_REFERER")的用法

    2008-06-19 13:33:00
  • 浅析MySQL并行复制

    2024-01-13 02:54:10
  • ORACLE8的分区管理

    2010-07-30 13:18:00
  • Python3简单实例计算同花的概率代码

    2023-07-20 16:23:03
  • 详解pygame中Rect对象

    2021-01-09 04:37:58
  • CSS绝对定位在宽屏分辨率下错位

    2009-07-28 12:24:00
  • Python操作JSON实现网络数据交换

    2023-01-27 02:21:53
  • insert和select结合实现"插入某字段在数据库中的最大值+1"的方法

    2024-01-25 07:52:46
  • Mysql 执行一条语句的整个过程详细

    2024-01-19 08:53:08
  • 前端优化,让你的网页显示的更快更流畅

    2009-06-08 13:09:00
  • python如何从键盘获取输入实例

    2021-02-21 07:21:47
  • 设定sql server定期自动备份数据库

    2024-01-16 18:14:30
  • MySQL动态SQL拼接实例详解

    2024-01-20 15:13:55
  • asp之家 网络编程 m.aspxhome.com