import PIL.Image as Image
import os
IMAGES_PATH = 'C:' #數據集路徑
IMAGES_FORMAT = ['.jpg', '.JPG']
IMAGE_SIZE = 60 #單張圖像分辨率
IMAGE_ROW = 6 #行列數
IMAGE_COLUMN = 6
IMAGE_SAVE_PATH = 'new.jpg' #保存地址
image_names = [name for name in os.listdir(IMAGES_PATH) for item in IMAGES_FORMAT if
os.path.splitext(name)[1] == item]
def image_compose():
to_image = Image.new('RGB', (IMAGE_COLUMN * IMAGE_SIZE, IMAGE_ROW * IMAGE_SIZE))
for y in range(1, IMAGE_ROW + 1):
for x in range(1, IMAGE_COLUMN + 1):
from_image = Image.open(IMAGES_PATH + image_names[IMAGE_COLUMN * (y - 1) + x - 1]).resize(
(IMAGE_SIZE, IMAGE_SIZE),Image.ANTIALIAS)
to_image.paste(from_image, ((x - 1) * IMAGE_SIZE, (y - 1) * IMAGE_SIZE))
return to_image.save(IMAGE_SAVE_PATH)
image_compose()
結果:
(結果已經過隱私處理)