Class: Imgen::Image
- Inherits:
-
Object
- Object
- Imgen::Image
- Defined in:
- lib/imgen.rb
Instance Method Summary collapse
-
#display_image(img, options) ⇒ Object
optionally display image after creation.
-
#initialize(options) ⇒ Image
constructor
main entry point.
-
#make_image(img, options) ⇒ Object
image processing.
-
#make_new_pixel(new_name) ⇒ Object
create new Pixel from color name.
-
#write_image_to_file(img, options) ⇒ Object
write image to disk.
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]) #img.colorspace = Magick::RGBColorspace colors = {r: 0, g: 0, b: 0} [:color_dominant] = colors.keys.to_a.sample make_image(img, ) 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, ) if [:display_x11] begin img.display rescue puts "could not display #{options[:filename_path]}" end elsif [: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, ) 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 [: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, ) display_image(img, ) 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, ) 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}/#{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 #{options[:filename_path]} to disk" if [:verbose] begin img.write([:filename_path]) rescue puts 'error writing image to disk' end end |