[英]Fill an image with color but keep the alpha (Color overlay in 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.