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实现配置文件备份的方法
Jul 30 Python
Python Requests 基础入门
Apr 07 Python
python爬虫入门教程--利用requests构建知乎API(三)
May 25 Python
Python学习笔记之if语句的使用示例
Oct 23 Python
Python Numpy 数组的初始化和基本操作
Mar 13 Python
Python实现图片拼接的代码
Jul 02 Python
python如何解析配置文件并应用到项目中
Jun 27 Python
基于Python实现剪切板实时监控方法解析
Sep 11 Python
详解Django将秒转换为xx天xx时xx分
Sep 27 Python
Python 文件操作之读取文件(read),文件指针与写入文件(write),文件打开方式示例
Sep 29 Python
python实现二分类的卡方分箱示例
Nov 22 Python
python应用Axes3D绘图(批量梯度下降算法)
Mar 25 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+mysql 采用ajax技术的 省 市 地 3级联动无刷新菜单 源码
2006/12/16 PHP
wiki-shan写的php在线加密的解密程序
2008/09/07 PHP
php中取得URL的根域名的代码
2011/03/23 PHP
web server使用php生成web页面的三种方法总结
2013/10/28 PHP
PHP限制页面只能在微信自带浏览器访问的代码
2014/01/15 PHP
一个php生成16位随机数的代码(两种方法)
2014/09/16 PHP
php学习笔记之面向对象
2014/11/08 PHP
php curl请求信息和返回信息设置代码实例
2015/04/27 PHP
PHP获取当前日期及本周一是几月几号的方法
2017/03/28 PHP
yii2项目实战之restful api授权验证详解
2017/05/20 PHP
javascript基础第一章 JavaScript与用户端
2010/07/22 Javascript
用JavaScript仿PS里的羽化效果代码
2011/12/20 Javascript
Jquery 实现表格颜色交替变化鼠标移过颜色变化实例
2013/08/28 Javascript
jquery中对于批量deferred的处理方法
2014/01/22 Javascript
详解JavaScript中setSeconds()方法的使用
2015/06/11 Javascript
Angularjs中如何使用filterFilter函数过滤
2016/02/06 Javascript
jQuery中常用动画效果函数(日常整理)
2016/09/17 Javascript
微信小程序 wxapp内容组件 progress详细介绍
2016/10/31 Javascript
详解React Native顶|底部导航使用小技巧
2017/09/14 Javascript
Spring Boot/VUE中路由传递参数的实现代码
2018/03/02 Javascript
React.js绑定this的5种方法(小结)
2018/06/05 Javascript
JS实现点击按钮可实现编辑功能
2018/07/03 Javascript
JavaScript之解构赋值的理解
2019/01/30 Javascript
Python新手实现2048小游戏
2015/03/31 Python
spark dataframe 将一列展开,把该列所有值都变成新列的方法
2019/01/29 Python
centos6.5安装python3.7.1之后无法使用pip的解决方案
2019/02/14 Python
Python semaphore evevt生产者消费者模型原理解析
2020/03/18 Python
利用python 下载bilibili视频
2020/11/13 Python
CSS3 Media Queries详细介绍和使用实例
2014/05/08 HTML / CSS
阿迪达斯荷兰官方网站:adidas荷兰
2018/03/16 全球购物
Hashtable 添加内容的方式有哪几种,有什么区别?
2012/04/08 面试题
大学生党员自我批评
2014/02/14 职场文书
奥巴马获胜演讲稿
2014/05/15 职场文书
2014党员学习习主席讲话思想汇报
2014/09/15 职场文书
2015年助残日活动总结
2015/03/27 职场文书
vue route新窗口跳转页面并且携带与接收参数
2022/04/10 Vue.js