/ /画像の不透明度を変更してPythonの別の画像とマージする方法 - python、python-imaging-library

画像の不透明度を変更してPythonの別の画像とマージする方法 - python、python-imaging-library

私は2つの画像を一緒にする方法を見ていて、一番上のものを約50%透明にしました。

これまでのところ、私はこれを見つけることができました:

from PIL import Image

def merge():
background = Image.open("ib.jpg")
background = background .convert("L") #only foreground color matters
foreground = Image.open("if.jpg")

background.paste(foreground, (0, 0), foreground)
background.show()

しかし、空白の画像しか出力しません。

どちらも同じサイズです。

ib.jpg:

ib.jpg

if.jpg:

if.jpg

所望の出力:

ここに画像の説明を入力

これをRGBまたはRGBAファイルで行うためのヒントはありますか?私は両方の型(実際には、アルファ層を持つもの)を扱うべきです。

ありがとう、

回答:

回答№1は4

あなたは使用する必要があります blend からの関数 PIL.Image

from PIL import Image
bg = Image.open("1.jpg")
fg = Image.open("2.jpg")
# set alpha to .7
Image.blend(bg, fg, .7).save("out.png")

ここに画像の説明を入力