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 相关文章推荐
合并Excel工作薄中成绩表的VBA代码,非常适合教育一线的朋友
Apr 09 Python
在Docker上开始部署Python应用的教程
Apr 17 Python
Python读取指定目录下指定后缀文件并保存为docx
Apr 23 Python
python内置函数:lambda、map、filter简单介绍
Nov 16 Python
python 提取key 为中文的json 串方法
Dec 31 Python
Python生成rsa密钥对操作示例
Apr 26 Python
python占位符输入方式实例
May 27 Python
Python 生成一个从0到n个数字的列表4种方法小结
Nov 28 Python
keras获得model中某一层的某一个Tensor的输出维度教程
Jan 24 Python
Python 统计位数为偶数的数字代码详解
Mar 15 Python
pip/anaconda修改镜像源,加快python模块安装速度的操作
Mar 04 Python
Python中else的三种使用场景
Jun 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
晶体管来复再生式二管收音机
2021/03/02 无线电
php中常用字符串处理代码片段整理
2011/11/07 PHP
PHP微信开发之根据用户回复关键词\位置返回附近信息
2016/06/24 PHP
php实现学生管理系统
2020/03/21 PHP
php封装的page分页类完整实例
2016/10/18 PHP
php实现用户注册密码的crypt加密
2017/06/08 PHP
PHP中Session ID的实现原理实例分析
2019/08/17 PHP
php多进程并发编程防止出现僵尸进程的方法分析
2020/02/28 PHP
JavaScript 异步调用框架 (Part 4 - 链式调用)
2009/08/04 Javascript
jQuery 使用手册(四)
2009/09/23 Javascript
js 强制弹出窗口代码研究-又一款代码
2010/03/20 Javascript
IE下js调试工具Companion.JS
2010/10/15 Javascript
动态添加option及createElement使用示例
2014/01/26 Javascript
JS实现超简洁网页title标题跑动闪烁提示效果代码
2015/10/23 Javascript
jQuery实现点击按钮文字变成input框点击保存变成文字
2016/05/09 Javascript
jQuery模仿单选按钮选中效果
2016/06/24 Javascript
PHP抓取HTTPS内容和错误处理的方法
2016/09/30 Javascript
使用 bootstrap modal遇到的问题小结
2016/11/09 Javascript
详解Node.js实现301、302重定向服务
2017/04/07 Javascript
vue做移动端适配最佳解决方案(亲测有效)
2018/09/04 Javascript
基于AngularJs select绑定数字类型的问题
2018/10/08 Javascript
判断js数据类型的函数实例详解
2019/05/23 Javascript
vue实践---根据不同环境,自动转换请求的url地址操作
2020/09/21 Javascript
Vue3+elementui plus创建项目的方法
2020/12/01 Vue.js
Python爬虫信息输入及页面的切换方法
2018/05/11 Python
python自动化测试之DDT数据驱动的实现代码
2019/07/23 Python
tensorflow 实现自定义layer并添加到计算图中
2020/02/04 Python
python3.8.1+selenium实现登录滑块验证功能
2020/05/22 Python
HTML5实现经典坦克大战坦克乱走还能发出一个子弹
2013/09/02 HTML / CSS
采用专利算法搜索最廉价的机票:CheapAir
2016/09/10 全球购物
日语专业推荐信
2013/11/12 职场文书
个人实习生的自我评价
2014/02/16 职场文书
公务员诚信承诺书
2014/05/26 职场文书
分公司总经理岗位职责
2014/07/30 职场文书
2015年第31个教师节致辞
2015/07/31 职场文书
如何获取numpy array前N个最大值
2021/05/14 Python