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装饰器
May 09 Python
详解python如何调用C/C++底层库与互相传值
Aug 10 Python
Python求出0~100以内的所有素数
Jan 23 Python
python使用turtle绘制分形树
Jun 22 Python
PyCharm的设置方法和第一个Python程序的建立
Jan 16 Python
python实现列表中最大最小值输出的示例
Jul 09 Python
详解Django将秒转换为xx天xx时xx分
Sep 27 Python
python使用HTMLTestRunner导出饼图分析报告的方法
Dec 30 Python
使用python 的matplotlib 画轨道实例
Jan 19 Python
如何使用Cython对python代码进行加密
Jul 08 Python
python给list排序的简单方法
Dec 10 Python
Python基础之常用库常用方法整理
Apr 30 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
收音机另类DIY - 纸巾盒做外壳
2021/03/02 无线电
用PHP实现文件上传二法
2006/10/09 PHP
php数组应用之比较两个时间的相减排序
2008/08/18 PHP
让的PHP代码飞起来的40条小技巧(提升php效率)
2010/04/12 PHP
php设计模式之组合模式实例详解【星际争霸游戏案例】
2020/03/27 PHP
jquery判断字符输入个数(数字英文长度记为1,中文记为2,超过长度自动截取)
2010/10/15 Javascript
IE6、IE7中获取Button元素的值的bug说明
2011/08/28 Javascript
JavaScript获取onclick、onchange等事件值的代码
2013/07/22 Javascript
2种jQuery 实现刮刮卡效果
2015/02/01 Javascript
jQuery实现复选框批量选择与反选的方法
2015/06/17 Javascript
JavaScript节点及列表操作实例小结
2015/08/05 Javascript
js实现带圆角的两级导航菜单效果代码
2015/08/24 Javascript
AngularJS基础 ng-class-odd 指令示例
2016/08/01 Javascript
利用js给datalist或select动态添加option选项的方法
2018/01/25 Javascript
JavaScript中的ES6 Proxy的具体使用
2019/06/16 Javascript
详解vue-cli项目在IE浏览器打开报错解决方法
2020/12/10 Vue.js
聊聊vue 中的v-on参数问题
2021/01/29 Vue.js
python实现2014火车票查询代码分享
2014/01/10 Python
Python 描述符(Descriptor)入门
2016/11/20 Python
python技能之数据导出excel的实例代码
2017/08/11 Python
python脚本开机自启的实现方法
2019/06/28 Python
python使用Qt界面以及逻辑实现方法
2019/07/10 Python
用python求一重积分和二重积分的例子
2019/12/06 Python
英国最大的奢侈珠宝和手表网站:C W Sellors
2017/02/10 全球购物
英国家电购物网站:Sonic Direct
2019/03/26 全球购物
联想英国官网:Lenovo英国
2019/07/17 全球购物
十佳教师事迹材料
2014/01/11 职场文书
自我鉴定总结
2014/03/24 职场文书
《真想变成大大的荷叶》教学反思
2014/04/14 职场文书
小学优秀教育工作者事迹材料
2014/05/09 职场文书
2014年教师党员自我评价范文
2014/09/22 职场文书
创新创业项目计划书该怎样写?
2019/08/13 职场文书
HTML中table表格拆分合并(colspan、rowspan)
2021/04/07 HTML / CSS
vue2实现provide inject传递响应式
2021/05/21 Vue.js
浅谈MySql update会锁定哪些范围的数据
2022/06/25 MySQL
基于Android10渲染Surface的创建过程
2022/08/14 Java/Android