【问题标题】:How do you composite an image onto another image with PIL in Python?如何在 Python 中使用 PIL 将图像合成到另一个图像上?
【发布时间】:2011-02-03 13:31:40
【问题描述】:

我需要拍摄一张图片并将其放置在新生成的白色背景上,以便将其转换为可下载的桌面壁纸。所以这个过程会:

  1. 生成新的全白图像,尺寸为 1440x900
  2. 将现有图片放在顶部,居中
  3. 另存为单张图片

在 PIL 中,我看到了 ImageDraw 对象,但没有任何迹象表明它可以将现有图像数据绘制到另一个图像上。任何人都可以推荐的建议或链接?

【问题讨论】:

    标签: python image-processing python-imaging-library


    【解决方案1】:

    Image.blend()? [link]

    或者,更好的是,Image.paste(),相同的链接。

    【讨论】:

    • “通过在给定图像之间插值创建一个新图像,使用恒定的 alpha。两个图像必须具有相同的大小和模式。”从文档来看,它们的大小似乎不能不同。
    • 我也注意到Image.paste(),这最终是解决方案。
    • 网址已过期
    【解决方案2】:

    这可以通过 Image 实例的 paste 方法来完成:

    from PIL import Image
    img = Image.open('/path/to/file', 'r')
    img_w, img_h = img.size
    background = Image.new('RGBA', (1440, 900), (255, 255, 255, 255))
    bg_w, bg_h = background.size
    offset = ((bg_w - img_w) // 2, (bg_h - img_h) // 2)
    background.paste(img, offset)
    background.save('out.png')
    

    这个和许多其他 PIL 技巧可以在Nadia Alramli's PIL Tutorial 获得

    【讨论】:

    • 您可能需要根据您的模块/系统/版本导入:from PIL import Image
    • 感谢@NunoAniceto,我已将其更改为from PIL import Image,以使代码更像compatible with Pillow
    • 如果您使用的是 Python 3.x,请检查 stackoverflow.com/a/17530159/7326714 以修复“offset”元组整数错误。
    【解决方案3】:

    也许为时已晚,但对于此类图像操作,我们确实在带有原始图像的模型中使用ImageSpecField

    【讨论】:

      【解决方案4】:

      基于 unutbus 的回答:

      #!/usr/bin/env python
      
      from PIL import Image
      import math
      
      
      def resize_canvas(old_image_path="314.jpg", new_image_path="save.jpg",
                        canvas_width=500, canvas_height=500):
          """
          Place one image on another image.
      
          Resize the canvas of old_image_path and store the new image in
          new_image_path. Center the image on the new canvas.
          """
          im = Image.open(old_image_path)
          old_width, old_height = im.size
      
          # Center the image
          x1 = int(math.floor((canvas_width - old_width) / 2))
          y1 = int(math.floor((canvas_height - old_height) / 2))
      
          mode = im.mode
          if len(mode) == 1:  # L, 1
              new_background = (255)
          if len(mode) == 3:  # RGB
              new_background = (255, 255, 255)
          if len(mode) == 4:  # RGBA, CMYK
              new_background = (255, 255, 255, 255)
      
          newImage = Image.new(mode, (canvas_width, canvas_height), new_background)
          newImage.paste(im, (x1, y1, x1 + old_width, y1 + old_height))
          newImage.save(new_image_path)
      
      resize_canvas()
      

      请记住使用 Pillow(DocumentationGitHubPyPI)而不是 python-imaging,因为 Pillow 适用于 Python 2.X 和 Python 3.X。

      【讨论】:

        【解决方案5】:

        这是为了做类似的事情

        我首先在 Photoshop 中生成“白色背景”并将其导出为 PNG 文件。这就是我得到 im1 的地方(图 1)。然后使用粘贴功能,因为它更容易。

        from PIL import Image
        
        im1 = Image.open('image/path/1.png')
        im2 = Image.open('image/path/2.png')
        area = (40, 1345, 551, 1625)  
        im1.paste(im2, area)
                           l>(511+40) l>(280+1345)
                 |    l> From 0 (move, 1345px down) 
                  -> From 0 (top left, move 40 pixels right)
        

        Okay so where did these #'s come from? (40, 1345, 551, 1625) im2.size (511, 280) Because I added 40 right and 1345 down (40, 1345, 511, 280) I must add them to the original image size which = (40, 1345, 551, 1625)

        im1.show() 
        

        展示你的新形象

        【讨论】:

          猜你喜欢
          • 2014-08-24
          • 1970-01-01
          • 2013-11-02
          • 1970-01-01
          • 2015-08-10
          • 1970-01-01
          • 2023-03-17
          相关资源
          最近更新 更多