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])
    #img.colorspace = Magick::RGBColorspace
    colors = {r: 0, g: 0, b: 0}
    options[:color_dominant] = colors.keys.to_a.sample

    make_image(img, options)
  end
end

Instance Method Details

#display_image(img, options) ⇒ Object

optionally display image after creation



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/imgen.rb', line 89

def display_image(img, options)
  if options[:display_x11]
    begin
      img.display
    rescue
      puts "could not display #{options[:filename_path]}"
    end
  elsif options[:display_imgcat]
    begin
      system("imgcat #{options[:filename_path]}")
    rescue
      puts "could not display #{options[:filename_path]}"
    end
  end
end

#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
# File 'lib/imgen.rb', line 20

def make_image(img, options)
  index_range = (-5..5)
  color_name_index = rand(0..COLOR_NAMES.length - 1)

  # iterate over each pixel
  (0..img.columns).each do |x|
    (0..img.rows).each do |y|
      case options[:method]
      when :solid
        pixel = make_new_pixel(COLOR_NAMES[color_name_index])
      when :texture
        new_color_name_index = (color_name_index + rand(index_range))

        # fix boundary of index
        if new_color_name_index < 0
          color_name_index = 0
        elsif new_color_name_index >= COLOR_NAMES.length
          color_name_index = COLOR_NAMES.length - 1
        else
          color_name_index = new_color_name_index
        end

        pixel = make_new_pixel(COLOR_NAMES[color_name_index])
      end

      # save pixel color
      img.pixel_color(x, y, pixel)
    end
  end

  write_image_to_file(img, options)

  display_image(img, options)
end

#make_new_pixel(new_name) ⇒ Object

create new Pixel from color name



56
57
58
# File 'lib/imgen.rb', line 56

def make_new_pixel(new_name)
  return Magick::Pixel.from_color(new_name)
end

#write_image_to_file(img, options) ⇒ Object

write image to disk



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
# File 'lib/imgen.rb', line 61

def write_image_to_file(img, options)
  img_dir = options[:directory]
  img_ext = options[:format]

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

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

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

  puts "writing #{options[:filename_path]} to disk" if options[:verbose]

  begin
    img.write(options[:filename_path])
  rescue
    puts 'error writing image to disk'
  end
end