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 SQLAlchemy基本操作和常用技巧(包含大量实例,非常好)
May 06 Python
通过代码实例展示Python中列表生成式的用法
Mar 31 Python
Python Web编程之WSGI协议简介
Jul 18 Python
python对html过滤处理的方法
Oct 21 Python
python 实现selenium断言和验证的方法
Feb 13 Python
搭建python django虚拟环境完整步骤详解
Jul 08 Python
python使用socket实现的传输demo示例【基于TCP协议】
Sep 24 Python
在OpenCV里实现条码区域识别的方法示例
Dec 04 Python
利用OpenCV和Python实现查找图片差异
Dec 19 Python
如何在pycharm中安装第三方包
Oct 27 Python
PyCharm最新激活码(2020/10/27全网最新)
Oct 27 Python
基于Python实现股票收益率分析
Apr 02 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中根据变量的类型 选择echo或dump
2012/07/05 PHP
php检索或者复制远程文件的方法
2015/03/13 PHP
PHP简单实现断点续传下载的方法
2015/09/25 PHP
如何通过View::first使用Laravel Blade的动态模板详解
2017/09/21 PHP
PHP 中 var_export、print_r、var_dump 调试中的区别
2018/06/19 PHP
PHP PDOStatement::fetch讲解
2019/01/31 PHP
jquery formValidator插件ajax验证 内容不做任何修改再离开提示错误的bug解决方法
2013/01/04 Javascript
jQuery动画效果-slideUp slideDown上下滑动示例代码
2013/08/28 Javascript
jQuery实现简单的列表式导航菜单效果代码
2015/08/31 Javascript
javascript字符串对象常用api函数小结(连接,替换,分割,转换等)
2016/09/20 Javascript
js实现年月日表单三级联动
2020/04/17 Javascript
详解vue-cli脚手架build目录中的dev-server.js配置文件
2017/11/24 Javascript
vue、react等单页面项目部署到服务器的方法及vue和react的区别
2018/09/29 Javascript
vue中子组件传递数据给父组件的讲解
2019/01/27 Javascript
实现vuex与组件data之间的数据同步更新方式
2019/11/12 Javascript
jquery实现简单拖拽效果
2020/07/20 jQuery
Vue.extend 登录注册模态框的实现
2020/12/29 Vue.js
Python3实现连接SQLite数据库的方法
2014/08/23 Python
Python获取Linux系统下的本机IP地址代码分享
2014/11/07 Python
详解K-means算法在Python中的实现
2017/12/05 Python
python实现读Excel写入.txt的方法
2018/04/29 Python
Python使用Windows API创建窗口示例【基于win32gui模块】
2018/05/09 Python
Python编程在flask中模拟进行Restful的CRUD操作
2018/12/28 Python
Django REST framework视图的用法
2019/01/16 Python
PyQt5实现简易计算器
2020/05/30 Python
Python异常模块traceback用法实例分析
2019/10/22 Python
通过celery异步处理一个查询任务的完整代码
2019/11/19 Python
解决Python paramiko 模块远程执行ssh 命令 nohup 不生效的问题
2020/07/14 Python
Python -m参数原理及使用方法解析
2020/08/21 Python
Currentbody美国/加拿大:美容仪专家
2020/03/09 全球购物
大学生未来职业生涯规划书
2014/02/15 职场文书
投标承诺书范本
2014/03/27 职场文书
责任书格式范文
2014/07/28 职场文书
2015年幼儿园中班开学寄语
2015/05/27 职场文书
学校安全管理制度
2015/08/06 职场文书
Python装饰器的练习题
2021/11/23 Python