Repository navigation
Expand file tree
/
Copy pathannotate-meme
More file actions
executable file
·141 lines (114 loc) · 4.21 KB
/
Copy pathannotate-meme
File metadata and controls
executable file
·141 lines (114 loc) · 4.21 KB
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
#!/usr/bin/env ruby
# annotate-meme <input.png> [<output.png>] - annotate an image, meme caption style
require "colorize"
require "optimist"
$:.unshift(File.expand_path("~/bin/lib"))
require "dbrady_cli"
String.disable_colorization unless $stdout.tty?
class Application
include DbradyCli
def bottom?; opts[:bottom_given]; end
def top?; opts[:top_given]; end
opt_flag :open,
:preview
opt_reader :bottom,
:bottom_gravity,
:bottom_pointsize,
:bottom_y,
:fill,
:font,
:image_width,
:pointsize,
:stroke,
:stroke_width,
:top,
:top_gravity,
:top_pointsize,
:top_y
def run
@opts = Optimist.options do
banner <<BANNER
# annotate-meme <input.png> [<output.png>] - annotate an image, meme caption style
Options:
BANNER
opt :debug, "Print extra debug info", default: false
opt :pretend, "Print commands but do not run them", default: false
opt :verbose, "Run with verbose output (overrides --quiet)", default: false
opt :quiet, "Run with minimal output", default: false
opt :bottom, "Text to display at top", type: :string
opt :bottom_gravity, "Gravity to anchor bottom caption", default: "South"
opt :bottom_pointsize, "Bottom caption pointsize (overrides --pointsize)", type: :integer
opt :bottom_y, "Vertical offset of bottom caption", default: 0
opt :caption_width, "Maximum caption width in px", type: :integer
opt :fill, "Caption fill color", default: "white"
opt :font, "Font for caption", default: Application.best_font_for_this_os
opt :open, "Open output image after creation", default: false
opt :pointsize, "Caption pointsize", default: 144
opt :preview, "Preview output image after creation (requires chafa)", default: false
opt :stroke, "Caption stroke color", default: "black"
opt :stroke_width, "Caption stroke width", default: 5
opt :top, "Text to display at top", type: :string
opt :top_gravity, "Gravity to anchor top caption", default: "North"
opt :top_pointsize, "Top caption pointsize (overrides --pointsize)", type: :integer
opt :top_y, "Vertical offset of top caption", default: 0
end
Optimist::die "Error: you must specify at least one of --top or --bottom" unless top? || bottom?
opts[:quiet] = !opts[:verbose] if opts[:verbose_given]
puts opts.sort.to_h.inspect if debug?
on_run
end
def on_run
puts "Convert Command:"
run_command convert_command
run_command preview_command if preview?
run_command open_command if open?
end
def self.best_font_for_this_os
if linux?
"DejaVu-Sans-Bold"
else
"<no default font for this OS>"
end
end
def preview_command
%Q|chafa "#{output_image}"|
end
def open_command
osx? ? "open" : "xdg-open"
end
def image
@image ||= ARGV.first
end
def output_image
puts "File.basename(image): #{File.basename(image)}" if debug?
ext = File.extname(image)
image_basename = image[..ext.size]
opts[:output] || "#{image_basename}.out#{ext}"
end
def image_width
width = get_command_output(%Q|identify -format "%w" "#{image}|).to_i
end
def pointsize_for_top
top_pointsize || pointsize
end
def pointsize_for_bottom
bottom_pointsize || pointsize
end
def bottom_caption
return nil unless bottom?
%Q|\\( -background none -fill #{fill} -stroke #{stroke} -strokewidth #{stroke_width} -font #{font} -pointsize #{pointsize_for_bottom} -size #{image_width} -gravity #{bottom_gravity} caption:"#{bottom}" \\) -gravity #{bottom_gravity} -geometry +0+#{bottom_y} -composite|
end
def top_caption
return nil unless top?
%Q|\\( -background none -fill #{fill} -stroke #{stroke} -strokewidth #{stroke_width} -font #{font} -pointsize #{pointsize_for_top} -size #{image_width} -gravity North caption:"#{top}" \\) -gravity #{top_gravity} -geometry +0+#{top_y} -composite|
end
def convert_command
%Q|convert "#{image}" #{top_caption} #{bottom_caption} "#{output_image}"|
end
def self.linux?
@linux ||= `uname -s`.strip == "Linux"
end
end
if __FILE__ == $0
Application.new.run
end