-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathgenerate.rb
107 lines (69 loc) · 1.94 KB
/
generate.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
###################
# to run use:
#
# ruby ./generate.rb
require 'csvreader'
require 'pixelart'
def normalize( str )
## allow (ignore):
## space ( ),
## underscore (_),
## dash (-)
str.downcase.gsub( /[ _-]/, '' ).strip
end
def generate_punk( *values, dir: "./basic" )
punk_type = values[0]
attribute_names = values[1..-1]
punk_type = normalize( punk_type )
path = "#{dir}/#{punk_type}.png"
punk = Image.read( path )
m_or_f = if punk_type.index( 'female' )
'f'
else
'm'
end
attribute_names.each do |attribute_name|
next if attribute_name.nil? || attribute_name.empty? ## note: skip nil or empty string attributes
attribute_name = normalize( attribute_name )
path = "#{dir}/#{m_or_f}/#{attribute_name}.png"
attribute = Image.read( path )
punk.compose!( attribute )
end
punk
end # method generate
###
# test drive
# generate punk #0
punk = generate_punk( 'Female 2', 'Earring', 'Blonde Bob', 'Green Eye Shadow' )
punk.save( "./tmp/punk0.png" )
punk.zoom(20).save( "./tmp/[email protected]" )
# generate punk #1
punk = generate_punk( 'Male 1', 'Smile', 'Mohawk' )
punk.save( "./tmp/punk1.png" )
punk.zoom(20).save( "./tmp/[email protected]" )
##
# read in all meta data records of all 10 000 punks
recs = CsvHash.read( './punks.csv' )
puts "#{recs.size} punk(s)" #=> 10000 punk(s)
##
# let's go - generate all 10 000 using the records
recs.each_with_index do |rec,i|
puts "==> punk #{i}:"
pp rec
values = rec.values
punk = generate_punk( *values )
name = "punk#{i}"
punk.save( "./o/#{name}.png" )
punk.zoom(20).save( "./o/#{name}@20x.png" )
end
###
# bonus:
# generate the all-in-one composite using a 100x100 grid
punks = CompositeImage.new( 100, 100 )
recs.each_with_index do |rec,i|
values = rec.values
punk = generate_punk( *values )
punks << punk
end
punks.save( "./o/punks.png" )
puts "bye"