这是我试图实现的结果:
我的算法目前是:
它工作正常,但我需要图像在角落弯曲。有没有办法实现这个目标?
这是我使用PIL在python中编写的当前算法代码:
def merge_case_image(case_path, design_path, save_path):
case = Image.open(case_path) # cas template
design = Image.open(design_path) # design
design.thumbnail(case.size, Image.ANTIALIAS) # resize design
design_w, design_h = design.size
bg_white = Image.new('RGBA', case.size, (255, 255, 255, 255)) # white background
bg_w, bg_h = bg_white.size
offset = ((bg_w - design_w) / 2, (bg_h - design_h) / 2)
if design.mode == "RGBA":
bg_white.paste(design, offset, design)
elif design.mode == "RGB":
resizeImageMask = Image.new("L", design.size)
resizedImagePixels = design.load()
width, height = design.size
for x in range(width):
for y in range(height):
cpixel = resizedImagePixels[x, y]
if ((cpixel[0] == 255) and (cpixel[1] == 255) and (cpixel[2] == 255)):
resizeImageMask.putpixel((x, y), 0)
else:
resizeImageMask.putpixel((x, y), 255)
bg_white.paste(design, offset, resizeImageMask)
bg_white.paste(case, (0, 0, bg_w, bg_h), case)
bg_white.save(save_path, "PNG")