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 列表list使用介绍
Nov 30 Python
使用Python的Bottle框架写一个简单的服务接口的示例
Aug 25 Python
json跨域调用python的方法详解
Jan 11 Python
老生常谈Python序列化和反序列化
Jun 28 Python
Python字符串拼接六种方法介绍
Dec 18 Python
python实现抖音点赞功能
Apr 07 Python
不到20行代码用Python做一个智能聊天机器人
Apr 19 Python
Python使用ffmpy将amr格式的音频转化为mp3格式的例子
Aug 08 Python
python logging设置level失败的解决方法
Feb 19 Python
写好Python代码的几条重要技巧
May 21 Python
python_tkinter弹出对话框创建
Mar 20 Python
4种方法python批量修改替换列表中元素
Apr 07 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
动态新闻发布的实现及其技巧
2006/10/09 PHP
Win2003下APACHE+PHP5+MYSQL4+PHPMYADMIN 的简易安装配置
2006/11/18 PHP
ThinkPHP3.1新特性之对分组支持的改进与完善概述
2014/06/19 PHP
php保存信息到当前Session的方法
2015/03/16 PHP
详解yii2使用多个数据库的案例
2017/06/16 PHP
PHP实现动态压缩js与css文件的方法
2018/05/02 PHP
CSS+JS构建的图片查看器
2006/07/22 Javascript
?牟┛途W扣了一??效果出?? target=
2007/05/27 Javascript
jquery简单实现滚动条下拉DIV固定在头部不动
2013/11/25 Javascript
JS判断客户端是手机还是PC的2个代码
2014/04/12 Javascript
使用script的src实现跨域和类似ajax效果
2014/11/10 Javascript
javascript面向对象之this关键词用法分析
2015/01/13 Javascript
JS iFrame加载慢怎么解决
2016/05/13 Javascript
jquery实现无刷新验证码的简单实例
2016/05/19 Javascript
JS 组件系列之Bootstrap Table 冻结列功能IE浏览器兼容性问题解决方案
2017/06/30 Javascript
vue-infinite-loading2.0 中文文档详解
2018/04/08 Javascript
小程序瀑布流组件实现翻页与图片懒加载
2020/05/19 Javascript
antd-DatePicker组件获取时间值,及相关设置方式
2020/10/27 Javascript
解决ant Design Search无法输入内容的问题
2020/10/29 Javascript
JS实现页面侧边栏效果探究
2021/01/08 Javascript
[06:40]2014DOTA2西雅图国际邀请赛 DK战队巡礼
2014/07/07 DOTA
Python中字典的setdefault()方法教程
2017/02/07 Python
详解Python中的Numpy、SciPy、MatPlotLib安装与配置
2017/11/17 Python
Python的CGIHTTPServer交互实现详解
2018/02/08 Python
python多线程同步之文件读写控制
2021/02/25 Python
Python利用for循环打印星号三角形的案例
2020/04/12 Python
使用pymysql查询数据库,把结果保存为列表并获取指定元素下标实例
2020/05/15 Python
Python Selenium库的基本使用教程
2021/01/04 Python
Viking Direct荷兰:购买办公用品
2019/06/20 全球购物
SIMON MILLER官网:洛杉矶的生活方式品牌
2020/10/19 全球购物
大学生就业自荐信
2013/10/26 职场文书
中学教师教育感言
2014/02/21 职场文书
政治学求职信
2014/06/03 职场文书
远程培训的心得体会
2014/09/01 职场文书
入学证明
2015/06/23 职场文书
人生感悟经典句子
2019/08/20 职场文书