集めた画像からPythonとOpenCVで壁紙を作る

## 概要
今までに集めてきた画像を使って私用のPC壁紙を作りたいと思い、PythonとOpenCVで試しに作りました。

## 使用した実行環境
- Windows10
- Python 3.7.4
- OpenCV 4.1.1.26

## ソースコード
### とりあえず画像を並べてみるプログラム
以下のような感じになりました。読み込んだ画像群に対して指定した枠に収まる数の画像をランダムに抽出して、抽出した画像をランダムに配置するようにしました。

```python
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()

```

#### 実行結果
たくさんできました。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/331462/0d82fdc3-f9c4-efde-54ea-29c704879431.png)
いいかんじですね。
![rinko_shirogane_3x3_wallpaper_22.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/331462/98ffaabf-fd40-5714-ef5d-7a620a7ae3a8.jpeg)

### とりあえず画像を並べてみるプログラム（プチ応用）
ただ並べるだけだとあれなので比率を変えて配置してみました。

```python
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()

```

#### 実行結果
たくさんできました。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/331462/38091375-13df-0b0d-5378-07eae46de39c.png)

なんだか雰囲気が出たので満足ですね。
![roselia_3x3_wallpaper_24.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/331462/b7a7202d-8cc0-1428-844f-21be86771e69.jpeg)


## 感想
やりたかったことは達成できたのでよかったです。時間があるときにさらに応用していきたいですね。