簡體   English   中英

用顏色填充圖像,但保留Alpha(PIL中的顏色疊加)

[英]Fill an image with color but keep the alpha (Color overlay in PIL)

基本上,我正在嘗試制作一個將采用給定圖像和顏色的函數。 對於圖像中的每個像素,它將保留原始的Alpha值,但會將顏色更改為給定的顏色。

例如,如果函數獲取下面的箭頭圖像和紅色,

原始圖像-所有顏色

它將輸出以下圖像:

結果圖像-紅色

在Photoshop和其他圖像編輯器中,此效果稱為“顏色疊加”。 在PIL中是否有任何快速簡便的方法來達到相同的結果? 提前致謝! (;

一種方法是創建與原始尺寸相同的純紅色圖像,然后將原始圖像的alpha通道復制到該圖像上:

from PIL import Image

# Open original image and extract the alpha channel
im = Image.open('arrow.png')
alpha = im.getchannel('A')

# Create red image the same size and copy alpha channel across
red = Image.new('RGBA', im.size, color='red')
red.putalpha(alpha) 

在此處輸入圖片說明


這是使用Numpy的第二種方法:

from PIL import Image
import numpy as np

# Open image
im = Image.open('arrow.png')

# Make into Numpy array
n = np.array(im) 

# Set first three channels to red
n[...,0:3]=[255,0,0] 

# Convert back to PIL Image and save
Image.fromarray(n).save('result.png')

第三種方法是使用相似大小的紅色副本進行合成,並使用原始的Alpha蒙版:

from PIL import Image

# Open image
im = Image.open('arrow.png')                                                                                                       

# Make solid red image same size
red = Image.new('RGBA', im.size, color='red')                                                                                      

# Composite the two together, honouring the original mask
im = Image.composite(red,im,im)  

關鍵字 :圖像,圖像處理,Python,Pillow,PIL,Numpy,提取Alpha,Alpha通道,透明度,替換透明度,復制透明度,復制Alpha,移植Alpha,移植透明度。

讓我們考慮以下圖像-http: //www.libpng.org/pub/png/img_png/globe-scene-fish-bowl-pngcrush.png

image = cv2.imread("/home/thalish/bleed_test/globe-scene-fish-bowl-pngcrush.png",cv2.IMREAD_UNCHANGED)

image[:,:,0],image[:,:,1],image[:,:,2] = (255,0,0) #to replace all pixels with Red but keep alpha channel unchanged

最終影像

嘗試:

from PIL import Image

# Takes the input image
img = Image.open(r"Arrow.png")

# Getting the dimensions of the image
x, y = img.size

# Obtaining values of Red, Green, Blue for each opaque pixel
red = int(input("Enter the red value you want in each pixel = "))
green = int(input("Enter the green value you want in each pixel = "))
blue = int(input("Enter the blue value you want in each pixel = "))

i = j = 0

# This loop makes sure that alpha only has two discrete values (0 , 255)
# This is to ensure that constant color is obtained, at pixels where transparence may not be 255
# (prevents color escapes at pixels where total transparency is not achieved)
while i < x:
    while j < y:
        r, g, b, a = img.getpixel((i,j))
        if a > 200 and a < 256:
            a = 255
        else:
            a = 0
        img.putpixel((i,j),(r,g,b,a))
        j += 1
    j = 0
    i += 1

i = j = 0

# Two nested loops
# Outer one goes through rows of image
# Inner one (nested one) goes through columns of image
while i < x:
    while j < y:

        # This condition checks, if the value of alpha for that individual pixel is 255 (~opaque), 
        # if true then change its RGB values to user defined values
        if img.getpixel((i,j))[-1] == 255:
            img.putpixel((i,j), (red, green, blue, 255))
        j += 1
    j = 0
    i += 1

img.save("Arrow.png")

輸入: -

Enter the red value you want in each pixel = 0
Enter the green value you want in each pixel = 0
Enter the blue value you want in each pixel = 255

輸出: -

解釋第一圈:-

如果第一個循環的alpha值沒有達到閾值,那么在輸出圖像中會產生很多錯誤。 即,靠近對象邊緣的像素值傾向於具有比255 (總不透明度)小一點的alpha像素值,以實現平滑的抗鋸齒效果 如果這些像素被丟棄,則輸出圖像可能看起來像這樣:

注意箭頭邊緣的不需要的顏色

PS:-盡管OpenCV是大多數圖像分析師/專家的首選,但我絕對會建議您一開始就堅持使用PIL / Pillow,因為它可以使您以一種非常友好的方式掌握成像的基礎知識。 不可否認,OpenCV在幾乎所有方面都遠遠超過了PIL,但是,即使您一開始就學習PIL,從PIL過渡到OpenCV也會很容易在以后進行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM