Help us understand the problem. What is going on with this article?

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

概要

今までに集めてきた画像を使って私用の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()

実行結果

たくさんできました。
image.png
いいかんじですね。
rinko_shirogane_3x3_wallpaper_22.jpg

とりあえず画像を並べてみるプログラム(プチ応用)

ただ並べるだけだとあれなので比率を変えて配置してみました。

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

なんだか雰囲気が出たので満足ですね。
roselia_3x3_wallpaper_24.jpg

感想

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

reinsF82
プログラミング、仮想化技術、画像処理、機械学習、ネットワーク、情報セキュリティ等に興味あり。イラスト画像を集めるためにスクレイピングついて勉強中。
http://reinforce82.hatenablog.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした