Class: Imgen::Image
- Inherits:
-
Object
- Object
- Imgen::Image
- Defined in:
- lib/imgen.rb
Instance Method Summary collapse
-
#initialize(options) ⇒ Image
constructor
main entry point.
-
#make_image(img, options) ⇒ Object
image processing.
Constructor Details
#initialize(options) ⇒ Image
main entry point
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/imgen.rb', line 8 def initialize() 1.upto([:quantity]) do img = Magick::Image.new([:width], [:height]) colors = {r: 0, g: 0, b: 0} color_dominant = colors.keys.to_a.sample [:color_dominant] = color_dominant make_image(img, ) end end |
Instance Method Details
#make_image(img, options) ⇒ Object
image processing
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 |
# File 'lib/imgen.rb', line 20 def make_image(img, ) last_pixel = {} new_pixel = {} (0..img.columns).each do |x| (0..img.rows).each do |y| case [:method] when :lines if !last_pixel.empty? if last_pixel[:color_dominant] == 100 new_pixel[[:color_dominant]] = 0 elsif last_pixel[:color_dominant] == 0 new_pixel[[:color_dominant]] = rand(1..10) + last_pixel[[:color_dominant]] else new_pixel[[:color_dominant]] = rand(-10..10) + last_pixel[[:color_dominant]] end else new_pixel[:r] = ([:color_dominant] == :r) ? rand(0..100) : 0 new_pixel[:g] = ([:color_dominant] == :g) ? rand(0..100) : 0 new_pixel[:b] = ([:color_dominant] == :b) ? rand(0..100) : 0 new_pixel[:a] = rand(0..100) end when :noise new_pixel[:r] = ([:color_dominant] == :r) ? rand(0..100) : 0 new_pixel[:g] = ([:color_dominant] == :g) ? rand(0..100) : 0 new_pixel[:b] = ([:color_dominant] == :b) ? rand(0..100) : 0 new_pixel[:a] = rand(0..100) end img.pixel_color(x,y,"rgba(#{new_pixel[:r]}%, #{new_pixel[:g]}%, #{new_pixel[:b]}%, #{new_pixel[:a]}%)") last_pixel = {r: new_pixel[:r], g: new_pixel[:g], b: new_pixel[:b], a: new_pixel[:a]} if [:debug] print "R#{new_pixel[:r].to_s.ljust(3)}" print "G#{new_pixel[:g].to_s.ljust(3)}" print "B#{new_pixel[:b].to_s.ljust(3)}" print "A#{new_pixel[:a].to_s.ljust(3)}| " end end print "\n" if [:debug] end img_dir = [:directory] img_ext = [:format] unless File.directory?(img_dir) FileUtils.mkdir_p(img_dir) end counter = 0 img_uniq = "_0" filename_path = "#{img_dir}/#{[:width]}x#{[:height]}#{img_uniq}.#{img_ext}" until !File.exists?(filename_path) counter += 1 img_uniq = "_" + counter.to_s filename_path = "#{img_dir}/#{[:width]}x#{[:height]}#{img_uniq}.#{img_ext}" end puts "writing #{filename_path} to disk" img.write(filename_path) if [:display] begin puts "displaying #{filename_path} in X11..." img.display rescue puts "could not display #{filename_path}" end end end |