Class: PixelDreamer::ImageDreamer

Inherits:
Object
  • Object
show all
Includes:
Magick
Defined in:
lib/pixel_dreamer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image, overlay_image = nil, app = false) ⇒ ImageDreamer

Returns a new instance of ImageDreamer.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pixel_dreamer.rb', line 21

def initialize(image, overlay_image = nil, app = false)
  @image = prepare_image(image)
  @overlay_image = overlay_image
  @input_name = name_parser(image)
  @parent_path = parent_path(image)
  @sequence_folder = sequence_folder
  @sequence_frame_path = sequence_frame_path
  @image_path = image_path
  @output_folder = output_folder
  @app = app
end

Instance Attribute Details

#imageObject

Returns the value of attribute image.



19
20
21
# File 'lib/pixel_dreamer.rb', line 19

def image
  @image
end

#overlay_imageObject

Returns the value of attribute overlay_image.



19
20
21
# File 'lib/pixel_dreamer.rb', line 19

def overlay_image
  @overlay_image
end

Class Method Details

.uri_helper(location, file_name) ⇒ Object

creates a uri by adding the name to common paths example: test = uri_helper(‘desktop’, ‘test.png’)



273
274
275
# File 'lib/pixel_dreamer.rb', line 273

def self.uri_helper(location, file_name)
  "/Users/#{ENV['USER']}/#{location}/" + file_name
end

Instance Method Details

#barrage(options = {}) ⇒ Object

creates an image for each setting from the settings hash quickest way to see how all of the settings effect the image supplied once the image has been instantiated you can run this method without passing in any parameters by default it is executed with these settings: gif: false, compress: true, speed: 84

example: image.barrage or with parameters, an output name (string) can be passed in as well image.barrage({ gif: true, compress: false, speed: 42, output_name: ‘image_glitched’ })

the output name must only include the name of the output image not the file extension the uri_helper can be used to create the input uri example using the uri_helper:



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/pixel_dreamer.rb', line 147

def barrage(options = {})
  options[:output_name] ||= @input_name
  options = Constants::BARRAGE_DEFAULTS.merge(options)
  output_name = options[:output_name]
  gif = options[:gif]
  counter = 1
  prepare(counter, options[:compress])
  puts 'Begin barrage.'

  Constants::SETTINGS.each do |key, setting_hash|
    brute_sort_save_with_settings(image: image, settings: setting_hash, output_name: (output_name + "_#{key}"),
                                  gif: gif, output_folder: true)
    puts "Image #{counter}/#{Constants::SETTINGS.length} complete."
    counter += 1
  end
  gif(options) if gif
end

#brute_sort_save_with_settings(options = {}) ⇒ Object

pixel sorts an image and outputs a text file with settings used once the image has been instantiated you can run this method without passing in any parameters by default it runs with these paramaters: { settings: DEFAULTS, output_name: nil, gif: false, output_folder: false } example: image.brute_sort_save_with_settings or with these parameters

image.brute_sort_save_with_settings(settings: PixelDreamer::Constants::DEFAULTS, output_name: nil, gif: false, output_folder: false) or image.brute_sort_save_with_settings(settings: { reverse: false, vertical: false, diagonal: false,

smooth: false, method: 'sum-rgb', verbose: false,
min: Float::INFINITY, max: Float::INFINITY,
trusted: false, middle: false })

also read the documentation for pxlsrt



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/pixel_dreamer.rb', line 66

def brute_sort_save_with_settings(options = {})
  options[:image] ||= @image
  options = Constants::BRUTE_SORT_SAVE_WITH_SETTINGS_DEFAULTS.merge(options)
  image = options[:image]
  resize!(image) if options[:resize]
  compress!(image) if options[:compress]
  settings = Constants::DEFAULTS.merge(options[:settings])
  output_name = options[:output_name]
  gif = options[:gif]
  output_folder = options[:output_folder]

  f = Pxlsrt::Brute.brute(image, reverse: settings[:reverse], vertical: settings[:vertical],
                          diagonal: settings[:diagonal], smooth: settings[:smooth], method: settings[:method],
                          verbose: settings[:verbose], min: settings[:min], max: settings[:max],
                          trusted: settings[:trusted], middle: settings[:middle]
  ).save(file_name_with_settings(image, settings, output_name, gif, output_folder))
  if @overlay_image
    pixel_sorted = Magick::Image.read(f.path).first
    overlay = Magick::Image.read(@overlay_image).first

    result = pixel_sorted.composite(overlay, Magick::CenterGravity, Magick::OverCompositeOp)
    result.write(f.path)
  end
  f.path
end

#compress(img, path) ⇒ Object

copies and compresses file passed cannot compress b&w images



193
194
195
196
197
198
199
200
# File 'lib/pixel_dreamer.rb', line 193

def compress(img, path)
  puts 'Compressing image.'
  image_optim = ImageOptim.new(allow_lossy: true, verbose: false, skip_missing_workers: true, optipng: false,
                               pngcrush: false, pngquant: { allow_lossy: true },
                               advpng: false, pngout: false, svgo: false)
  File.rename(image_optim.optimize_image(img), path)
  puts 'Image copied and compressed.'
end

#compress!(img) ⇒ Object

compresses image in place



204
205
206
207
208
209
210
211
# File 'lib/pixel_dreamer.rb', line 204

def compress!(img)
  puts 'Compressing image.'
  image_optim = ImageOptim.new(allow_lossy: true, verbose: false, skip_missing_workers: true, optipng: false,
                               pngcrush: false, pngquant: { allow_lossy: true },
                               advpng: false, pngout: false, svgo: false)
  image_optim.optimize_image(img)
  puts 'compressed.'
end

#convert_to_png(image_uri) ⇒ Object

converts image to png



43
44
45
46
47
48
49
# File 'lib/pixel_dreamer.rb', line 43

def convert_to_png(image_uri)
  path_without_ext = File.join(File.dirname(image_uri), File.basename(image_uri, '.*'))
  end_image = path_without_ext + '.png'
  image = Image.read(image_uri).first
  image.write(end_image)
  end_image
end

#gif(options = {}) ⇒ Object

creates a gif using the @sequence_folder path and outputs gif into the @output_folder path once the image has been instantiated you can run this method without passing in any parameters by default it is executed with these settings: speed: 84, dither: DITHER_DEFAULTS, image_delay: IMAGE_DELAY_DEFAULTS

example: image.gif or with parameters, an output name (string) can be passed in as well image.gif(42, dither: DITHER_DEFAULTS, image_delay: IMAGE_DELAY_DEFAULTS, output_name: ‘image_gif’)

or image.gif(42, dither: {active: false, number_of_colors: 200,

image_delay: {active: false, image_to_delay: 1, delay_length: 1000}, patrol: true, output_name: 'image_gif'})

speed is used to set the length of time for each frame of the gif, it defaults to milliseconds

- 12 fps: length of frame = 84 ms
- 24 fps: length of frame = 42 ms
- 30 fps: length of frame = 33 ms
- 60 fps: length of frame = 17 ms

the dither hash used to set the dither settings the image_delay hash is used to pause the sequence on an image for a set amount of time the patrol boolean is used to have the animation reverse at the end of it’s cycle



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/pixel_dreamer.rb', line 234

def gif(options = {})
  options[:output_name] ||= @input_name
  options = Constants::GIF_DEFAULTS.merge(options)
  options[:image_delay] = Constants::IMAGE_DELAY_DEFAULTS.merge(options[:image_delay])
  options[:dither] = Constants::DITHER_DEFAULTS.merge(options[:dither])
  image_delay = options[:image_delay]
  dither = options[:dither]

  sorted_dir = Dir["#{@sequence_folder}*.png"].sort_by do |x|
    b = x[/_(\d+)/]
    if b.nil?
      0
    else
      b.delete('_').to_i
    end
  end
  animation = ImageList.new(*sorted_dir)
  animation.concat(patrol(animation)) unless options[:patrol].nil?
  animation.ticks_per_second = 1000
  puts 'Got images.'
  animation.delay = options[:speed]
  animation[(image_delay[:image_to_delay] - 1)].delay = image_delay[:delay_length] if image_delay[:active]
  puts 'Creating GIF.'
  animation = dither(animation, dither[:number_of_colors]) if dither[:active]
  animation = animation.deconstruct if @overlay_image && @app
  animation.write("#{@output_folder}#{options[:output_name]}.gif")

  if (File.size("#{@output_folder}#{options[:output_name]}.gif") / 1000000) > 8 && @app
    animation = dither(animation, 256)
    animation.write("#{@output_folder}#{options[:output_name]}.gif")
  end

  puts 'Complete.'
  "#{@output_folder}#{options[:output_name]}.gif"
end

#glitch_sequence(options = {}) ⇒ Object

creates a sequence of pixel sorted images based on the setting hash and a sequence_setting hash chosen once the image has been instantiated you can run this method without passing in any parameters by default it is executed with these settings: settings: SETTINGS, sequence_settings: SEQUENCE_SETTINGS, compress: true, speed: 84 example: image.glitch_sequence or with parameters, an output name can be passed in as well (string) image.glitch_sequence({ settings: SETTINGS, sequence_settings: SEQUENCE_SETTINGS,

compress: false, speed: 42, output_name: 'image_glitched' })

the output name must only include the name of the output image not the file extension this creates a sequence of of images that have been pixel sorted in with increments specified

the settings_hash can be pulled from the SETTINGS, defaults to SETTINGS the sequence_settings cans be pulled from the SEQUENCE_SETTINGS, defaults to SEQUENCE_SETTINGS compress defaults to true, copies and compresses input file and creates sequence from compressed file the fps is set by the speed which is in milliseconds, defaults to 84ms (12fps) the uri_helper method can be used to create the input uri



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/pixel_dreamer.rb', line 109

def glitch_sequence(options = {})
  options[:output_name] ||= @input_name
  options = Constants::GLITCH_SEQUENCE_DEFAULTS.merge(options)
  settings = options[:settings]
  sequence_settings = options[:sequence_settings]
  output_name = options[:output_name]
  counter = sequence_settings[:counter]
  prepare(counter, options[:compress])
  puts 'Begin glitch sequence.'

  image_number = 1
  while counter < sequence_settings[:break_point]
    settings[:min] = counter
    settings[:max] = counter * sequence_settings[:max_multiple]
    brute_sort_save_with_settings(image: @path, settings: settings, output_name: (output_name + "_#{image_number}"),
                                  gif: true, output_folder: true)
    puts "IMAGE #{image_number}/#{sequence_settings[:break_point] - sequence_settings[:counter]} COMPLETE"
    image_number += 1
    counter += sequence_settings[:increment]
  end
  gif(options) if options[:gif]
end

#randomize(options = {}) ⇒ Object

creates a series of images that are pixel sorted with randomized settings the output is very hectic once the image has been instantiated you can run this method without passing in any parameters by default it is executed with these settings: gif: false, compress: true, speed: 84, image_number: 10

example: image.randomize or with parameters, an output name (string) can be passed in as well image.randomize({ gif: trues, compress: false, speed: 42, image_number: 20, output_name: ‘image_glitched’ })

the output name must only include the name of the output image not the file extension the amount of images that are created are based on the image_number (integer)



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/pixel_dreamer.rb', line 178

def randomize(options = {})
  options[:output_name] ||= @input_name
  options = Constants::RANDOMIZE_DEFAULTS.merge(options)
  prepare(1, options[:compress])
  puts 'Being randomizing.'

  options[:image_number].times do
    brute_sort_save_with_settings(image: image, settings: randomize_settings, output_name: (options[:output_name] + random_name),
                                  gif: options[:gif], output_folder: true)
  end
end

#show_output_folderObject



37
38
39
# File 'lib/pixel_dreamer.rb', line 37

def show_output_folder
  @output_folder
end

#show_sequence_folderObject



33
34
35
# File 'lib/pixel_dreamer.rb', line 33

def show_sequence_folder
  @sequence_folder
end