弯曲图像的算法

时间:2014-12-22 12:55:43

标签: python algorithm image-processing 3d python-imaging-library

这是我试图实现的结果:

enter image description here

我的算法目前是:

  1. 此画布照片在设计的位置是透明的。
  2. 我调整了设计的大小,使其比帆布照片的大小略大一些
  3. 我将画布照片粘贴到设计
  4. 它工作正常,但我需要图像在角落弯曲。有没有办法实现这个目标?

    这是我使用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")
    

0 个答案:

没有答案