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实现控制台进度条功能
Jan 04 Python
Python内置的HTTP协议服务器SimpleHTTPServer使用指南
Mar 30 Python
Python中的字符串替换操作示例
Jun 27 Python
Python字典操作详细介绍及字典内建方法分享
Jan 04 Python
Python调用C++,通过Pybind11制作Python接口
Oct 16 Python
Python中的字符串切片(截取字符串)的详解
May 15 Python
python读写Excel表格的实例代码(简单实用)
Dec 19 Python
Matplotlib绘制雷达图和三维图的示例代码
Jan 07 Python
Python random模块制作简易的四位数验证码
Feb 01 Python
django-xadmin根据当前登录用户动态设置表单字段默认值方式
Mar 13 Python
在Sublime Editor中配置Python环境的详细教程
May 03 Python
python基于tkinter制作无损音乐下载工具
Mar 29 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小教程之实现链表
2014/06/09 PHP
PHP使用array_fill定义多维数组的方法
2015/03/18 PHP
PHP防止刷新重复提交页面的示例代码
2015/11/11 PHP
详解PHP的Yii框架中扩展的安装与使用
2016/04/01 PHP
PHP基于imagick扩展实现合成图片的两种方法【附imagick扩展下载】
2017/11/14 PHP
LaravelS通过Swoole加速Laravel/Lumen详解
2018/03/02 PHP
JS Pro-深入面向对象的程序设计之继承的详解
2013/05/07 Javascript
jquery特效 幻灯片效果示例代码
2013/07/16 Javascript
zeroclipboard 单个复制按钮和多个复制按钮的实现方法
2014/06/14 Javascript
javascript内置对象操作详解
2015/02/04 Javascript
javascript实现rgb颜色转换成16进制格式
2015/07/10 Javascript
JavaScript实现文字跟随鼠标特效
2015/08/06 Javascript
使用ionic播放轮询广告的实现方法(必看)
2017/04/24 Javascript
完美解决UI-Grid表格元素中多个空格显示为一个空格的问题
2017/04/25 Javascript
在 Node.js 中使用原生 ES 模块方法解析
2017/09/19 Javascript
js中document.write和document.writeln的区别
2018/03/11 Javascript
mpvue中使用flyjs全局拦截的实现代码
2018/09/13 Javascript
Python re模块介绍
2014/11/30 Python
python中sys.argv参数用法实例分析
2015/05/20 Python
python中使用序列的方法
2015/08/03 Python
深入理解Python中字典的键的使用
2015/08/19 Python
Python创建xml文件示例
2017/03/22 Python
python爬虫入门教程--正则表达式完全指南(五)
2017/05/25 Python
python找出一个列表中相同元素的多个索引实例
2019/06/11 Python
哈工大自然语言处理工具箱之ltp在windows10下的安装使用教程
2020/05/07 Python
Pytorch1.5.1版本安装的方法步骤
2020/12/31 Python
Groupon比利时官方网站:特卖和网上购物高达-70%
2019/08/09 全球购物
2014年家长学校工作总结
2014/11/20 职场文书
小学五年级语文上册教学计划
2015/01/22 职场文书
泰坦尼克号观后感
2015/06/04 职场文书
田径运动会通讯稿
2015/07/18 职场文书
社区结对共建协议书
2016/03/23 职场文书
2019年年中工作总结讲话稿模板
2019/03/25 职场文书
大学迎新生的欢迎词
2019/06/25 职场文书
JavaScript如何优化逻辑判断代码详解
2021/06/08 Javascript
Mysql分库分表之后主键处理的几种方法
2022/02/15 MySQL