Python用Image来合并图像,写了两个类似的函数,分别是纵向合并和横向合并。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from PIL import Image

def mergei(files, output_file):
tot = len(files)
img = Image.open(files[0])
w, h = img.size[0], img.size[1]
merge_img = Image.new('RGB', (w * tot, h), 0xffffff)
i = 0
for f in files:
print(f)
img = Image.open(f)
merge_img.paste(img, (i, 0))
i += w
merge_img.save(output_file)

def mergej(files, output_file):
tot = len(files)
img = Image.open(files[0])
w, h = img.size[0], img.size[1]
merge_img = Image.new('RGB', (w, h * tot), 0xffffff)
j = 0
for f in files:
print(f)
img = Image.open(f)
merge_img.paste(img, (0, j))
j += h
merge_img.save(output_file)

for j in range(48):
tot = 0
for i in range(0, 48, 8):
files = []
for ii in range(i, i + 8):
files.append('{}_{}.jpg'.format(ii, j))
mergei(files, 'z\{}_{}.jpg'.format(tot, j))
tot += 1

for i in range(0, 6):
tot = 0
for j in range(0, 48, 8):
files = []
for jj in range(j, j + 8):
files.append('z\{}_{}.jpg'.format(i, jj))
mergej(files, 'y\{}_{}.jpg'.format(i, tot))
tot += 1