【问题标题】:Merge green screen image with a background image using Python使用 Python 将绿屏图像与背景图像合并
【发布时间】:2021-11-04 08:09:56
【问题描述】:

大家好,我一直在尝试使用 PIL 合并 2 张图像(一张是绿屏图像,照片中间有一个物体,第二张是森林)。两张图片的大小相同(800、600)我试图将它们合并成一个新图片,但在我看来这似乎是不可能的。

任何代码解决方案?

【问题讨论】:

    标签: python merge python-imaging-library


    【解决方案1】:

    我会将问题分解为多个部分:首先,您需要从第一张图片中移除绿屏背景。您可以使用PIL 执行此操作,方法是遍历视频的每一帧并移除绿色。网上有great guides可以这样做。

    接下来,您需要将第一张图片放在第二张图片之上。您可以使用paste() 方法来实现,在online guides 中也有详细说明。

    【讨论】:

      【解决方案2】:

      我也是 Python 新手,但我们的讲师提供了另一种方法来完成这项工作,即使用 if 条件和嵌套 for 循环

      *from PIL import Image
      penguin_image = Image.open ("penguin.jpg")
      beach_image = Image.open("beach.jpg")
      
      penguin_width, penguin_height = penguin_image.size
      
      #Accessing the pixels of the images
      penguin_pixel = penguin_image.load()
      beach_pixel = beach_image.load()
      
      #Image inspection
      (r0,g0,b0) = penguin_pixel[0,0]
      (r1,g1,b1) = penguin_pixel[1,0]
      (r2,g2,b2) = penguin_pixel[2,0]
      (r3,g3,b3) = penguin_pixel[3,0]
      (r4,g4,b4) = penguin_pixel[4,0]
      (r5,g5,b5) = penguin_pixel[5,0]
      (br,bg,bb) = beach_pixel[0,0]
      
      print(penguin_width, penguin_height)
      print(br, bg, bb)
      
      #Creation of new image to hold combined pixels
      combined_image = Image.new("RGB", penguin_image.size)
      combined_pixel = combined_image.load()
      
      #Checking for green
      for ver in range (0, penguin_height):
          for hor in range(0, penguin_width):
              (r,g,b) = penguin_pixel[hor, ver]
              (br,bg,bb) = beach_pixel[hor, ver]
              if (r < 100 and g == 207 and b < 100):
                  combined_pixel[hor,ver] = (br, bg, bb)
              else:
                  combined_pixel[hor,ver] = (r, g, b)*
      

      combined_image.show()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-04
        • 1970-01-01
        • 2012-04-14
        • 2022-07-21
        • 2016-12-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多