import os
from PIL import Image
UNIT_SIZE = 220 # the size of image
# train
train_save_path = '/root/……cyclegan_8.0/web/train_result/'
train_path = "/root/……_cyclegan_8.0/web/images"
# test
test_save_path = '/root/……_cyclegan_8.0/web/test_result/'
test_path = "/root/……_cyclegan_8.0/test_latest/images"
train = True
def pinjie():
if train:
images = []
for img in os.listdir(train_path):
images.append(Image.open(os.path.join(train_path, img)))
if not os.path.exists(train_save_path):
os.makedirs(train_save_path)
for i in range(len(images) / 6): # every 6 a group
target = Image.new('RGB', (UNIT_SIZE * 3, UNIT_SIZE * 2)) # result is 2*3
leftone = 0
lefttwo = 0
rightone = UNIT_SIZE
righttwo = UNIT_SIZE
for j in range(6):
if (j <= 2):
target.paste(images[j + i * 6], (leftone, 0, rightone, UNIT_SIZE))
leftone += UNIT_SIZE
rightone += UNIT_SIZE
else:
target.paste(images[j + i * 6], (lefttwo, UNIT_SIZE, righttwo, UNIT_SIZE * 2))
lefttwo += UNIT_SIZE
righttwo += UNIT_SIZE
quality_value = 500
target.save(train_save_path + 'epoch_{}.png'.format(i + 1), quality=quality_value)
else:
images = []
for img in os.listdir(test_path):
images.append(Image.open(os.path.join(test_path, img)))
if not os.path.exists(test_save_path):
os.makedirs(test_save_path)
for i in range(len(images) / 6): # every 6 a group
target = Image.new('RGB', (UNIT_SIZE * 3, UNIT_SIZE * 2)) # result is 2*3
leftone = 0
lefttwo = 0
rightone = UNIT_SIZE
righttwo = UNIT_SIZE
for j in range(6):
if (j <= 2):
target.paste(images[j + i * 6], (leftone, 0, rightone, UNIT_SIZE))
leftone += UNIT_SIZE
rightone += UNIT_SIZE
else:
target.paste(images[j + i * 6], (lefttwo, UNIT_SIZE, righttwo, UNIT_SIZE * 2))
lefttwo += UNIT_SIZE
righttwo += UNIT_SIZE
quality_value = 500
target.save(test_save_path + 'epoch_{}.png'.format(i + 1), quality=quality_value)
if __name__ == '__main__':
pinjie()
同时横向纵向拼接
原图:
实际读图片的顺序是: realA-> fakeB-> recA. realB-> fakeA-> recB
拼接后: