python小游戏编程arcade----坦克动画图片合成
创始人
2024-03-04 11:59:06
0

python小游戏编程arcade----坦克动画图片合成

    • 前言
    • 坦克动画图片合成
      • 1、PIL image
        • 1.1 读取文件并转换
        • 1.2 裁切,粘贴
        • 1.3 效果图
        • 1.4 代码实现
      • 2、处理图片的透明度问题
        • 2.1 past 函数的三个参数
        • 2.2 注意点1
        • 2.3 注意点2
        • 2.4 效果![在这里插入图片描述](http://www.shanghaiyincai.com.cn/webdata/wwwroot/pics.8red.cn/weishitang/202403/a57d8099740359e.png)
        • 2.4 代码实现

前言

接上篇文章继续解绍arcade游戏编程的基本知识。如何通过程序合成所需的动画图片

坦克动画图片合成

游戏素材
在这里插入图片描述
如何通过程序合成所需的动画图片

1、PIL image

1.1 读取文件并转换

from PIL import Image
img = Image.open(“images/tank.png”).convert(“RGBA”) #读取系统的内照片

1.2 裁切,粘贴

    def crop(self, box=None):"""Returns a rectangular region from this image. The box is a4-tuple defining the left, upper, right, and lower pixelcoordinate. See :ref:`coordinate-system`.Note: Prior to Pillow 3.4.0, this was a lazy operation.:param box: The crop rectangle, as a (left, upper, right, lower)-tuple.:rtype: :py:class:`~PIL.Image.Image`:returns: An :py:class:`~PIL.Image.Image` object."""if box is None:return self.copy()if box[2] < box[0]:raise ValueError("Coordinate 'right' is less than 'left'")elif box[3] < box[1]:raise ValueError("Coordinate 'lower' is less than 'upper'")self.load()return self._new(self._crop(self.im, box))

crop函数带的参数为(起始点的横坐标,起始点的纵坐标,宽度,高度)
paste函数的参数为(需要修改的图片,粘贴的起始点的横坐标,粘贴的起始点的纵坐标)

1.3 效果图

在这里插入图片描述

1.4 代码实现

from PIL import Image
import colorsysi = 1
j = 1
img = Image.open("images/tank.png").convert("RGBA") #读取系统的内照片box=(0,179,185,256)
pp = img.crop(box)
box=(0,132,175,179)
pp2 = img.crop(box)
print(pp2.size)
pp2.show()
nimg= Image.new("RGBA",(200,200))
nimg.paste(pp)
nimg.show()
# nimg.putalpha(pp2)
nimg.paste(pp2,(5,47,180,94))nimg.show()

透明度不对

2、处理图片的透明度问题

2.1 past 函数的三个参数

    def paste(self, im, box=None, mask=None):"""Pastes another image into this image. The box argument is eithera 2-tuple giving the upper left corner, a 4-tuple defining theleft, upper, right, and lower pixel coordinate, or None (same as(0, 0)). See :ref:`coordinate-system`. If a 4-tuple is given, the sizeof the pasted image must match the size of the region.If the modes don't match, the pasted image is converted to the mode ofthis image (see the :py:meth:`~PIL.Image.Image.convert` method fordetails).Instead of an image, the source can be a integer or tuplecontaining pixel values.  The method then fills the regionwith the given color.  When creating RGB images, you canalso use color strings as supported by the ImageColor module.If a mask is given, this method updates only the regionsindicated by the mask. You can use either "1", "L", "LA", "RGBA"or "RGBa" images (if present, the alpha band is used as mask).Where the mask is 255, the given image is copied as is.  Wherethe mask is 0, the current value is preserved.  Intermediatevalues will mix the two images together, including their alphachannels if they have them.See :py:meth:`~PIL.Image.Image.alpha_composite` if you want tocombine images with respect to their alpha channels.:param im: Source image or pixel value (integer or tuple).:param box: An optional 4-tuple giving the region to paste into.If a 2-tuple is used instead, it's treated as the upper leftcorner.  If omitted or None, the source is pasted into theupper left corner.If an image is given as the second argument and there is nothird, the box defaults to (0, 0), and the second argumentis interpreted as a mask image.:param mask: An optional mask image."""if isImageType(box) and mask is None:# abbreviated paste(im, mask) syntaxmask = boxbox = Noneif box is None:box = (0, 0)if len(box) == 2:# upper left corner given; get size from image or maskif isImageType(im):size = im.sizeelif isImageType(mask):size = mask.sizeelse:# FIXME: use self.size here?raise ValueError("cannot determine region size; use 4-item box")box += (box[0] + size[0], box[1] + size[1])if isinstance(im, str):from . import ImageColorim = ImageColor.getcolor(im, self.mode)elif isImageType(im):im.load()if self.mode != im.mode:if self.mode != "RGB" or im.mode not in ("LA", "RGBA", "RGBa"):# should use an adapter for this!im = im.convert(self.mode)im = im.imself._ensure_mutable()if mask:mask.load()self.im.paste(im, box, mask.im)else:self.im.paste(im, box)

2.2 注意点1

原图读取时要有透明度层的数据
在这里插入图片描述
img = Image.open(“images/tank.png”).convert(“RGBA”) #读取系统的内照片

2.3 注意点2

pp2 = img.crop(box)
#分离通道
r, g, b, a = pp2.split()
#粘贴要加mask
nimg.paste(pp2,(5,47,180,94),mask=a)

2.4 效果在这里插入图片描述

微调数据
在这里插入图片描述

2.4 代码实现

# _*_ coding: UTF-8 _*_
# 开发团队: 信息化未来
# 开发人员: Administrator
# 开发时间:2022/11/30 20:17
# 文件名称: 图片合成.py
# 开发工具: PyCharmfrom PIL import Image
import colorsysi = 1
j = 1
img = Image.open("images/tank.png").convert("RGBA") #读取系统的内照片box=(0,179,185,256)
pp = img.crop(box)
print(pp.size)
box=(0,132,175,179)
pp2 = img.crop(box)
r, g, b, a = pp2.split()
print(pp2.size)
pp2.show()
nimg= Image.new("RGBA",(200,200))
nimg.paste(pp,(0,123,185,200))
nimg.show()
# nimg.putalpha(pp2)
nimg.paste(pp2,(0,153,175,200),mask=a)nimg.show()

今天是以此模板持续更新此育儿专栏的第 39/50次。
可以关注我,点赞我、评论我、收藏我啦。

相关内容

热门资讯

美国2年期国债收益率上涨15个... 原标题:美国2年期国债收益率上涨15个基点 美国2年期国债收益率上涨15个基...
汽车油箱结构是什么(汽车油箱结... 本篇文章极速百科给大家谈谈汽车油箱结构是什么,以及汽车油箱结构原理图解对应的知识点,希望对各位有所帮...
嵌入式 ADC使用手册完整版 ... 嵌入式 ADC使用手册完整版 (188977万字)💜&#...
重大消息战皇大厅开挂是真的吗... 您好:战皇大厅这款游戏可以开挂,确实是有挂的,需要了解加客服微信【8435338】很多玩家在这款游戏...
盘点十款牵手跑胡子为什么一直... 您好:牵手跑胡子这款游戏可以开挂,确实是有挂的,需要了解加客服微信【8435338】很多玩家在这款游...
senator香烟多少一盒(s... 今天给各位分享senator香烟多少一盒的知识,其中也会对sevebstars香烟进行解释,如果能碰...
终于懂了新荣耀斗牛真的有挂吗... 您好:新荣耀斗牛这款游戏可以开挂,确实是有挂的,需要了解加客服微信8435338】很多玩家在这款游戏...
盘点十款明星麻将到底有没有挂... 您好:明星麻将这款游戏可以开挂,确实是有挂的,需要了解加客服微信【5848499】很多玩家在这款游戏...
总结文章“新道游棋牌有透视挂吗... 您好:新道游棋牌这款游戏可以开挂,确实是有挂的,需要了解加客服微信【7682267】很多玩家在这款游...
终于懂了手机麻将到底有没有挂... 您好:手机麻将这款游戏可以开挂,确实是有挂的,需要了解加客服微信【8435338】很多玩家在这款游戏...