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


Posted in Python onApril 24, 2015

本文实例讲述了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 相关文章推荐
Python中的变量和作用域详解
Jul 13 Python
Python数据结构与算法之图的广度优先与深度优先搜索算法示例
Dec 14 Python
python使用xpath中遇到:到底是什么?
Jan 04 Python
python自动化报告的输出用例详解
May 30 Python
Flask框架使用DBUtils模块连接数据库操作示例
Jul 20 Python
python 通过麦克风录音 生成wav文件的方法
Jan 09 Python
Python3多目标赋值及共享引用注意事项
May 27 Python
django框架使用orm实现批量更新数据的方法
Jun 21 Python
python打包成so文件过程解析
Sep 28 Python
python处理excel绘制雷达图
Oct 18 Python
python文件绝对路径写法介绍(windows)
Dec 25 Python
详解使用Python写一个向数据库填充数据的小工具(推荐)
Sep 11 Python
python使用PIL缩放网络图片并保存的方法
Apr 24 #Python
python使用Tkinter显示网络图片的方法
Apr 24 #Python
Python中最常用的操作列表的几种方法归纳
Apr 24 #Python
在Python中使用lambda高效操作列表的教程
Apr 24 #Python
使用Python的判断语句模拟三目运算
Apr 24 #Python
Python的字典和列表的使用中一些需要注意的地方
Apr 24 #Python
整理Python最基本的操作字典的方法
Apr 24 #Python
You might like
比较时间段一与时间段二是否有交集的php函数
2011/05/31 PHP
php中如何同时使用session和cookie来保存用户登录信息
2013/07/05 PHP
PHP curl实现抓取302跳转后页面的示例
2014/07/04 PHP
浅析php适配器模式(Adapter)
2014/11/25 PHP
CI框架中数据库操作函数$this->db->where()相关用法总结
2016/05/17 PHP
php调用云片网接口发送短信的实现方法
2017/10/25 PHP
详解phpstorm2020最新破解方法
2020/09/17 PHP
JavaScript创建命名空间(namespace)的最简实现
2007/12/11 Javascript
用js来解决ajax读取页面乱码
2010/11/28 Javascript
Vue.js中用webpack合并打包多个组件并实现按需加载
2017/02/17 Javascript
JavaScript实现指定数量的并发限制的示例代码
2020/03/10 Javascript
解决qrcode.js生成二维码时必须定义一个空div的问题
2020/07/09 Javascript
webpack+vue-cil 中proxyTable配置接口地址代理操作
2020/07/18 Javascript
Openlayers3实现车辆轨迹回放功能
2020/09/29 Javascript
[01:11:28]DOTA2-DPC中国联赛定级赛 RNG vs Phoenix BO3第一场 1月8日
2021/03/11 DOTA
python爬虫正则表达式之处理换行符
2018/06/08 Python
python3+selenium自动化测试框架详解
2019/03/17 Python
python 画出使用分类器得到的决策边界
2019/08/21 Python
在OpenCV里实现条码区域识别的方法示例
2019/12/04 Python
Python同时处理多个异常的方法
2020/07/28 Python
Python字符串三种格式化输出
2020/09/17 Python
HTML5梦幻之旅——炫丽的流星雨效果实现过程
2013/08/06 HTML / CSS
使用HTML5的Notification API制作web通知的教程
2015/05/08 HTML / CSS
英国鲜花速递:Serenata Flowers
2018/04/03 全球购物
C语言怎样定义和声明全局变量和函数最好
2013/11/26 面试题
元宵节主持词
2014/03/25 职场文书
教师一帮一活动总结
2014/07/08 职场文书
区长工作作风个人整改措施
2014/10/01 职场文书
解放思想大讨论活动总结
2015/05/09 职场文书
党支部考察意见范文
2015/06/02 职场文书
学习新党章心得体会2016
2016/01/15 职场文书
导游词之临安白水涧
2019/11/05 职场文书
2019年最新感恩节祝福语(28句)
2019/11/27 职场文书
Java数据开发辅助工具Docker与普通程序使用方法
2021/09/15 Java/Android
浅析python中特殊文件和特殊函数
2022/02/24 Python
基于Python编写简易版的天天跑酷游戏的示例代码
2022/03/23 Python