Class: Waifu2x::Processor

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

Defined Under Namespace

Classes: Waifu2xNotFoundError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.process_images(input_dir, output_dir, **options) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/waifu2x/processor.rb', line 13

def process_images(input_dir, output_dir, scale: 2, noise: 1, model: 'anime_style_art_rgb', processor: :waifu2x_ncnn_vulkan, waifu2x_bin: nil, models_path: nil, recursive: false, auto_scale_a4: false, pdf_density: nil, verbose: false, image_format: 'png', on_error: 'stop')
  FileUtils.mkdir_p(output_dir) unless Dir.exist?(output_dir)

  image_paths = find_images(input_dir, recursive: recursive)
  return if image_paths.empty?

  Parallel.each(image_paths, in_threads: [Parallel.processor_count, 1].max) do |image_path|
    local_scale = if auto_scale_a4
                    choose_scale_for_a4(image_path, target_dpi: (pdf_density || 300))
                  else
                    scale
                  end
    begin
      process_single_image(image_path, output_dir, local_scale, noise, model, processor, waifu2x_bin, models_path, verbose, image_format)
    rescue StandardError => e
      safe_path = begin
        image_path.to_s.encode('UTF-8', invalid: :replace, undef: :replace)
      rescue StandardError
        image_path.to_s
      end
      safe_err = begin
        e.message.to_s.encode('UTF-8', invalid: :replace, undef: :replace)
      rescue StandardError
        e.message.to_s
      end
      warn "[waifu2x] failed: #{safe_path} (#{e.class}: #{safe_err})"
      raise e if on_error != 'skip'
    end
  end
end

Instance Method Details

#process_images_without_test_stub ⇒ Object



40
# File 'lib/images_convert/waifu2x_test_stub.rb', line 40

alias_method :process_images_without_test_stub, :process_images