Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python中的PIL将一个图像合成到另一个图像上?_Python_Image Processing_Python Imaging Library - Fatal编程技术网

如何使用Python中的PIL将一个图像合成到另一个图像上?

如何使用Python中的PIL将一个图像合成到另一个图像上?,python,image-processing,python-imaging-library,Python,Image Processing,Python Imaging Library,我需要采取一个图像,并把它放在一个新的,生成的白色背景,以便它被转换成一个可下载的桌面壁纸。因此,这个过程将是: 生成新的全白色图像,尺寸为1440x900 将现有图像放置在顶部,居中 另存为单个图像 在PIL中,我看到了ImageDraw对象,但没有任何迹象表明它可以将现有图像数据绘制到另一个图像上。任何人都可以推荐的建议或链接?Image.blend()?[] 或者,更好的是,Image.paste(),相同的链接。这可以通过图像实例的paste方法实现: from PIL import I

我需要采取一个图像,并把它放在一个新的,生成的白色背景,以便它被转换成一个可下载的桌面壁纸。因此,这个过程将是:

  • 生成新的全白色图像,尺寸为1440x900
  • 将现有图像放置在顶部,居中
  • 另存为单个图像
  • 在PIL中,我看到了
    ImageDraw
    对象,但没有任何迹象表明它可以将现有图像数据绘制到另一个图像上。任何人都可以推荐的建议或链接?

    Image.blend()
    ?[]


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

    这可以通过图像实例的
    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技巧可以在

    上学习,可能太晚了,但对于此类图像操作,我们确实在具有原始图像的模型中使用。

    基于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()
    

    记住使用枕头(,)而不是python图像,因为枕头与python 2.X和python 3.X配合使用。

    这是为了做类似的事情

    我首先在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)
    
    好的,那么这些是从哪里来的呢?
    (40, 1345, 551, 1625)
    im2.2尺寸
    (511, 280)
    因为我添加了40右和1345下(40134511280),所以我必须将它们添加到原始图像大小=(4013455511265)

    来展示你的新形象


    “通过在给定图像之间使用常量alpha进行插值来创建新图像。两个图像必须具有相同的大小和模式。”从文档中可以看出,它们的大小不能不同。我还注意到
    image.paste()
    ,这就是最终的解决方案。url过期您可能需要根据您的模块/系统/版本进行导入:from PIL import ImageThank@NunoAniceto,我已将其更改为
    from PIL import Images
    ,以使代码更完整。如果您使用的是Python 3.x,请检查以修复“offeset”元组整数错误。
    im1.show()