Files
AiDA_Python/app/service/design_batch/utils/transparent.py

27 lines
919 B
Python
Raw Normal View History

2024-12-11 11:06:20 +08:00
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