我只需要將線性透明漸變應用于影像的底部 5%。我有一些想法,首先我需要在 alpha 通道上創建蒙版。但我不確定如何才能做到只影響影像底部 5% 的部分。到目前為止,我嘗試了不同的方法,但沒有運氣。漸變應該從影像的 5% 高度開始,100% alpha 和 0% alpha 最后。此外,影像可以是具有透明背景的非矩形 PNG。我真的很感激任何幫助。謝謝你。

magick lena_circle.png \
-alpha set \
-set option:wd "%w" \
-set option:ht "%h" \
-set option:ht2 "%[fx:round(0.25*ht)]" \
-set option:ht3 "%[fx:ht-ht2]" \
\( -size "%[wd]x%[ht3]" xc:white \) \
\( -size "%[wd]x%[ht2]" gradient:white-black \) \
\( -clone 1,2 -append \) \
-delete 1,2 \
\( -clone 0 -alpha extract \) \
\( -clone 1,2 -compose multiply -composite \) \
-delete 1,2 \
-alpha off -compose copy_opacity -composite \
lena_circle_fade3.png
結果影像:

uj5u.com熱心網友回復:
這是在 Python/OpenCV/Numpy 中為不透明影像執行此操作的一種方法。
- Read the input
- Compute top and bottom heights for the alpha channel
- Create a constant white image for top
- Create a vertical gradient going from 255 to 0 for the bottom
- Stack the top and bottom parts
- Convert the image to 4 channels BGRA
- Replace the alpha in the BGRA image with the stacked alpha
- Save the result
輸入:

import cv2
import numpy as np
# read image
img = cv2.imread("lena.png")
ht, wd = img.shape[:2]
# compute 5% of ht and 95% of ht
# pct = 5
pct = 25 # temparily set pct to 25 percent for demonstration
ht2 = int(ht*pct/100)
ht3 = ht - ht2
# create opaque white image for top
top = np.full((ht3,wd), 255, dtype=np.uint8)
# create vertical gradient for bottom
btm = np.linspace(255, 0, ht2, endpoint=True, dtype=np.uint8)
btm = np.tile(btm, (wd,1))
btm = np.transpose(btm)
# stack top and bottom
alpha = np.vstack((top,btm))
# put alpha channel into image
result = img.copy()
result = cv2.cvtColor(result, cv2.COLOR_BGR2BGRA)
result[:,:,3] = alpha
# save result
cv2.imwrite('lena_fade.png', result)
# display results
# (note: display does not show transparency)
cv2.imshow('btm', btm)
cv2.imshow('alpha', alpha)
cv2.imshow('result', result)
cv2.waitKey(0)
結果:

添加
這是在 ImageMagick 7 中如何做到這一點。
magick lena.png \
-set option:wd "%w" \
-set option:ht "%h" \
-set option:ht2 "%[fx:round(25*ht/100)]" \
-set option:ht3 "%[fx:ht-ht2]" \
\( -size "%[wd]x%[ht3]" xc:white \) \
\( -size "%[wd]x%[ht2]" gradient:white-black \) \
\( -clone 1,2 -append \) \
-delete 1,2 \
-alpha off -compose copy_opacity -composite \
lena_fade2.png
結果影像:

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/361756.html
標籤:Python 图像处理 图像魔术师 蟒蛇成像库 简历2
上一篇:僅通過二進制大小檢測影像變化
下一篇:影像的矩陣變換
