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人人网登录应用实例
Sep 26 Python
Python实现删除文件但保留指定文件
Jun 21 Python
python生成excel的实例代码
Nov 08 Python
python写一个md5解密器示例
Feb 23 Python
对Python 3.5拼接列表的新语法详解
Nov 08 Python
对python中的装包与解包实例详解
Aug 24 Python
详解Python3 pickle模块用法
Sep 16 Python
python实现连续变量最优分箱详解--CART算法
Nov 22 Python
Python内置方法实现字符串的秘钥加解密(推荐)
Dec 09 Python
浅析python函数式编程
Sep 26 Python
详解Python 中的 defaultdict 数据类型
Feb 22 Python
python代码实现扫码关注公众号登录的实战
Nov 01 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的开发框架的现状和展望
2007/03/16 PHP
劣质的PHP代码简化
2010/02/08 PHP
php处理json时中文问题的解决方法
2011/04/12 PHP
PHP判断是手机端还是PC端 PHP判断是否是微信浏览器
2017/03/15 PHP
PHP使用GD库制作验证码的方法(点击验证码或看不清会刷新验证码)
2017/08/15 PHP
PHPExcel实现的读取多工作表操作示例
2020/04/14 PHP
对象的类型:本地对象(1)
2006/12/29 Javascript
一个判断email合法性的函数[非正则]
2008/12/09 Javascript
使用jQuery实现dropdownlist的联动效果(sharepoint 2007)
2011/03/30 Javascript
基于jquery的当鼠标滚轮到最底端继续加载新数据思路分享(多用于微博、空间、论坛 )
2011/10/10 Javascript
jQuery html()方法使用不了无法显示内容的问题
2014/08/06 Javascript
分享javascript计算时间差的示例代码
2020/03/19 Javascript
jQuery图片旋转插件jQueryRotate.js用法实例(附demo下载)
2016/01/21 Javascript
js实现密码强度检测【附示例】
2016/03/30 Javascript
jquery基本选择器匹配多个元素的实现方法
2016/09/05 Javascript
利用jquery实现下拉框的禁用与启用
2016/12/07 Javascript
在vue-cli中组件通信的方法
2017/12/16 Javascript
Nginx设置为Node.js的前端服务器方法总结
2019/03/27 Javascript
Javascript Web Worker使用过程解析
2020/03/16 Javascript
基于vue3.0.1beta搭建仿京东的电商H5项目
2020/05/06 Javascript
[55:44]OG vs NAVI 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/17 DOTA
python使用点操作符访问字典(dict)数据的方法
2015/03/16 Python
Python列表list操作符实例分析【标准类型操作符、切片、连接字符、列表解析、重复操作等】
2017/07/24 Python
Django中ORM外键和表的关系详解
2019/05/20 Python
Python socket处理client连接过程解析
2020/03/18 Python
Python requests及aiohttp速度对比代码实例
2020/07/16 Python
在pycharm中使用pipenv创建虚拟环境和安装django的详细教程
2020/11/30 Python
开普敦通行证:Cape Town Pass
2019/07/18 全球购物
精彩自我鉴定
2014/01/16 职场文书
教师产假请假条
2014/04/10 职场文书
2014年居委会工作总结
2014/12/09 职场文书
员工辞职信范文大全
2015/05/12 职场文书
幼儿园大班教育随笔
2015/08/14 职场文书
军训决心书范文
2015/09/22 职场文书
《风娃娃》教学反思
2016/02/18 职场文书
vue项目打包后路由错误的解决方法
2022/04/13 Vue.js