概要
今までに集めてきた画像を使って私用のPC壁紙を作りたいと思い、PythonとOpenCVで試しに作りました。
使用した実行環境
- Windows10
- Python 3.7.4
- OpenCV 4.1.1.26
ソースコード
とりあえず画像を並べてみるプログラム
以下のような感じになりました。読み込んだ画像群に対して指定した枠に収まる数の画像をランダムに抽出して、抽出した画像をランダムに配置するようにしました。
import cv2
import sys
import os
import glob
import numpy as np
from numpy.random import *
def make_wallpaper(size_height, size_width, rh, rw, filename, inp_path, out_path):
if not os.path.isdir(out_path): os.makedirs(out_path)
inp_dir = glob.glob(inp_path)
out_dir = out_path
images = [cv2.imread(inp, 1) for inp in inp_dir]
img_background = np.zeros((size_hight*rh,size_width*rw,3), dtype=np.uint8)
all_img_nums = [i for i in range(0, len(images))]
all_img_nums.sort()
pairs = [[i, j] for i in range(0, size_hight*rh, size_hight) for j in range(0, size_width*rw, size_width)]
select_img_point_nums = [i for i in range(len(pairs))]
for count in range(1, 100):
select_img_nums = choice(all_img_nums, len(pairs), replace=False)
select_img_nums.sort()
randam_select_img_point_nums = choice(select_img_point_nums, len(select_img_point_nums), replace=False)
for randam_select_img_point_num, select_img_num in zip(randam_select_img_point_nums, select_img_nums):
img_background[pairs[randam_select_img_point_num][0]:pairs[randam_select_img_point_num][0]+size_hight, pairs[randam_select_img_point_num][1]:pairs[randam_select_img_point_num][1]+size_width] = images[select_img_num].copy()
name = out_dir + filename + '_wallpaper_' + str(count) + '.jpg'
cv2.imwrite(name, img_background)
def main():
make_wallpaper(size_height=1002, size_width=1334, rh=3, rw=3, filename = 'rinko_shirogane_3x3',
inp_path = '../../../100.data/0.input/bang-dream_gbp/rinko_shirogane/card/*.png', out_path = '../../../100.data/1.output/python/bang-dream_gbp/middle/rinko_shirogane/wallpaper/')
if __name__ == '__main__':
main()
実行結果
とりあえず画像を並べてみるプログラム(プチ応用)
ただ並べるだけだとあれなので比率を変えて配置してみました。
import cv2
import sys
import os
import glob
import numpy as np
from numpy.random import *
def make_wallpaper_roselia(size_height, size_width, rh, rw, filename, inp_path, out_path):
if not os.path.isdir(out_path): os.makedirs(out_path)
inp_dir = glob.glob(inp_path)
out_dir = out_path
base_img = cv2.imread('../../../100.data/0.input/bang-dream_gbp/1.Roselia/IMG_20190804_211827_waifu2x_art_noise1_scale_tta_1.png', 1)
base_img = cv2.resize(base_img, (size_width*4, size_height*3), interpolation=cv2.INTER_LANCZOS4)
images = [cv2.imread(inp, 1) for inp in inp_dir]
background_height = size_height*rh + base_img.shape[0]
background_width = size_width*rw
img_background = np.zeros((background_height, background_width, 3), dtype=np.uint8)
all_img_nums = [i for i in range(0, len(images))]
all_img_nums.sort()
pairs = [[i, j] for i in range(0, background_height, size_height) for j in range(0, background_width, size_width)]
select_img_point_nums = [i for i in range(len(pairs))]
print(pairs)
for count in range(1, 100):
select_img_nums = choice(all_img_nums, len(pairs), replace=False)
select_img_nums.sort()
randam_select_img_point_nums = choice(select_img_point_nums, len(select_img_point_nums), replace=False)
for randam_select_img_point_num, select_img_num in zip(randam_select_img_point_nums, select_img_nums):
img_background[pairs[randam_select_img_point_num][0]:pairs[randam_select_img_point_num][0]+size_height, pairs[randam_select_img_point_num][1]:pairs[randam_select_img_point_num][1]+size_width] = images[select_img_num].copy()
img_background[round(background_height/2 - base_img.shape[0]/2):round(background_height/2 - base_img.shape[0]/2) + base_img.shape[0], round(background_width/2 - base_img.shape[1]/2):round(background_width/2 - base_img.shape[1]/2) + base_img.shape[1]] = base_img.copy()
name = out_dir + filename + '_wallpaper_' + str(count) + '.jpg'
cv2.imwrite(name, img_background)
def main():
make_wallpaper_roselia(size_height=1002, size_width=1334, rh=4, rw=6, filename = 'roselia_3x3',
inp_path = '../../../100.data/0.input/bang-dream_gbp/1.Roselia/card/*.png', out_path = '../../../100.data/1.output/python/bang-dream_gbp/middle/1.roselia/wallpaper/')
if __name__ == '__main__':
main()
実行結果
感想
やりたかったことは達成できたのでよかったです。時間があるときにさらに応用していきたいですね。