Class: ImagesConvertCLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/images_convert.rb

Overview

images_convert.rb: Thorベースの画像変換スクリプト 形式やリサイズ、入出力ディレクトリは設定値を使用(config コマンドで変更可能)

Constant Summary collapse

DOWNLOADS_DIR =
"#{ENV['HOME']}/Downloads"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.command_name ⇒ Object

コマンド名を imgconv に設定(短縮形として imgconv を使用)



93
94
95
# File 'lib/images_convert.rb', line 93

def self.command_name
  'imgconv'
end

Instance Method Details

#cleanup ⇒ Object



562
563
564
# File 'lib/images_convert.rb', line 562

def cleanup
  ImagesConvert::Cleanup.run
end

#config(*args) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/images_convert.rb', line 191

def config(*args)
  if args.first.to_s == 'reset'
    unless options[:force]
      say '設定ファイルを既定値に戻します。よろしいですか? [y/N]'
      print '> '
      answer = (STDIN.gets || '').strip.downcase
      unless %w[y yes].include?(answer)
        say 'リセットをキャンセルしました。'
        return
      end
    end

    ImagesConvert::Configuration.reset_to_defaults
    say '設定を既定値にリセットしました。'
    say ''
    say '現在の設定:'
    show_config_summary
    return
  end

  # サブコマンド: set FROM TO RESIZE
  if args.first.to_s == 'set'
    _, from, to, resize = args
    unless from && to && resize
      say "使い方: images-convert config set FROM TO RESIZE", :red
      exit 1
    end
    ImagesConvert::Configuration.save_defaults(from: from, to: to, resize: resize)
    say "設定を変更しました:"
    say "  デフォルト入力形式: #{from}"
    say "  デフォルト出力形式: #{to}"
    say "  デフォルトリサイズ: #{resize}"
    return
  end

  # サブコマンド: set-dir INPUT_DIR OUTPUT_DIR
  if args.first.to_s == 'set-dir'
    _, input_dir, output_dir = args
    unless input_dir && output_dir
      say "使い方: images-convert config set-dir INPUT_DIR OUTPUT_DIR", :red
      exit 1
    end
    ImagesConvert::Configuration.save_directories(input_dir: input_dir, output_dir: output_dir)
    say "ディレクトリ設定を変更しました:"
    say "  デフォルト入力ディレクトリ: #{input_dir}"
    say "  デフォルト出力ディレクトリ: #{output_dir}"
    return
  end

  if args.first.to_s == 'set-latest-mode'
    _, mode = args
    unless mode
      say "使い方: images-convert config set-latest-mode MODE (mtime/exif)", :red
      exit 1
    end

    normalized = mode.to_s.downcase
    begin
      ImagesConvert::Configuration.save_latest_mode(normalized)
    rescue ArgumentError => e
      say "エラー: #{e.message}", :red
      exit 1
    end

    say "最新判定モードを #{normalized} に設定しました。"
    return
  end

  if args.first.to_s == 'set-waifu2x'
    new_scale = if options[:scale]
                  clamp_waifu2x_scale(options[:scale])
                else
                  ImagesConvert::Configuration.default_waifu2x_scale
                end

    new_noise = if options[:noise]
                  clamp_waifu2x_noise(options[:noise])
                else
                  ImagesConvert::Configuration.default_waifu2x_noise
                end

    new_model = options[:model] || ImagesConvert::Configuration.default_waifu2x_model

    auto_scale_default = ImagesConvert::Configuration.default_waifu2x_auto_scale_a4
    new_auto_scale = options[:auto_scale_a4].nil? ? auto_scale_default : options[:auto_scale_a4]

    a4_print_default = ImagesConvert::Configuration.default_waifu2x_a4_print
    new_a4_print = options[:a4_print].nil? ? a4_print_default : options[:a4_print]

    if options[:image_format]
      normalized_format = normalize_waifu2x_output_format(options[:image_format])
      unless %w[png jpg jpeg webp].include?(normalized_format)
        say "エラー: waifu2x 出力フォーマットは png/jpg/jpeg/webp のいずれかを指定してください。", :red
        exit 1
      end
    end
    new_image_format = if options[:image_format]
                         normalize_waifu2x_output_format(options[:image_format])
                       else
                         normalize_waifu2x_output_format(ImagesConvert::Configuration.default_waifu2x_image_format)
                       end

    new_on_error = options[:on_error] || ImagesConvert::Configuration.default_waifu2x_on_error

    ImagesConvert::Configuration.save_waifu2x_settings(
      scale: new_scale,
      noise: new_noise,
      model: new_model,
      auto_scale_a4: new_auto_scale,
      a4_print: new_a4_print,
      image_format: new_image_format,
      on_error: new_on_error
    )

    say "waifu2x 設定を更新しました:"
    say "  拡大率: #{new_scale}"
    say "  ノイズ除去レベル: #{new_noise}"
    say "  モデル: #{new_model}"
    say "  A4自動スケール: #{new_auto_scale}"
    say "  A4レイアウトPDF: #{new_a4_print}"
    say "  出力フォーマット: #{new_image_format}"
    say "  エラー時の動作: #{new_on_error}"
    return
  end

  if args.first.to_s == 'set-output'
    new_image_quality = if options[:image_quality]
                          clamp_output_quality(options[:image_quality])
                        else
                          ImagesConvert::Configuration.default_output_image_quality
                        end

    new_pdf_quality = if options[:pdf_quality]
                        clamp_output_quality(options[:pdf_quality])
                      else
                        ImagesConvert::Configuration.default_output_pdf_quality
                      end

    new_pdf_compression = if options[:pdf_compression]
                            normalized = options[:pdf_compression].to_s.strip.downcase
                            %w[jpeg jpeg2000 zip].include?(normalized) ? normalized : ImagesConvert::Configuration.default_output_pdf_compression
                          else
                            ImagesConvert::Configuration.default_output_pdf_compression
                          end

    new_pdf_density = if options[:pdf_density]
                        clamp_pdf_density(options[:pdf_density])
                      else
                        ImagesConvert::Configuration.default_output_pdf_density
                      end

    ImagesConvert::Configuration.save_output_settings(
      image_quality: new_image_quality,
      pdf_quality: new_pdf_quality,
      pdf_compression: new_pdf_compression,
      pdf_density: new_pdf_density
    )

    say "出力設定を更新しました:"
    say "  画像品質: #{new_image_quality}"
    say "  PDF 品質: #{new_pdf_quality}"
    say "  PDF 圧縮方式: #{new_pdf_compression}"
    say "  PDF DPI: #{new_pdf_density}"
    return
  end

  # それ以外(引数なし)は現在の設定を表示
  settings = ImagesConvert::Configuration.show_all_settings

  # 初回実行時はメッセージが既に表示されているので、改行だけ
  puts "" unless ImagesConvert::Configuration.first_run_message_displayed?

  show_config_summary(settings)

  say "設定を変更するには:"
  say "  images-convert set FROM TO RESIZE"
  say "  images-convert set-dir INPUT_DIR OUTPUT_DIR"
  say "  images-convert set-latest-mode MODE (mtime/exif)"
  say "  images-convert set-waifu2x [--scale N --noise N --model TYPE --no-auto-scale-a4 --no-a4-print --image-format FORMAT --on-error skip]"
  say "  images-convert set-output [--image-quality N --pdf-quality N --pdf-compression TYPE --pdf-density DPI]"
end

#convert(target = nil, dest_dir = nil) ⇒ Object



649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
# File 'lib/images_convert.rb', line 649

def convert(target = nil, dest_dir = nil)
  from   = options[:from]   || ImagesConvert::Configuration.default_from
  to     = options[:to]     || ImagesConvert::Configuration.default_to
  resize = options[:resize] || ImagesConvert::Configuration.default_resize
  latest_mode = latest_mode_from_options
  pdf_density = clamp_pdf_density(options[:pdf_density])

  # 最新N件/全件変換(入力ディレクトリを走査)
  if options[:all] || (options.key?(:latest) && options[:latest].to_s.strip != '')
    # コマンドライン引数でディレクトリが指定されている場合は優先
    input_dir = if target && File.directory?(target)
                  File.expand_path(target)
                else
                  options[:input_dir] || ImagesConvert::Configuration.default_input_dir
                end
    output_dir = if dest_dir
                   File.expand_path(dest_dir)
                 else
                   options[:output_dir] || ImagesConvert::Configuration.default_output_dir
                 end

    unless Dir.exist?(input_dir)
      say "エラー: 入力ディレクトリ '#{input_dir}' が存在しません。", :red
      exit 1
    end

    # 入力候補の列挙
    exts = candidate_exts(from)
    candidates = list_input_candidates(input_dir, exts)

    if candidates.empty?
      say "対象ファイルが見つかりませんでした (#{input_dir}, 拡張子: #{exts.uniq.join('/')})."
      return
    end

    # 最新順の並べ替え(EXIF 対応)
    sorted, exif_fallback_used = sort_candidates_by_mode(candidates, latest_mode)
    sorted = sorted.uniq
    mode, count = resolve_selection(sorted)
    case mode
    when :latest
      say "最新の #{count} 件を変換します (#{input_dir}, 基準: #{latest_mode})"
      if latest_mode == 'exif' && exif_fallback_used
        say '  ※ EXIF 情報がないファイルはファイル更新日時で判定しました。'
      end
    when :all
      say "すべての画像ファイルを変換します (#{input_dir})"
    else
      say "エラー: --latest N (N>=1) または --all / -0 を指定してください(--latest 0 は全件として扱います)", :red
      exit 1
    end

    targets = sorted.first(count)

    targets.each do |filepath|
      base = File.basename(filepath, '.*')
      perform_conversion(base, from, to, resize, input_dir, output_dir, suffix: options[:suffix], pdf_density: pdf_density)
    end
    return
  end

  # 一括変換(ディレクトリ指定)
  if target && File.directory?(target)
    src_dir = File.expand_path(target)
    out_dir = dest_dir ? File.expand_path(dest_dir) : src_dir # 単一引数時は in-place
    unless Dir.exist?(src_dir)
      say "エラー: 入力ディレクトリ '#{src_dir}' が存在しません。", :red
      exit 1
    end
    FileUtils.mkdir_p(out_dir)
    perform_bulk_conversion(
      src_dir,
      out_dir,
      from,
      to,
      resize,
      overwrite: options[:overwrite],
      suffix: options[:suffix],
      recursive: options[:recursive],
      delete: options[:delete],
      pdf_density: pdf_density
    )
    return
  end

  # 単一ファイル変換: target はファイル名または番号(IMG_1234 を想定)
  input_dir = options[:input_dir] || ImagesConvert::Configuration.default_input_dir
  output_dir = if dest_dir
                 File.expand_path(dest_dir)
               else
                 options[:output_dir] || ImagesConvert::Configuration.default_output_dir
               end

  # --output オプションが指定されている場合は出力ファイルを直接指定
  if options[:output] && target
    # --output で指定されたパスをフルパスに変換
    output_file_path = File.expand_path(options[:output])
    output_dir = File.dirname(output_file_path)
    output_basename = File.basename(output_file_path, '.*')
    # 出力ファイル名を指定されたものに変更
    def self.custom_output_filename(input_file, to, output_dir, custom_basename, suffix: nil, create_dir: true)
      basename = suffix && !suffix.empty? ? "#{custom_basename}#{suffix}" : custom_basename
      FileUtils.mkdir_p(output_dir) if create_dir && !Dir.exist?(output_dir)
      "#{output_dir}/#{basename}.#{to.downcase}"
    end

    # 入力がフルパス/実在パスなら input_dir 検証をスキップしてそのまま使う
    input_path_like = target.to_s.include?('/') || File.exist?(target.to_s)
    input_file = nil
    if input_path_like
      candidate = File.expand_path(target.to_s)
      if File.exist?(candidate)
        input_file = candidate
      end
    end

    # まだ見つからない場合は従来どおり input_dir + base 名から探索
    unless input_file
      validate_arguments(target, input_dir)
      input_file = find_input_file(target, from, input_dir)
      unless input_file
        say "エラー: 指定されたファイル '#{target}' (#{from} 形式) は #{input_dir} に存在しません。", :red
        exit 1
      end
    end

    normalized_to = normalized_to_format(to)
    output_file = custom_output_filename(input_file, normalized_to, output_dir, output_basename)

    if attempt_waifu2x_single(
        input_file: input_file,
        normalized_to: normalized_to,
        resize: resize,
        output_path: output_file,
        overwrite: options[:overwrite],
        delete_original: options[:delete],
        settings: waifu2x_runtime_settings(target_format: normalized_to),
        explicit_scale: options[:waifu2x_scale]
      )
      return
    end

    if File.exist?(output_file) && !options[:overwrite]
      say "- 既存のためスキップ: #{output_file}(--overwrite で上書き可能)", :yellow
      return
    end

    say "#{input_file} を #{output_file} に変換しています..."

    begin
      image = MiniMagick::Image.open(input_file)
      image.resize resize unless resize.empty?
      image.format normalized_to
      apply_image_quality(image, effective_output_quality)
      image.write output_file

      say "変換成功! 新しいファイルは #{output_file} です。", :green

      if options[:delete]
        FileUtils.rm(input_file)
        say "元のファイル #{input_file} は削除されました。", :yellow
      else
        say '元のファイルは削除されませんでした。削除するには --delete オプションを使用してください。', :blue
      end
    rescue => e
      say "エラー: 変換に失敗しました。#{e.message}", :red
      say 'ImageMagick のインストール状況を確認してください。', :red
      return
    end
    return
  end

  perform_conversion(target, from, to, resize, input_dir, output_dir, suffix: options[:suffix], pdf_density: pdf_density)
end

#help(command = nil, subcommand = false) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/images_convert.rb', line 108

def help(command = nil, subcommand = false)
  if command
    if command.to_s == 'convert'
      say <<~HELP
        Usage:
          imgconv convert TARGET [DEST_DIR]

        Options:
          -i, --input-dir=INPUT_DIR        入力ディレクトリ(デフォルト: 設定値または ~/Downloads)
          -o, --output=OUTPUT              出力ファイルパス(単一ファイル変換時、ディレクトリ指定時は出力ディレクトリ)
              --output-dir=OUTPUT_DIR      出力ディレクトリ(単一ファイル時)
          -d, --delete                     変換後に元のファイルを削除
          -f, --overwrite                  既存の出力ファイルがある場合に上書き
          -s, --suffix=SUFFIX              出力ファイル名の末尾に付与するサフィックス(例: _converted)
              --recursive                  ディレクトリを再帰的に処理(一括変換時)
              --resize=RESIZE              リサイズ値(例: 1920x, 50%)
              --latest=N                   最新の画像ファイル N 件を変換(入力ディレクトリ内から)
              --latest-exif                 最新 N 件の選定を EXIF 撮影日時に基づいて行う(--latest と併用)
              --all                         入力ディレクトリ内の対象画像をすべて変換
              --from=FROM                  入力形式(設定を一時的に上書き)
              --to=TO                      出力形式(設定を一時的に上書き)

        Description:
          使い方:

            images-convert [convert] TARGET [DEST_DIR] [オプション]
            imgconv [convert] TARGET [DEST_DIR] [オプション]

          動作:
            - TARGET がファイル名/番号の場合は単一ファイル変換(-i/-o で入出力ディレクトリ指定可)
            - TARGET がディレクトリの場合は一括変換(DEST_DIR 未指定時は in-place 変換)

          主なオプション:
            -i, --input-dir=DIR       入力ディレクトリ(単一ファイル時)
            -o, --output-dir=DIR      出力ディレクトリ(単一ファイル時)
            -d, --delete              変換後に元ファイルを削除
            -f, --overwrite           既存の出力ファイルがある場合に上書き
            -s, --suffix=SUFFIX       出力ファイル名の末尾に付与(例: _web)
            --recursive               ディレクトリを再帰的に処理(一括変換時)

          例:
            # 1枚だけ変換(番号指定、入出力ディレクトリや変換形式は既定値)
            images-convert [convert] 1234
            # または短縮コマンド
            imgconv [convert] 1234

            # 1枚だけ変換(ファイル名指定、入出力ディレクトリを明示)
            images-convert [convert] IMG_0001 ~/Downloads ~/Downloads

            # 一括変換(in-place)
            images-convert [convert] ./photos

            # 一括変換(出力先を別ディレクトリ、再帰処理、サフィックス付与)
            images-convert [convert] ./photos ./export --recursive --suffix _resized
      HELP
    else
      super
    end
  else
    say <<~HELP
      Commands:
        imgconv [convert] TARGET [INPUT_DIR] [OUTPUT_DIR]  # 単一ファイル変換(TARGET=ファイル名/番号)
        imgconv [convert] INPUT_DIR [OUTPUT_DIR]           # 一括変換
        imgconv config                                     # 現在の設定を表示
        imgconv set FROM TO [RESIZE]                       # 変換形式、リサイズ値の既定値を設定
        imgconv set-dir INPUT_DIR OUTPUT_DIR               # 入出力ディレクトリの既定値を設定
        imgconv --help                                     # 利用可能なコマンド一覧を表示
        imgconv [COMMAND] --help                           # 各コマンドの詳細を表示
        imgconv --version                                  # バージョン情報を表示
    HELP
  end
end

#self_uninstall ⇒ Object



569
570
571
572
573
574
575
576
577
578
579
580
581
# File 'lib/images_convert.rb', line 569

def self_uninstall
  ImagesConvert::Cleanup.run
  say ''
  say 'images-convert の gem をアンインストールしますか? [Y/n]'
  print '> '
  answer = (STDIN.gets || '').strip
  answer = 'y' if answer.empty?
  if answer.downcase.start_with?('y')
    system('gem uninstall images-convert')
  else
    say 'gem のアンインストールをキャンセルしました。'
  end
end

#set(from = nil, to = nil, resize = nil) ⇒ Object



524
525
526
527
528
529
530
531
532
# File 'lib/images_convert.rb', line 524

def set(from = nil, to = nil, resize = nil)
  unless from && to
    say "使い方: imgconv set FROM TO [RESIZE]", :red
    exit 1
  end

  resize ||= ImagesConvert::Configuration.default_resize
  config('set', from, to, resize)
end

#set_dir(input_dir = nil, output_dir = nil) ⇒ Object



535
536
537
538
539
540
541
542
# File 'lib/images_convert.rb', line 535

def set_dir(input_dir = nil, output_dir = nil)
  unless input_dir && output_dir
    say "使い方: imgconv set-dir INPUT_DIR OUTPUT_DIR", :red
    exit 1
  end

  config('set-dir', input_dir, output_dir)
end

#set_latest_mode(mode = nil) ⇒ Object



545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'lib/images_convert.rb', line 545

def set_latest_mode(mode = nil)
  unless mode
    say "使い方: imgconv set-latest-mode MODE (mtime/exif)", :red
    exit 1
  end

  normalized = mode.to_s.downcase
  unless %w[mtime exif].include?(normalized)
    say "エラー: MODE には mtime または exif を指定してください。", :red
    exit 1
  end

  ImagesConvert::Configuration.save_latest_mode(normalized)
  say "最新判定モードを #{normalized} に設定しました。"
end

#setup ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/images_convert.rb', line 374

def setup
  say '=== images-convert 設定ウィザード ===', :green
  say 'Enter キーのみで現在の設定値を維持できます。'
  say ''

  ImagesConvert::Configuration.load

  updated_sections = []
  changes = []

  if prompt_yes_no('1) 入出力形式とリサイズを変更しますか?', default: true)
    current_from = ImagesConvert::Configuration.default_from
    current_to = ImagesConvert::Configuration.default_to
    current_resize = ImagesConvert::Configuration.default_resize

    new_from = prompt_string('  入力形式', current_from)
    new_to = prompt_string('  出力形式', current_to)
    new_resize = prompt_string('  リサイズ値', current_resize)

    if [new_from, new_to, new_resize] != [current_from, current_to, current_resize]
      record_change(changes, 'デフォルト入力形式', current_from, new_from) if new_from != current_from
      record_change(changes, 'デフォルト出力形式', current_to, new_to) if new_to != current_to
      record_change(changes, 'デフォルトリサイズ', current_resize, new_resize) if new_resize != current_resize
      ImagesConvert::Configuration.save_defaults(from: new_from, to: new_to, resize: new_resize)
      updated_sections << '基本設定'
    end
  end

  if prompt_yes_no('2) 入出力ディレクトリを変更しますか?', default: false)
    current_input = ImagesConvert::Configuration.default_input_dir
    current_output = ImagesConvert::Configuration.default_output_dir

    new_input = prompt_path('  入力ディレクトリ', current_input)
    new_output = prompt_path('  出力ディレクトリ', current_output)

    if [new_input, new_output] != [current_input, current_output]
      record_change(changes, 'デフォルト入力ディレクトリ', current_input, new_input) if new_input != current_input
      record_change(changes, 'デフォルト出力ディレクトリ', current_output, new_output) if new_output != current_output
      ImagesConvert::Configuration.save_directories(input_dir: new_input, output_dir: new_output)
      updated_sections << 'ディレクトリ'
    end
  end

  if prompt_yes_no('3) 最新判定モードを変更しますか?', default: false)
    current_latest = ImagesConvert::Configuration.default_latest_mode
    new_latest = prompt_choice('  最新判定モード (mtime/exif)', current_latest, %w[mtime exif])

    if new_latest != current_latest
      record_change(changes, '最新判定モード', current_latest, new_latest)
      ImagesConvert::Configuration.save_latest_mode(new_latest)
      updated_sections << '最新判定モード'
    end
  end

  if prompt_yes_no('4) 出力品質を変更しますか?', default: false)
    current_image_quality = ImagesConvert::Configuration.default_output_image_quality
    current_pdf_quality = ImagesConvert::Configuration.default_output_pdf_quality
    current_pdf_compression = ImagesConvert::Configuration.default_output_pdf_compression
    current_pdf_density = ImagesConvert::Configuration.default_output_pdf_density

    new_image_quality = prompt_integer('  出力画像品質 (1-100)', current_image_quality, min: 1, max: 100)
    new_pdf_quality = prompt_integer('  PDF 品質 (1-100)', current_pdf_quality, min: 1, max: 100)
    new_pdf_compression = prompt_choice('  PDF 圧縮方式 (jpeg/jpeg2000/zip)', current_pdf_compression, %w[jpeg jpeg2000 zip])
    new_pdf_density = prompt_integer('  PDF DPI (72-1200)', current_pdf_density, min: 72, max: 1200)

    new_image_quality = clamp_output_quality(new_image_quality)
    new_pdf_quality = clamp_output_quality(new_pdf_quality)
    new_pdf_density = clamp_pdf_density(new_pdf_density)

    if [new_image_quality, new_pdf_quality, new_pdf_compression, new_pdf_density] != [current_image_quality, current_pdf_quality, current_pdf_compression, current_pdf_density]
      record_change(changes, '出力画像品質', current_image_quality, new_image_quality) if new_image_quality != current_image_quality
      record_change(changes, 'PDF 品質', current_pdf_quality, new_pdf_quality) if new_pdf_quality != current_pdf_quality
      record_change(changes, 'PDF 圧縮方式', current_pdf_compression, new_pdf_compression) if new_pdf_compression != current_pdf_compression
      record_change(changes, 'PDF DPI', current_pdf_density, new_pdf_density) if new_pdf_density != current_pdf_density
      ImagesConvert::Configuration.save_output_settings(
        image_quality: new_image_quality,
        pdf_quality: new_pdf_quality,
        pdf_compression: new_pdf_compression,
        pdf_density: new_pdf_density
      )
      updated_sections << '出力品質'
    end
  end

  if prompt_yes_no('5) waifu2x の設定を変更しますか?', default: false)
    current_scale = ImagesConvert::Configuration.default_waifu2x_scale
    current_noise = ImagesConvert::Configuration.default_waifu2x_noise
    current_model = ImagesConvert::Configuration.default_waifu2x_model
    current_auto_scale = ImagesConvert::Configuration.default_waifu2x_auto_scale_a4
    current_a4_print = ImagesConvert::Configuration.default_waifu2x_a4_print
    current_image_format = ImagesConvert::Configuration.default_waifu2x_image_format
    current_on_error = ImagesConvert::Configuration.default_waifu2x_on_error

    new_scale = prompt_integer('  waifu2x 拡大率 (1-4)', current_scale, min: 1, max: 4)
    new_noise = prompt_integer('  waifu2x ノイズ除去レベル (0-4)', current_noise, min: 0, max: 4)
    new_model = prompt_string('  waifu2x モデル (anime/photo/anime_rgb/cunet/upresnet10)', current_model)
    new_auto_scale = prompt_yes_no('  A4自動スケールを有効にしますか?', default: current_auto_scale, current_value: current_auto_scale)
    new_a4_print = prompt_yes_no('  A4レイアウトPDFを適用しますか?', default: current_a4_print, current_value: current_a4_print)
    new_image_format_raw = prompt_choice('  waifu2x 出力フォーマット (png/jpg/webp)', current_image_format, %w[png jpg jpeg webp])
    new_on_error = prompt_choice('  waifu2x エラー時の動作 (stop/skip)', current_on_error, %w[stop skip])

    new_scale = clamp_waifu2x_scale(new_scale)
    new_noise = clamp_waifu2x_noise(new_noise)
    new_image_format = normalize_waifu2x_output_format(new_image_format_raw)
    current_image_format_normalized = normalize_waifu2x_output_format(current_image_format)

    if [new_scale, new_noise, new_model, new_auto_scale, new_a4_print, new_image_format, new_on_error] != [current_scale, current_noise, current_model, current_auto_scale, current_a4_print, current_image_format_normalized, current_on_error]
      record_change(changes, 'waifu2x 既定倍率', current_scale, new_scale) if new_scale != current_scale
      record_change(changes, 'waifu2x ノイズ除去', current_noise, new_noise) if new_noise != current_noise
      record_change(changes, 'waifu2x モデル', current_model, new_model) if new_model != current_model
      record_change(changes, 'waifu2x A4自動スケール', current_auto_scale, new_auto_scale) if new_auto_scale != current_auto_scale
      record_change(changes, 'waifu2x A4レイアウトPDF', current_a4_print, new_a4_print) if new_a4_print != current_a4_print
      record_change(changes, 'waifu2x 出力フォーマット', current_image_format_normalized, new_image_format) if new_image_format != current_image_format_normalized
      record_change(changes, 'waifu2x エラー時動作', current_on_error, new_on_error) if new_on_error != current_on_error
      ImagesConvert::Configuration.save_waifu2x_settings(
        scale: new_scale,
        noise: new_noise,
        model: new_model,
        auto_scale_a4: new_auto_scale,
        a4_print: new_a4_print,
        image_format: new_image_format,
        on_error: new_on_error
      )
      updated_sections << 'waifu2x'
    end
  end

  if updated_sections.empty?
    say ''
    say '設定は変更されませんでした。', :yellow
  else
    say ''
    say '更新された設定:'
    updated_sections.each do |label|
      say "  - #{label}"
    end
    if changes.any?
      say ''
      say '変更点の詳細:'
      changes.each do |change|
        say "  - #{change[:label]}: #{change[:before]} => #{set_color(change[:after], :green)}"
      end
    end
  end

  say ''
  invoke :config
end

#version ⇒ Object



104
105
106
# File 'lib/images_convert.rb', line 104

def version
  say "images-convert #{ImagesConvert::VERSION}"
end