Class: Imgen::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/imgen.rb

Instance Method Summary collapse

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(options)
  1.upto(options[:quantity]) do
    img = Magick::Image.new(options[:width], options[:height])
    colors = {r: 0, g: 0, b: 0}
    color_dominant = colors.keys.to_a.sample
    options[:color_dominant] = color_dominant

    make_image(img, options)
  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, options)
  last_pixel = {}
  new_pixel = {}

  (0..img.columns).each do |x|
    (0..img.rows).each do |y|
      case options[:method]
      when :lines
        if !last_pixel.empty?
          if last_pixel[:color_dominant] == 100
            new_pixel[options[:color_dominant]] = 0
          elsif last_pixel[:color_dominant] == 0
            new_pixel[options[:color_dominant]] = rand(1..10) + last_pixel[options[:color_dominant]]
          else
            new_pixel[options[:color_dominant]] = rand(-10..10) + last_pixel[options[:color_dominant]]
          end
        else
          new_pixel[:r] = (options[:color_dominant] == :r) ? rand(0..100) : 0
          new_pixel[:g] = (options[:color_dominant] == :g) ? rand(0..100) : 0
          new_pixel[:b] = (options[:color_dominant] == :b) ? rand(0..100) : 0
          new_pixel[:a] = rand(0..100)
        end
      when :noise
        new_pixel[:r] = (options[:color_dominant] == :r) ? rand(0..100) : 0
        new_pixel[:g] = (options[:color_dominant] == :g) ? rand(0..100) : 0
        new_pixel[:b] = (options[: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 options[: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 options[:debug]
  end

  img_dir = options[:directory]
  img_ext = options[:format]

  unless File.directory?(img_dir)
    FileUtils.mkdir_p(img_dir)
  end

  counter = 0
  img_uniq = "_0"
  filename_path = "#{img_dir}/#{options[:width]}x#{options[:height]}#{img_uniq}.#{img_ext}"

  until !File.exists?(filename_path)
    counter += 1
    img_uniq = "_" + counter.to_s
    filename_path = "#{img_dir}/#{options[:width]}x#{options[:height]}#{img_uniq}.#{img_ext}"
  end

  puts "writing #{filename_path} to disk"
  img.write(filename_path)

  if options[:display]
    begin
      puts "displaying #{filename_path} in X11..."
      img.display
    rescue
      puts "could not display #{filename_path}"
    end
  end
end