概要
前回、以下の記事で集めてきた画像を使って私用のPC壁紙を作りました。PythonでできるならJuliaでもできるだろうと思い、Juliaで書き直しました。
検証環境
- Windows10
- Julia 1.2.0
使用したライブラリ
-
Images.jl
- An image processing library for Julia.
- Juliaで画像を扱うために利用しました
-
Glob.jl
- This implementation of Glob is based on the IEEE Std 1003.1, 2004 Edition (Open Group Base Specifications Issue 6) for fnmatch and glob. The specification of which can be found online: fnmatch and glob.
- 複数の画像を読み込むために利用しました
ソースコード
とりあえず画像を並べてみるプログラム
読み込んだ画像群に対して指定した枠に収まる数の画像をランダムに抽出して、抽出した画像をランダムに配置します。
using Images, ImageView, FileIO
using Glob
using Random
using StatsBase
function my_making_wallpaper(size_height, size_width, rh, rw, filename, inpath, dirpath)
if isdir(dirpath) == false
mkpath(dirpath)
end
inpdirs = glob(inpath)
out_dir = dirpath
images = [load(inpdir) for inpdir in inpdirs]
img_background = zeros(RGB{N0f8}, size_height*rh, size_width*rw)
all_img_nums = [i for i in range(1, stop = length(images))]
sort!(all_img_nums, rev=false)
pairs = [[i, j] for i in range(1, stop=size_height*rh-1, step=size_height) for j in range(1, stop=size_width*rw-1, step=size_width)]
select_img_point_nums = [i for i in range(1, stop=length(pairs))]
for count in range(1, stop=100)
select_img_nums = sample(all_img_nums, length(pairs), replace=false)
sort!(select_img_nums, rev=false)
randam_select_img_point_nums = sample(select_img_point_nums, length(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)
y1 = pairs[randam_select_img_point_num, 1][1]
y2 = pairs[randam_select_img_point_num, 1][1] + size_height - 1
x1 = pairs[randam_select_img_point_num, 1][2]
x2 = pairs[randam_select_img_point_num, 1][2] + size_width - 1
img_background[y1:y2, x1:x2] = copy(images[select_img_num])
end
name = out_dir * filename * "_wallpaper_" * string(count) * ".jpg"
save(name, img_background)
end
end
function main()
size_height=1002
size_width=1334
rh=3
rw=3
filename = "rinko_shirogane_3x3"
inpath = "../../../100.data/0.input/bang-dream_gbp/rinko_shirogane/card/*.png"
dirpath = "../../../100.data/1.output/julia/bang-dream_gbp/middle/rinko_shirogane/wallpaper/"
my_making_wallpaper(size_height, size_width, rh, rw, filename, inpath, dirpath)
end
main()
実行結果
とりあえず画像を並べてみるプログラム(プチ応用)
比率を変えて配置します。
function my_making_wallpaper_roselia(size_height, size_width, rh, rw, filename, inpath, dirpath)
if isdir(dirpath) == false
mkpath(dirpath)
end
inpdirs = glob(inpath)
out_dir = dirpath
base_img = load("../../../100.data/0.input/bang-dream_gbp/1.Roselia/IMG_20190804_211827_waifu2x_art_noise1_scale_tta_1.png")
base_img = imresize(base_img, size_height*3, size_width*4)
images = [load(inpdir) for inpdir in inpdirs]
background_height = size_height*rh + size(base_img)[1]
background_width = size_width*rw
img_background = zeros(RGB{N0f8}, background_height, background_width)
all_img_nums = [i for i in range(1, stop = length(images))]
sort!(all_img_nums, rev=false)
pairs = [[i, j] for i in range(1, stop=background_height-1, step=size_height) for j in range(1, stop=background_width-1, step=size_width)]
select_img_point_nums = [i for i in range(1, stop=length(pairs))]
for count in range(1, stop=100)
select_img_nums = sample(all_img_nums, length(pairs), replace=false)
sort!(select_img_nums, rev=false)
randam_select_img_point_nums = sample(select_img_point_nums, length(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)
y1 = pairs[randam_select_img_point_num, 1][1]
y2 = pairs[randam_select_img_point_num, 1][1] + size_height - 1
x1 = pairs[randam_select_img_point_num, 1][2]
x2 = pairs[randam_select_img_point_num, 1][2] + size_width - 1
img_background[y1:y2, x1:x2] = copy(images[select_img_num])
end
by1 = Int(round(background_height/2 - size(base_img)[1]/2))
by2 = Int(round(background_height/2 - size(base_img)[1]/2) + size(base_img)[1] - 1)
bx1 = Int(round(background_width/2 - size(base_img)[2]/2))
bx2 = Int(round(background_width/2 - size(base_img)[2]/2) + size(base_img)[2] - 1)
img_background[by1:by2, bx1:bx2] = copy(base_img)
name = out_dir * filename * "_wallpaper_" * string(count) * ".jpg"
save(name, img_background)
end
end
function main()
size_height=1002
size_width=1334
rh=4
rw=8
filename = "roselia_3x3"
inpath = "../../../100.data/0.input/bang-dream_gbp/1.Roselia/card/*.png"
dirpath = "../../../100.data/1.output/julia/bang-dream_gbp/middle/1.roselia/wallpaper/"
my_making_wallpaper_roselia(size_height, size_width, rh, rw, filename, inpath, dirpath)
end
main()
実行結果
Pythonのときと出力画像の比率を変えてみました。こっちもいい感じですね。
感想
Pythonと微妙に画素の操作や配列の操作が異なっていたので調整に少し時間が掛かりました...
とりあえず完成できてよかったです。