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 20 Python
python Flask 装饰器顺序问题解决
Aug 08 Python
Python 使用多属性来进行排序
Sep 01 Python
python通过移动端访问查看电脑界面
Jan 06 Python
python实现梯度下降和逻辑回归
Mar 24 Python
Python接口开发实现步骤详解
Apr 26 Python
解决keras使用cov1D函数的输入问题
Jun 29 Python
Python编写单元测试代码实例
Sep 10 Python
Python+OpenCV图像处理——实现轮廓发现
Oct 23 Python
python实现三壶谜题的示例详解
Nov 02 Python
Matplotlib animation模块实现动态图
Feb 25 Python
Python xlwings插入Excel图片的实现方法
Feb 26 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
PL-880隐藏功能
2021/03/01 无线电
phpfans留言版用到的数据操作类和分页类
2007/01/04 PHP
基于PHP读取TXT文件向数据库导入海量数据的方法
2013/04/23 PHP
php使用codebase生成随机数
2014/03/25 PHP
PHP 7的一些引人注目的新特性简单介绍
2015/11/08 PHP
PHP实现长文章分页实例代码(附源码)
2016/02/03 PHP
linux下php上传文件注意事项
2016/06/11 PHP
PHP生成短网址的思路以及实现方法的详解
2019/03/25 PHP
pjblog中的UBBCode.js
2007/04/25 Javascript
js 分栏效果实现代码
2009/08/29 Javascript
javascript 模拟点击广告
2010/01/02 Javascript
js 禁用只读文本框获得焦点时的退格键
2010/04/25 Javascript
jQuery 源码分析笔记(3) Deferred机制
2011/06/19 Javascript
javascript删除option选项的多种方法总结
2013/11/22 Javascript
跟我学习javascript的隐式强制转换
2015/11/16 Javascript
Vue学习笔记进阶篇之vue-cli安装及介绍
2017/07/18 Javascript
Vue之Watcher源码解析(2)
2017/07/19 Javascript
基于JavaScript实现淘宝商品广告效果
2017/08/10 Javascript
ligerUI---ListBox(列表框可移动的实例)
2017/11/28 Javascript
浅谈vuejs实现数据驱动视图原理
2018/02/23 Javascript
JavaScript使用表单元素验证表单的示例代码
2019/08/20 Javascript
微信小程序自定义菜单切换栏tabbar组件代码实例
2019/12/30 Javascript
vue v-on:click传递动态参数的步骤
2020/09/11 Javascript
[49:43]VG vs FNATIC 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/17 DOTA
使用Python编写一个在Linux下实现截图分享的脚本的教程
2015/04/24 Python
Python中 传递值 和 传递引用 的区别解析
2018/02/22 Python
详解Python中pandas的安装操作说明(傻瓜版)
2019/04/08 Python
Python二维码生成识别实例详解
2019/07/16 Python
Python urlencode和unquote函数使用实例解析
2020/03/31 Python
Python使用urlretrieve实现直接远程下载图片的示例代码
2020/08/17 Python
Pure Collection美国官网:来自英国羊绒专家的奢华羊绒
2017/11/19 全球购物
计算机专业学生求职信分享
2013/12/15 职场文书
求职信的最佳写作思路
2014/02/01 职场文书
同事打架检讨书
2015/05/06 职场文书
2015秋季开学典礼主持词
2015/07/16 职场文书
百善孝为先:关于孝道的经典语录
2019/10/18 职场文书