简体   繁体   English

如何使粘贴在另一个图像顶部的图像背景在枕头中透明?

[英]How do I make the background of an image, that is pasted on top of another image, transparent in pillow?

I'm making a chess bot which will run in a chat program reacting and moving pieces according to commands from users.我正在制作一个国际象棋机器人,它将在聊天程序中运行,根据用户的命令做出反应和移动棋子。 As such, I am trying to make it paste png images of chess pieces on top of a png background image of a chessboard by using the python package pillow.因此,我试图通过使用 python package 枕头将棋子的 png 图像粘贴到棋盘的 png 背景图像上。

In any other software, the chess pieces have transparent backgrounds and whatever is behind them show through but when I paste it on top of the chessboard using pillow the chess-piece gets a grey background which does not match the tile on the chessboard.在任何其他软件中,棋子都有透明的背景,它们后面的任何东西都会显示出来,但是当我使用枕头将它粘贴到棋盘顶部时,棋子的背景是灰色的,与棋盘上的瓷砖不匹配。

Is there a way in which I can paste the chess-piece image on top of my chessboard background without the added grey background colour of the chess-piece?有没有一种方法可以将棋子图像粘贴到我的棋盘背景上而不添加棋子的灰色背景颜色? Or is there an alternative way of doing this in python which will give the desired result?或者在 python 中是否有另一种方法可以提供所需的结果?

I'm not done with the bot and the game logic but as an example, I have included some code illustrating how I've pasted one image on top of the other one below:我还没有完成机器人和游戏逻辑,但作为示例,我包含了一些代码,说明了我如何将一张图像粘贴到下面的另一张图像之上:

from PIL import Image

board = Image.open('assets/chessboard.png')
w_pawn = Image.open('assets/w_pawn_png_shadow_128px.png')

a_eight = (102, 30, 210, 158)  # Left, top, right and bottom coordinates of where the piece is pasted onto the board

battle_board = board.copy()
battle_board.paste(w_pawn, a_eight)
battle_board.show()

Just to quickly show what the output looks like I've included a screenshot of what a pawn looks like when it's pasted onto the board:只是为了快速显示 output 的外观,我附上了一张将棋子粘贴到板上时的屏幕截图:

A white pawn in A8 with faulty background colour A8 中的白棋,背景颜色错误

在此处输入图像描述

棋盘

白典当

OK, here's how to do it using the PIL.Image.alpha_composite() PIL Image class static function that I recommended in the comments.好的,这是使用我在评论中推荐的PIL.Image.alpha_composite() PIL Image class static function 的方法。 It performs an image-processing operation known as alpha compositing .它执行称为alpha compositing的图像处理操作。

Update: I discovered there's also an Image.alpha_composite() method that does a similar operation, but does it "in-place" — which potentially could provide better performance (although the current documentation says that is currently not the case).更新:我发现还有一个Image.alpha_composite()方法可以执行类似的操作,但它是“就地”执行的——这可能会提供更好的性能(尽管当前文档说目前并非如此)。 Because that might change in the future, I've modified the code shown to make use of it to take advantage of possible future changes.因为将来可能会发生变化,所以我修改了显示的代码以利用它来利用未来可能发生的变化。

Since there's no alpha_composite_paste() function, an image the same size as the background chessboard image must first be created with a fully transparent background.由于没有alpha_composite_paste() function,因此必须首先使用完全透明的背景创建与背景棋盘图像相同大小的图像。 The chess-piece is then pasted onto it in the desired location.然后将棋子粘贴到所需位置。

Once all that's done, you now have two identically sized images — as required by alpha_composite() — the compositing is then applied…the final step of the process.完成所有这些后,您现在就有了两个相同大小的图像——根据alpha_composite()的要求——然后应用合成……过程的最后一步。

from PIL import Image

# Two RGBA images.
board = Image.open('chessboard.png')
w_pawn = Image.open('w_pawn_png_shadow_128px.png')

a_eight = (102, 30, 210, 158)  # Left, top, right and bottom coordinates of where
                               # the piece is pasted onto the board

battle_board = board.copy()

# First make a fully transparent image the same size as the chessboard image.
tmp_img = Image.new('RGBA', battle_board.size, color=(0,0,0,0))
tmp_img.paste(w_pawn, box=a_eight)  # Paste chess-piece on it at desired posn.
battle_board.alpha_composite(tmp_img)  # Composite tmp_img onto board img "in-place".
battle_board.show()  # TaDa!

Here's the result:结果如下:

显示透明度的结果图像被保留

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM