将多个图像水平地与 Python 组合在一起

我试图在 Python 中横向组合一些 JPEG 图像。

问题

我有3个图像-每个是148 x 95-见附件。我只是复制了3个相同的图片-这就是为什么他们是相同的。

enter image description hereenter image description hereenter image description here

我的尝试

我尝试使用以下代码横向加入他们:

import sys
from PIL import Image


list_im = ['Test1.jpg','Test2.jpg','Test3.jpg']


# creates a new empty image, RGB mode, and size 444 by 95
new_im = Image.new('RGB', (444,95))


for elem in list_im:
for i in xrange(0,444,95):
im=Image.open(elem)
new_im.paste(im, (i,0))
new_im.save('test.jpg')

但是,这会产生附加在 test.jpg上的输出。

enter image description here

提问

有没有一种方法可以水平连接这些图像,使 test.jpg 中的子图像不会显示额外的部分图像?

其他资料

我正在寻找一种方法,以水平连接 n 图像。我希望通常使用这个代码,所以我希望:

  • 如果可能的话,不要硬编码图像尺寸
  • 在一行中指定尺寸,以便于更改
260234 次浏览

你可以这样做:

import sys
from PIL import Image


images = [Image.open(x) for x in ['Test1.jpg', 'Test2.jpg', 'Test3.jpg']]
widths, heights = zip(*(i.size for i in images))


total_width = sum(widths)
max_height = max(heights)


new_im = Image.new('RGB', (total_width, max_height))


x_offset = 0
for im in images:
new_im.paste(im, (x_offset,0))
x_offset += im.size[0]


new_im.save('test.jpg')

Test1.jpg

Test1.jpg

Test2.jpg

Test2.jpg

Test3.jpg

Test3.jpg

test.jpg

enter image description here


嵌套的 for i in xrange(0,444,95):是粘贴每个图像5次,交错95像素分开。每个外部循环迭代粘贴前面的。

for elem in list_im:
for i in xrange(0,444,95):
im=Image.open(elem)
new_im.paste(im, (i,0))
new_im.save('new_' + elem + '.jpg')

enter image description here enter image description here enter image description here

编辑: DTing 的答案更适用于您的问题,因为它使用 PIL,但如果您想知道如何在 numpy 中进行操作,我将保留这个选项。

这里有一个 numpy/matplotlib 解决方案,可以用于任意大小/形状的 N 个图像(只有彩色图像)。

import numpy as np
import matplotlib.pyplot as plt


def concat_images(imga, imgb):
"""
Combines two color image ndarrays side-by-side.
"""
ha,wa = imga.shape[:2]
hb,wb = imgb.shape[:2]
max_height = np.max([ha, hb])
total_width = wa+wb
new_img = np.zeros(shape=(max_height, total_width, 3))
new_img[:ha,:wa]=imga
new_img[:hb,wa:wa+wb]=imgb
return new_img


def concat_n_images(image_path_list):
"""
Combines N color images from a list of image paths.
"""
output = None
for i, img_path in enumerate(image_path_list):
img = plt.imread(img_path)[:,:,:3]
if i==0:
output = img
else:
output = concat_images(output, img)
return output

下面是示例使用:

>>> images = ["ronda.jpeg", "rhod.jpeg", "ronda.jpeg", "rhod.jpeg"]
>>> output = concat_n_images(images)
>>> import matplotlib.pyplot as plt
>>> plt.imshow(output)
>>> plt.show()

enter image description here

我会试试这个:

import numpy as np
import PIL
from PIL import Image


list_im = ['Test1.jpg', 'Test2.jpg', 'Test3.jpg']
imgs    = [ Image.open(i) for i in list_im ]
# pick the image which is the smallest, and resize the others to match it (can be arbitrary image shape here)
min_shape = sorted( [(np.sum(i.size), i.size ) for i in imgs])[0][1]
imgs_comb = np.hstack([i.resize(min_shape) for i in imgs])


# save that beautiful picture
imgs_comb = Image.fromarray( imgs_comb)
imgs_comb.save( 'Trifecta.jpg' )


# for a vertical stacking it is simple: use vstack
imgs_comb = np.vstack([i.resize(min_shape) for i in imgs])
imgs_comb = Image.fromarray( imgs_comb)
imgs_comb.save( 'Trifecta_vertical.jpg' )

它应该只要所有图像是相同的品种(所有 RGB,所有 RGBA,或所有灰度)。对于多几行代码来说,确保这一点并不困难。下面是我的示例图片,结果是:

Test1.jpg

Test1.jpg

Test2.jpg

Test2.jpg

Test3.jpg

Test3.jpg

返回文章页面

combined images

三角形 _ 垂直. jpg

enter image description here

根据 DTing 的回答,我创建了一个更容易使用的函数:

from PIL import Image




def append_images(images, direction='horizontal',
bg_color=(255,255,255), aligment='center'):
"""
Appends images in horizontal/vertical direction.


Args:
images: List of PIL images
direction: direction of concatenation, 'horizontal' or 'vertical'
bg_color: Background color (default: white)
aligment: alignment mode if images need padding;
'left', 'right', 'top', 'bottom', or 'center'


Returns:
Concatenated image as a new PIL image object.
"""
widths, heights = zip(*(i.size for i in images))


if direction=='horizontal':
new_width = sum(widths)
new_height = max(heights)
else:
new_width = max(widths)
new_height = sum(heights)


new_im = Image.new('RGB', (new_width, new_height), color=bg_color)




offset = 0
for im in images:
if direction=='horizontal':
y = 0
if aligment == 'center':
y = int((new_height - im.size[1])/2)
elif aligment == 'bottom':
y = new_height - im.size[1]
new_im.paste(im, (offset, y))
offset += im.size[0]
else:
x = 0
if aligment == 'center':
x = int((new_width - im.size[0])/2)
elif aligment == 'right':
x = new_width - im.size[0]
new_im.paste(im, (x, offset))
offset += im.size[1]


return new_im

它允许选择背景颜色和图像对齐。递归也很简单:

images = map(Image.open, ['hummingbird.jpg', 'tiger.jpg', 'monarch.png'])


combo_1 = append_images(images, direction='horizontal')
combo_2 = append_images(images, direction='horizontal', aligment='top',
bg_color=(220, 140, 60))
combo_3 = append_images([combo_1, combo_2], direction='vertical')
combo_3.save('combo_3.png')

Example concatenated image

下面是一个泛化以前方法的函数,在 PIL 中创建一个图像网格:

from PIL import Image
import numpy as np


def pil_grid(images, max_horiz=np.iinfo(int).max):
n_images = len(images)
n_horiz = min(n_images, max_horiz)
h_sizes, v_sizes = [0] * n_horiz, [0] * (n_images // n_horiz)
for i, im in enumerate(images):
h, v = i % n_horiz, i // n_horiz
h_sizes[h] = max(h_sizes[h], im.size[0])
v_sizes[v] = max(v_sizes[v], im.size[1])
h_sizes, v_sizes = np.cumsum([0] + h_sizes), np.cumsum([0] + v_sizes)
im_grid = Image.new('RGB', (h_sizes[-1], v_sizes[-1]), color='white')
for i, im in enumerate(images):
im_grid.paste(im, (h_sizes[i % n_horiz], v_sizes[i // n_horiz]))
return im_grid

它将把网格的每一行和每一列缩小到最小。通过使用 pil _ grid (images)只能有一行,或者使用 pil _ grid (images,1)只能有一列。

与基于数字阵列的解决方案相比,使用 PIL 的一个好处是,您可以处理结构不同的图像(如灰度或基于调色板的图像)。

输出示例

def dummy(w, h):
"Produces a dummy PIL image of given dimensions"
from PIL import ImageDraw
im = Image.new('RGB', (w, h), color=tuple((np.random.rand(3) * 255).astype(np.uint8)))
draw = ImageDraw.Draw(im)
points = [(i, j) for i in (0, im.size[0]) for j in (0, im.size[1])]
for i in range(len(points) - 1):
for j in range(i+1, len(points)):
draw.line(points[i] + points[j], fill='black', width=2)
return im


dummy_images = [dummy(20 + np.random.randint(30), 20 + np.random.randint(30)) for _ in range(10)]

返回文章页面

line.png

返回文章页面

enter image description here

返回文章页面

enter image description here

"""
merge_image takes three parameters first two parameters specify
the two images to be merged and third parameter i.e. vertically
is a boolean type which if True merges images vertically
and finally saves and returns the file_name
"""
def merge_image(img1, img2, vertically):
images = list(map(Image.open, [img1, img2]))
widths, heights = zip(*(i.size for i in images))
if vertically:
max_width = max(widths)
total_height = sum(heights)
new_im = Image.new('RGB', (max_width, total_height))


y_offset = 0
for im in images:
new_im.paste(im, (0, y_offset))
y_offset += im.size[1]
else:
total_width = sum(widths)
max_height = max(heights)
new_im = Image.new('RGB', (total_width, max_height))


x_offset = 0
for im in images:
new_im.paste(im, (x_offset, 0))
x_offset += im.size[0]


new_im.save('test.jpg')
return 'test.jpg'

只是添加到已经建议的解决方案。假设相同的高度,没有调整大小。

import sys
import glob
from PIL import Image
Image.MAX_IMAGE_PIXELS = 100000000  # For PIL Image error when handling very large images


imgs    = [ Image.open(i) for i in list_im ]


widths, heights = zip(*(i.size for i in imgs))
total_width = sum(widths)
max_height = max(heights)


new_im = Image.new('RGB', (total_width, max_height))


# Place first image
new_im.paste(imgs[0],(0,0))


# Iteratively append images in list horizontally
hoffset=0
for i in range(1,len(imgs),1):
**hoffset=imgs[i-1].size[0]+hoffset  # update offset**
new_im.paste(imgs[i],**(hoffset,0)**)


new_im.save('output_horizontal_montage.jpg')
from __future__ import print_function
import os
from pil import Image


files = [
'1.png',
'2.png',
'3.png',
'4.png']


result = Image.new("RGB", (800, 800))


for index, file in enumerate(files):
path = os.path.expanduser(file)
img = Image.open(path)
img.thumbnail((400, 400), Image.ANTIALIAS)
x = index // 2 * 400
y = index % 2 * 400
w, h = img.size
result.paste(img, (x, y, x + w, y + h))


result.save(os.path.expanduser('output.jpg'))

输出

enter image description here

如果所有图像的高度相同,

import numpy as np


imgs = ['a.jpg', 'b.jp', 'c.jpg']
concatenated = Image.fromarray(
np.concatenate(
[np.array(Image.open(x)) for x in imgs],
axis=1
)
)

也许你可以像这样在连接之前调整图像的大小,

import numpy as np


imgs = ['a.jpg', 'b.jpg', 'c.jpg']
concatenated = Image.fromarray(
np.concatenate(
[np.array(Image.open(x).resize((640,480)) for x in imgs],
axis=1
)
)

我的解决办法是:

from PIL import Image




def join_images(*rows, bg_color=(0, 0, 0, 0), alignment=(0.5, 0.5)):
rows = [
[image.convert('RGBA') for image in row]
for row
in rows
]


heights = [
max(image.height for image in row)
for row
in rows
]


widths = [
max(image.width for image in column)
for column
in zip(*rows)
]


tmp = Image.new(
'RGBA',
size=(sum(widths), sum(heights)),
color=bg_color
)


for i, row in enumerate(rows):
for j, image in enumerate(row):
y = sum(heights[:i]) + int((heights[i] - image.height) * alignment[1])
x = sum(widths[:j]) + int((widths[j] - image.width) * alignment[0])
tmp.paste(image, (x, y))


return tmp




def join_images_horizontally(*row, bg_color=(0, 0, 0), alignment=(0.5, 0.5)):
return join_images(
row,
bg_color=bg_color,
alignment=alignment
)




def join_images_vertically(*column, bg_color=(0, 0, 0), alignment=(0.5, 0.5)):
return join_images(
*[[image] for image in column],
bg_color=bg_color,
alignment=alignment
)

以下图片:

images = [
[Image.open('banana.png'), Image.open('apple.png')],
[Image.open('lime.png'), Image.open('lemon.png')],
]

结果会是这样的:


join_images(
*images,
bg_color='green',
alignment=(0.5, 0.5)
).show()

enter image description here


join_images(
*images,
bg_color='green',
alignment=(0, 0)


).show()

enter image description here


join_images(
*images,
bg_color='green',
alignment=(1, 1)
).show()

enter image description here

我的解决办法是:

import sys
import os
from PIL import Image, ImageFilter
from PIL import ImageFont
from PIL import ImageDraw


os.chdir('C:/Users/Sidik/Desktop/setup')
print(os.getcwd())


image_list= ['IMG_7292.jpg','IMG_7293.jpg','IMG_7294.jpg', 'IMG_7295.jpg' ]


image = [Image.open(x) for x in image_list]  # list
im_1 = image[0].rotate(270)
im_2 = image[1].rotate(270)
im_3 = image[2].rotate(270)
#im_4 = image[3].rotate(270)


height = image[0].size[0]
width = image[0].size[1]
# Create an empty white image frame
new_im = Image.new('RGB',(height*2,width*2),(255,255,255))


new_im.paste(im_1,(0,0))
new_im.paste(im_2,(height,0))
new_im.paste(im_3,(0,width))
new_im.paste(im_4,(height,width))




draw = ImageDraw.Draw(new_im)
font = ImageFont.truetype('arial',200)


draw.text((0, 0), '(a)', fill='white', font=font)
draw.text((height, 0), '(b)', fill='white', font=font)
draw.text((0, width), '(c)', fill='white', font=font)
#draw.text((height, width), '(d)', fill='white', font=font)


new_im.show()
new_im.save('BS1319.pdf')
[![Laser spots on the edge][1]][1]

此外,还有 Skimage util 蒙太奇,用同样形状的图像制作蒙太奇效果:

import numpy as np
import PIL
from PIL import Image
from skimage.util import montage


list_im = ['Test1.jpg', 'Test2.jpg', 'Test3.jpg']
imgs    = [ np.array(Image.open(i)) for i in list_im ]


montage(imgs)
    #**How to merge cropped images back to original image**
images = [Image.open(x) for x in images_list]
print("Length:: ", len(images))
widths, heights = zip(*(i.size for i in images))
print(widths, heights)
total_width = sum(widths)
max_height = sum(heights)
print(total_width,max_height)
    

new_im = Image.new('RGB', (5*384, 5*216))
    

x_offset = 0
y_offset = 0
    

img_size = [384,216]
def grouped(iterable, n):
return zip(*[iter(iterable)]*n)
    

for x,y,a,b,c in grouped(images, 5):
temp = []
temp.append([x,y,a,b,c])
print(temp[0])
print(len(temp[0]))
for lsingle_img in temp[0]:
# print(lsingle_img)
print("x_y_offset: ", (x_offset, y_offset))
new_im.paste(lsingle_img, (x_offset, y_offset))
x_offset += img_size[0]
temp = []
x_offset = 0
y_offset += img_size[1]
new_im.save('test.jpg')