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的Cookie.py模块支持冒号做key的方法
Dec 28 Python
Python类属性的延迟计算
Oct 22 Python
Python网络爬虫中的同步与异步示例详解
Feb 03 Python
pygame游戏之旅 载入小车图片、更新窗口
Nov 20 Python
简单了解python的内存管理机制
Jul 08 Python
使用python模拟命令行终端的示例
Aug 13 Python
Django和Flask框架优缺点对比
Oct 24 Python
详谈tensorflow gfile文件的用法
Feb 05 Python
python 使用raw socket进行TCP SYN扫描实例
May 05 Python
解决pycharm安装第三方库失败的问题
May 09 Python
Pytorch转keras的有效方法,以FlowNet为例讲解
May 26 Python
python中sys模块是做什么用的
Aug 16 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函数的实现原理及性能分析(一)
2015/05/13 PHP
PHP微信开发之模板消息回复
2016/06/24 PHP
PHP入门教程之PHP操作MySQL的方法分析
2016/09/11 PHP
用JQuery 实现的自定义对话框
2007/03/24 Javascript
Prototype PeriodicalExecuter对象 学习
2009/07/19 Javascript
单击按钮显示隐藏子菜单经典案例
2013/01/04 Javascript
JS实现图片放大镜效果的方法
2015/02/27 Javascript
Jquery中基本选择器用法实例详解
2015/05/18 Javascript
js实现文字滚动效果
2016/03/03 Javascript
Javascript中的迭代、归并方法详解
2016/06/14 Javascript
Javascript之深入浅出prototype
2017/02/06 Javascript
使用vue-router beforEach实现判断用户登录跳转路由筛选功能
2018/06/25 Javascript
springMvc 前端用json的方式向后台传递对象数组方法
2018/08/07 Javascript
用VueJS写一个Chrome浏览器插件的实现方法
2019/02/27 Javascript
Jquery的autocomplete插件用法及参数讲解
2019/03/12 jQuery
基于JS判断对象是否是数组
2020/01/10 Javascript
vue项目中使用eslint+prettier规范与检查代码的方法
2020/01/16 Javascript
JS数组方法shift()、unshift()用法实例分析
2020/01/18 Javascript
[39:32]2014 DOTA2国际邀请赛中国区预选赛 TongFu VS DT 第二场
2014/05/23 DOTA
[01:07:13]TNC vs Pain 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
Python运行的17个时新手常见错误小结
2012/08/07 Python
Python实现的检测web服务器健康状况的小程序
2014/09/17 Python
Python实现导出数据生成excel报表的方法示例
2017/07/12 Python
python 字符串只保留汉字的方法
2018/11/16 Python
python 动态迁移solr数据过程解析
2019/09/04 Python
Pandas DataFrame中的tuple元素遍历的实现
2019/10/23 Python
详解在Python中使用Torchmoji将文本转换为表情符号
2020/07/27 Python
Bench加拿大官方网站:英国城市服装品牌
2017/11/03 全球购物
Everything But Water官网:美国泳装品牌
2019/03/17 全球购物
财务会计人员岗位职责
2013/11/30 职场文书
我的理想演讲稿
2014/04/30 职场文书
基层党员公开承诺书
2014/05/29 职场文书
学校安全管理责任书
2014/07/23 职场文书
2014国庆节商场促销活动策划方案
2014/09/16 职场文书
公司车辆管理制度
2015/08/04 职场文书
vue中控制mock在开发环境使用,在生产环境禁用方式
2022/04/06 Vue.js