-
Notifications
You must be signed in to change notification settings - Fork 8
/
neural-tile-2x1.sh
152 lines (125 loc) · 4.82 KB
/
neural-tile-2x1.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#! /bin/bash
# Check for output directory, and create it if missing
if [ ! -d "$output" ]; then
mkdir output
fi
main(){
# 1. Defines the content image as a variable
input=$1
input_file=`basename $input`
clean_name="${input_file%.*}"
#Defines the style image as a variable
style=$2
style_dir=`dirname $style`
style_file=`basename $style`
style_name="${style_file%.*}"
#Defines the output directory
output="./output"
out_file=$output/$input_file
#Defines the overlap
overlap_w=50
overlap_h=50
# 2. Creates your original styled output. This step will be skipped if you place a previously styled image with the same name
# as your specified "content image", located in your Neural-Style/output/<Styled_Image> directory.
if [ ! -s $out_file ] ; then
neural_style $input $style $out_file
fi
# 3. Chop the styled image into 2x1 tiles with the specified overlap value.
out_dir=$output/$clean_name
mkdir -p $out_dir
convert $out_file -crop 2x1+"$overlap_w"+"$overlap_h"@ +repage +adjoin $out_dir/$clean_name"_%d.png"
#Finds out the length and width of the first tile as a refrence point for resizing the other tiles.
original_tile_w=`convert $out_dir/$clean_name'_0.png' -format "%w" info:`
original_tile_h=`convert $out_dir/$clean_name'_0.png' -format "%h" info:`
#Resize all tiles to avoid ImageMagick weirdness
convert $out_dir/$clean_name'_0.png' -resize "$original_tile_w"x"$original_tile_h"\! $out_dir/$clean_name'_0.png'
convert $out_dir/$clean_name'_1.png' -resize "$original_tile_w"x"$original_tile_h"\! $out_dir/$clean_name'_1.png'
#WxH
# 4. neural-style each tile
tiles_dir="$out_dir/tiles"
mkdir -p $tiles_dir
for tile in `ls $out_dir | grep $clean_name"_"[0-2]".png"`
do
neural_style_tiled $out_dir/$tile $style $tiles_dir/$tile
done
#Perform the required mathematical operations:
upres_tile_w=`convert $tiles_dir/$clean_name'_0.png' -format "%w" info:`
upres_tile_h=`convert $tiles_dir/$clean_name'_0.png' -format "%h" info:`
tile_diff_w=`echo $upres_tile_w $original_tile_w | awk '{print $1/$2}'`
tile_diff_h=`echo $upres_tile_h $original_tile_h | awk '{print $1/$2}'`
smush_value_w=`echo $overlap_w $tile_diff_w | awk '{print $1*$2}'`
smush_value_h=`echo $overlap_h $tile_diff_h | awk '{print $1*$2}'`
# 5. feather tiles
feathered_dir=$out_dir/feathered
mkdir -p $feathered_dir
for tile in `ls $tiles_dir | grep $clean_name"_"[0-2]".png"`
do
tile_name="${tile%.*}"
convert $tiles_dir/$tile -alpha set -virtual-pixel transparent -channel A -morphology Distance Euclidean:1,50\! +channel "$feathered_dir/$tile_name.png"
done
# 7. Smush the feathered tiles together
convert -background transparent \( $feathered_dir/$clean_name'_0.png' $feathered_dir/$clean_name'_1.png' +smush -$smush_value_w -background transparent \) \
$output/$clean_name.large_feathered.png
# 8. Smush the non-feathered tiles together
convert \( $tiles_dir/$clean_name'_0.png' $tiles_dir/$clean_name'_1.png' +smush -$smush_value_w \) \
$output/$clean_name.large.png
# 8. Combine feathered and un-feathered output images to disguise feathering.
composite $output/$clean_name.large_feathered.png $output/$clean_name.large.png $output/$clean_name.large_final.png
}
retry=0
#Runs the content image and style image through Neural-Style with your chosen parameters.
neural_style(){
echo "Neural Style Transfering "$1
if [ ! -s $3 ]; then
#####################################################################################
th neural_style.lua \
-content_image $1 \
-style_image $2 \
-image_size 640 \
-output_image out1.png \
-num_iterations 100
th neural_style.lua \
-content_image $1 \
-style_image $2 \
-init image -init_image out1.png \
-output_image $3 \
-image_size 768 \
-num_iterations 50
#####################################################################################
fi
if [ ! -s $3 ] && [ $retry -lt 3 ] ;then
echo "Transfer Failed, Retrying for $retry time(s)"
retry=`echo 1 $retry | awk '{print $1+$2}'`
neural_style $1 $2 $3
fi
retry=0
}
retry=0
#Runs the tiles through Neural-Style with your chosen parameters.
neural_style_tiled(){
echo "Neural Style Transfering "$1
if [ ! -s $3 ]; then
#####################################################################################
th neural_style.lua \
-content_image $1 \
-style_image $2 \
-image_size 640 \
-output_image out1.png \
-num_iterations 100
th neural_style.lua \
-content_image $1 \
-style_image $2 \
-init image -init_image out1.png \
-output_image $3 \
-image_size 768 \
-num_iterations 50
#####################################################################################
fi
if [ ! -s $3 ] && [ $retry -lt 3 ] ;then
echo "Transfer Failed, Retrying for $retry time(s)"
retry=`echo 1 $retry | awk '{print $1+$2}'`
neural_style_tiled $1 $2 $3
fi
retry=0
}
main $1 $2 $3