Python Pygame (4) 图像的变换
Pygame中的transform模块可以使得你能够对图像(也就是Surface对象)做各种动作,列如左右上下翻转,按角度转动,放大缩小......,并返回Surface对象。这里列举了transform模块几个常用的方法及作用。

其实transform模块的这些方法使用的都是像素的变换,原理是通过一定的算法将图片进行像素位置修改。
大多数的方法在变换后难免造成一些精度上的损失(flip()方法不会)因此不建议对变换后的Surface对象进行再次变换。
例1
以之前小乌龟的例子,在这里使用smoothscale方法实现小乌龟的缩放:
# 放大、缩小小乌龟(=、-),空格键恢复原始尺寸
ratio = 1.0
oturtle = pygame.image.load("turtle.png")#oturtle用来保存最开始的图像
turtle = oturtle
oturtle_rect = oturtle.get_rect()
if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
# 最大只能放大一倍,缩小50%
if event.key == K_EQUALS and ratio < 2:
ratio += 0.1
if event.key == K_MINUS and ratio > 0.5:
ratio -= 0.1
if event.key == K_SPACE:
ratio = 1.0 turtle = pygame.transform.smoothscale(oturtle, \
(int(oturtle_rect.width * ratio), \
int(oturtle_rect.height * ratio)))#使用整形
#相应修改龟头两个朝向的Surface对象,否则单一移动就会打回原形
l_head = turtle
r_head = pygame.transform.flip(turtle, True, False)
例2:
使用rotate()方法实现小乌龟的贴地行走。rotate(Surface,angle)方法的第二个参数angle是用来指定旋转的角度,是逆时针角度,我们原来的图像是面朝左的图像,因而可以通过每次90度的逆时针方向的旋转来实现。
speed = [5, 0]
turtle_right = pygame.transform.rotate(turtle, 90)
turtle_top = pygame.transform.rotate(turtle, 180)
turtle_left = pygame.transform.rotate(turtle, 270)
turtle_bottom = turtle
turtle = turtle_top#刚开始走顶部 l_head = turtle
r_head = pygame.transform.flip(turtle, True, False) while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# 移动图像
position = position.move(speed) if position.right > width:
turtle = turtle_right
#变换后矩形的尺寸发生变化
position = turtle_rect = turtle.get_rect()
#矩形尺寸的改变导致位置也变化
position.left = width - turtle_rect.width
speed = [0, 5] if position.bottom > height:
turtle = turtle_bottom
position = turtle_rect = turtle.get_rect()
position.left = width - turtle_rect.width
position.top = height - turtle_rect.height
speed = [-5, 0] if position.left < 0:
turtle = turtle_left
position = turtle_rect = turtle.get_rect()
position.top = height - turtle_rect.height
speed = [0, -5] if position.top < 0:
turtle = turtle_top
position = turtle_rect = turtle.get_rect()
speed = [5, 0]
Python Pygame (4) 图像的变换的更多相关文章
- 【python图像处理】图像的缩放、旋转与翻转
[python图像处理]图像的缩放.旋转与翻转 图像的几何变换,如缩放.旋转和翻转等,在图像处理中扮演着重要的角色,python中的Image类分别提供了这些操作的接口函数,下面进行逐一介绍. 1.图 ...
- Making Games with Python & Pygame 中文翻译
Making Games with Python & Pygame 用Pygame做游戏 第1章-安装python和pygame 原文作者:Al Sweigart 翻译:bigbigli/李超 ...
- python+pygame游戏开发之使用Py2exe打包游戏
最近在用python+pygame 开发游戏,写完以后在分享给朋友玩的时候遇到了很大的问题,只有搭建了环境才能运行python脚本. 这会吓退99%以上的人……所以把我们的游戏打包(注意是打包而不是编 ...
- 用python+pygame写贪吃蛇小游戏
因为python语法简单好上手,前两天在想能不能用python写个小游戏出来,就上网搜了一下发现了pygame这个写2D游戏的库.了解了两天再参考了一些资料就开始写贪吃蛇这个小游戏. 毕竟最开始的练手 ...
- Matlab图像处理系列4———图像傅立叶变换与反变换
注:本系列来自于图像处理课程实验.用Matlab实现最主要的图像处理算法 1.Fourier变换 (1)频域增强 除了在空间域内能够加工处理图像以外.我们还能够将图像变换到其它空间后进行处理.这些方法 ...
- Win8 Metro(C#)数字图像处理--2.53图像傅立叶变换
原文:Win8 Metro(C#)数字图像处理--2.53图像傅立叶变换 [函数名称] 1,一维FFT变换函数 Complex[] FFT(Complex[] sourceDat ...
- python 多进程处理图像,充分利用CPU
默认情况下,Python程序使用一个CPU以单个进程运行.不过如果你是在最近几年配置的电脑,通常都是四核处理器,也就是有8个CPU.这就意味着在你苦苦等待Python脚本完成数据处理工作时,你的电脑其 ...
- 在图像中隐藏数据:用 Python 来实现图像隐写术
什么是“隐写术”? 隐写术是将机密信息隐藏在更大的信息中,使别人无法知道隐藏信息的存在以及隐藏信息内容的过程.隐写术的目的是保证双方之间的机密交流.与隐藏机密信息内容的密码学不同,隐写术隐瞒了传达消息 ...
- Python:PNG图像生成MP4
Python:PNG图像生成MP4 需求 需要将多张*.PNG图像,生成mp4格式的视频文件. 实现 利用Python中image库生成*.gif格式图像,但是图片未经压缩,文件体量较大. movie ...
随机推荐
- code#5 P1 报告
报告 时间限制: 1.0 秒 空间限制: 128 MB 相关文件: 题目目录 题目描述 企鹅高中有很多学生,自然管理起来也就非常麻烦.学校的教务处想要随时统计学校里面有多少个学生,但是他们只有很多 ...
- $.ajax 完整参数
jquery中的ajax方法参数 url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注意 ...
- VUE 采坑之旅-- Mint-ui 按需引入报出Module build failed: Error: Couldn't find preset "es2015" relative to directory "C:\\phpStudy\\PHPTutorial\\WWW\\text\\vuep\\vue-demo"
首先按照mint-ui的文档中按需引入的要求,先执行 npm install babel-plugin-component -D 然后,将.babelrc文件替换了,但是后来我又将其改了(采坑过程我也 ...
- Delphi Android USB Interface with the G2
来源:http://www.bverhue.nl/g2dev/?p=65 Delphi Android USB Interface with the G2 Leave a reply I first ...
- python paramiko模块和多线程讲解
1.paramiko 实现ssh 登录 import paramiko # 实现ssh登录 ssh_client = paramiko.SSHClient() ssh_client.set_missi ...
- 批处理之 for /f 中的delims和tokens
0x00 前言 今天在对windows进行提权之前的系统信息收集的时候,需要使用到一条批处理语句把特定部分的内容从一个txt的文本当中提取出来:该条语句是如下: for /f "tokens ...
- python教程(零)·前言
本教程是作者根据自己学习python的经验写下的,一来是想将经验分享给对python同样感兴趣的小白(大神请忽略),二来是想借此加深本人对python的理解,温故而知新. 学习基础 本教程面向的读者, ...
- PTA(BasicLevel)-1007素数对猜想
一 问题描述-素数对 让我们定义素数差dn为:dn=pn+1−pn,其中pi是第i个素数.显然有d1=1,且对于n>1有dn是偶数.“素数对猜想”认为“ ...
- openssl windows 下 编译 bat
++++全部++++++++ @echo offrem set sslpath=C:\0openssl\rem echo %sslpath% set X86_lib=C:\0openssl\32\li ...
- 选择区域缩放Flex Chart
http://www.riafan.com/zoom-chart/ 演示地址: http://www.riafan.com/flash/zoomchart/ 下载地址: http://www.riaf ...