-
Notifications
You must be signed in to change notification settings - Fork 1
/
resize
executable file
·216 lines (179 loc) · 4.25 KB
/
resize
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env ruby
require 'parallel'
require 'optparse'
DIMENSIONS_DELIMITER = "x"
RATIO_OPTS = {
w: :width,
h: :height,
}
def parse_options(options)
{}.tap do |args|
OptionParser.new do |opts|
opts.banner =
"""resize images to a constant aspect ratio
Centers images on the canvas and adds a letterbox.
Thanks to Instagram's restrictive posting interface
for inspiring this utility.
Usage: resize <options> <file1> [<file2> ...]
Example
=======
Files:
1.png: 6000x4000
2.png: 4000x6000
Command:
resize -w 4 -h 5 -d output *.png
Yields:
output/1.png: 6000x7500
output/2.png: 4800x6000
Options
=======
"""
RATIO_OPTS.each do |short, long|
opts.on(
"-#{short} <ratio_#{long}>",
"--#{long} <ratio_#{long}>",
"The aspect ratio's #{long}",
Float
)
end
opts.on(
"-m <max_width>",
"--max_width <max_width>",
"The maximum width of the images (optional)",
Float
)
opts.on(
"-d <directory>",
"--dir <directory>",
"Output directory (defaults to 'out')",
String
)
opts.on(
"-c <color>",
"--color <color>",
"Letterbox color (defaults to 'white' for Instagram)",
String
)
opts.on(
"--help",
"Print this help text"
) do
puts opts
exit
end
end.parse!(options, into: args)
end
end
OPTS = parse_options(ARGV)
def dir
OPTS[:dir] || "out"
end
def color
OPTS[:color] || "white"
end
RATIO_OPTS.each do |short, long|
define_method(long) do
OPTS[long] || puts("Error: required option \"-#{short}\" or \"--#{long}\" missing") & exit(1)
end
end
def identify_dimensions(filename)
`identify -format "%w#{DIMENSIONS_DELIMITER}%h" "#{filename}"`
end
def dimensions_to_s(dimensions)
"#{dimensions[:width]}#{DIMENSIONS_DELIMITER}#{dimensions[:height]}"
end
def convert_command(file)
"convert \"#{file[:filename]}\" " +
"-resize #{dimensions_to_s(file[:dimensions])} " +
"-background #{color} " +
"-gravity center " +
"-extent #{dimensions_to_s(file[:dimensions])} " +
"\"#{dir}/#{file[:filename]}\" && " +
"exiftool " +
"-tagsFromFile \"#{file[:filename]}\" " +
"-All:All " +
"-IFD1:All " +
"-overwrite_original " +
"\"#{dir}/#{file[:filename]}\""
end
# yields this schema:
# {
# filename: "filename",
# dimensions: {
# width: 1234,
# height: 1234
# }
# }
def dimensions(filename)
{filename: filename}.merge(dimensions: Hash[
[:width, :height].zip(
identify_dimensions(filename)
.split(DIMENSIONS_DELIMITER)
.map(&:to_i)
)
])
end
def max_filename_length(files)
@max_filename_length ||= files
.map { |file| file[:filename] }
.map(&:length)
.max
end
def dimensions_template(files)
"%-#{max_filename_length(files)}s\t%s\n"
end
def puts_dimensions(files)
printf(dimensions_template(files), "File", "Dimensions")
files.map do |file|
printf(dimensions_template(files), file[:filename], "#{file[:dimensions][:width]}#{DIMENSIONS_DELIMITER}#{file[:dimensions][:height]}")
end
puts
end
def max_dimension(dimensions)
dimensions.key(dimensions.values.max)
end
def min_dimension(dimensions)
dimensions.key(dimensions.values.min)
end
def limit_width(file)
OPTS[:max_width] ? file.merge({
dimensions: file[:dimensions].merge({
width: OPTS[:max_width].to_i,
height: (
OPTS[:max_width]/(
file[:dimensions][:width].to_f / file[:dimensions][:height].to_f
)
).to_i
})
}) : file
end
def conversion_factor(key)
{
width: height/width,
height: width/height
}[key]
end
def new_dimensions(file)
file.merge({
dimensions: file[:dimensions].merge({
:"#{min_dimension(file[:dimensions])}" => (
file[:dimensions][max_dimension(file[:dimensions])] *
conversion_factor(max_dimension(file[:dimensions]))
).to_i
})
})
end
def print_and_run(shell_command)
shell_command
.tap(&method(:puts))
.tap(&method(:`))
end
Parallel.each(ARGV) do |arg|
[arg]
.map(&method(:dimensions))
.tap(&method(:puts_dimensions))
.map(&method(:new_dimensions))
.map(&method(:limit_width))
.map(&method(:convert_command))
.each(&method(:print_and_run))
end