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自动化测试工具Splinter简介和使用实例
May 13 Python
Tornado协程在python2.7如何返回值(实现方法)
Jun 22 Python
python机器学习实战之K均值聚类
Dec 20 Python
python查看模块,对象的函数方法
Oct 16 Python
python使用插值法画出平滑曲线
Dec 15 Python
使用Python计算玩彩票赢钱概率
Jun 26 Python
python GUI库图形界面开发之PyQt5窗口背景与不规则窗口实例
Feb 25 Python
解决pycharm不能自动补全第三方库的函数和属性问题
Mar 12 Python
浅谈Python里面None True False之间的区别
Jul 09 Python
python缩进长度是否统一
Aug 02 Python
python 星号(*)的多种用途
Sep 21 Python
Python调用Redis的示例代码
Nov 24 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/07/28 PHP
PHP实现长文章分页实例代码(附源码)
2016/02/03 PHP
thinkPHP5项目中实现QQ第三方登录功能
2017/10/20 PHP
Aptana调试javascript图解教程
2009/11/30 Javascript
JavaScript isArray()函数判断对象类型的种种方法
2010/10/11 Javascript
jquery操作select option 的代码小结
2011/06/21 Javascript
jquery判断浏览器类型的代码
2012/11/05 Javascript
JS中图片缓冲loading技术的实例代码
2013/08/29 Javascript
window.onload追加函数使用示例
2014/03/03 Javascript
基于jQuery倒计时插件实现团购秒杀效果
2016/05/13 Javascript
js学习总结_选项卡封装(实例讲解)
2017/07/13 Javascript
详解如何构建Angular项目目录结构
2017/07/13 Javascript
浅谈es6语法 (Proxy和Reflect的对比)
2017/10/24 Javascript
vue父组件向子组件(props)传递数据的方法
2018/01/02 Javascript
javascript中的replace函数(带注释demo)
2018/01/07 Javascript
关于HTTP传输中gzip压缩的秘密探索分析
2018/01/12 Javascript
解决vue-router进行build无法正常显示路由页面的问题
2018/03/06 Javascript
浅谈Vue下使用百度地图的简易方法
2018/03/23 Javascript
vue js秒转天数小时分钟秒的实例代码
2018/08/08 Javascript
vue之延时刷新实例
2019/11/14 Javascript
vue.js实现简单的计算器功能
2020/02/22 Javascript
layui实现显示数据表格、搜索和修改功能示例
2020/06/03 Javascript
在Python中操作文件之truncate()方法的使用教程
2015/05/25 Python
NLTK 3.2.4 环境搭建教程
2018/09/19 Python
python的set处理二维数组转一维数组的方法示例
2019/05/31 Python
python匿名函数的使用方法解析
2019/10/10 Python
django 中使用DateTime常用的时间查询方式
2019/12/03 Python
Python基于字典实现switch case函数调用
2020/07/22 Python
Python利用Faiss库实现ANN近邻搜索的方法详解
2020/08/03 Python
求职简历中的自我评价分享
2013/12/08 职场文书
医院领导班子四风问题对照检查材料
2014/10/26 职场文书
2015年初一班主任工作总结
2015/05/13 职场文书
大学生党课心得体会
2016/01/07 职场文书
新课程改革心得体会
2016/01/22 职场文书
加薪申请书应该这样写!
2019/07/04 职场文书
解决numpy数组互换两行及赋值的问题
2021/04/17 Python