-
Notifications
You must be signed in to change notification settings - Fork 26
/
spite.rb
63 lines (48 loc) · 1.05 KB
/
spite.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
#!/usr/bin/env ruby
require "RMagick"
require "fileutils"
module Card
def card_width
self.columns / 13
end
def card_height
self.rows / 5
end
def each_card
w = self.card_width
h = self.card_height
y = 0
["c", "d", "h", "s"].each do |suit|
x = 0
(1..13).each do |rank|
yield suit, rank, x, y
x += w
end
y += h
end
["facedown", "freeslot"].each_with_index do |name, i|
yield name, "", w * (2 + i), h * 4
end
end
def extract_card x, y
w = self.card_width
h = self.card_height
pixels = self.export_pixels x, y, w, h, "RGBA"
card = Magick::Image.new(w, h) do
self.background_color = Magick::Pixel.new 0, 0, 0, 255
end
card.import_pixels 0, 0, w, h, "RGBA", pixels
end
end
def split name
image = Magick::Image.read("#{name}.png").first
image.extend Card
FileUtils.mkdir_p name
image.each_card do |suit, rank, x, y|
card = image.extract_card x, y
card.write "#{name}/#{suit}#{rank}.png"
end
end
if ARGV[0]
split ARGV[0]
end