forked from EricR/image_sorcery
/
image_sorcery.rb
115 lines (99 loc) · 2.95 KB
/
image_sorcery.rb
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
require 'gm_support'
class Sorcery
attr_reader :file
def initialize(file)
@file = file
end
# Runs ImageMagick's 'mogrify'.
# See http://www.imagemagick.org/script/mogrify.php
#
def manipulate!(args={})
tokens = ["mogrify"]
tokens << convert_to_arguments(args) if args
tokens << " '#{@file}#{"[#{args[:layer].to_s}]" if args[:layer]}'"
tokens << " -annotate #{args[:annotate].to_s}" if args[:annotate]
tokens = convert_to_command(tokens)
success = run(tokens)[1]
if success && args[:format]
replace_file args[:format].to_s.downcase
end
success
end
# Runs ImageMagick's 'convert'.
# See http://www.imagemagick.org/script/convert.php
#
def convert(output, args={})
tokens = ["convert"]
tokens << convert_to_arguments(args) if args
tokens << " '#{@file}#{"[#{args[:layer].to_s}]" if args[:layer]}'"
tokens << " -annotate #{args[:annotate].to_s}" if args[:annotate]
tokens << " #{output}"
tokens = convert_to_command(tokens)
success = run(tokens)[1]
success
end
# Runs ImageMagick's 'identify'.
# See http://www.imagemagick.org/script/identify.php
#
def identify(args={})
tokens = ["identify"]
tokens << convert_to_arguments(args) if args
tokens << " '#{@file}#{"[#{args[:layer].to_s}]" if args[:layer]}'"
tokens = convert_to_command(tokens)
output = run(tokens)[0]
output
end
# Return the x and y dimensions of an image as a hash.
#
def dimensions
dimensions = identify(:layer => 0, :format => "%wx%h").chomp.split("x")
{ :x => dimensions[0].to_i,
:y => dimensions[1].to_i }
end
# Returns the x dimension of an image as an integer
#
def width
dimensions()[:x]
end
# Returns the y dimension of an image as an integer
#
def height
dimensions()[:y]
end
# Runs ImageMagick's 'montage'.
# See http://www.imagemagick.org/script/montage.php
#
def montage(sources, args={})
tokens = ["montage"]
tokens << convert_to_arguments(args) if args
sources.each {|source| tokens << " '#{source}'" }
tokens << " '#{@file}'"
tokens = convert_to_command(tokens)
success = run(tokens)[1]
end
def filename_changed?
(@filename_changed)
end
private
def replace_file(format)
File.delete @file
@file = File.join File.dirname(@file), File.basename(@file, File.extname(@file)) + "." + format
@filename_changed = true
unless File.exists? @file
@file = File.join File.dirname(@file), File.basename(@file, File.extname(@file)) + "-*." + format
end
end
def convert_to_command(tokens)
tokens[0] = prefix(tokens[0]) if respond_to? :prefix
tokens.flatten.join("")
end
def convert_to_arguments(args)
special_args = [:layer, :annotate]
args.reject {|k, v| special_args.include?(k) }.map {|k, v| " -#{k} '#{v}'"}
end
def run(cmds)
output = IO.popen(cmds.to_s) {|o| o.read }
success = $?.exitstatus == 0 ? true : false
[output,success]
end
end