Files
AiDA_Python/app/service/design_fast/utils/transparent.py
2024-11-19 10:14:52 +08:00

27 lines
919 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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