接上篇文章继续解绍arcade游戏编程的基本知识。如何通过程序合成所需的动画图片
游戏素材
如何通过程序合成所需的动画图片
from PIL import Image
img = Image.open(“images/tank.png”).convert(“RGBA”) #读取系统的内照片
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函数的参数为(需要修改的图片,粘贴的起始点的横坐标,粘贴的起始点的纵坐标)
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()
透明度不对
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)
原图读取时要有透明度层的数据
img = Image.open(“images/tank.png”).convert(“RGBA”) #读取系统的内照片
pp2 = img.crop(box)
#分离通道
r, g, b, a = pp2.split()
#粘贴要加mask
nimg.paste(pp2,(5,47,180,94),mask=a)
微调数据
# _*_ 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次。
可以关注我,点赞我、评论我、收藏我啦。