27 lines
919 B
Python
27 lines
919 B
Python
from PIL import Image
|
||
|
||
|
||
def sketch_to_transparent(image, mask, transparency):
|
||
# 打开原始图片
|
||
image = image.convert("RGBA")
|
||
# 打开mask图片,假设mask图片是灰度图,白色区域为要处理的区域,黑色区域为保留的区域
|
||
mask = Image.fromarray(mask)
|
||
|
||
# 根据透明度调整因子,将透明度转换为0-255之间的值
|
||
alpha_value = int((1 - transparency) * 255.0)
|
||
|
||
# 获取图片的像素数据
|
||
image_pixels = image.load()
|
||
mask_pixels = mask.load()
|
||
|
||
width, height = image.size
|
||
|
||
for y in range(height):
|
||
for x in range(width):
|
||
# 如果mask区域对应的像素为白色(值大于128,这里假设白色为要处理的区域,可根据实际情况调整)
|
||
if mask_pixels[x, y] > 128:
|
||
r, g, b, a = image_pixels[x, y]
|
||
image_pixels[x, y] = (r, g, b, alpha_value)
|
||
|
||
return image
|