RubyおよびRMagickによる解像度の並べ替え

良い一日。 Habrを読んで、 「PythonとPILを使用した解像度による画像の並べ替え」という記事( 「PowerShellシーンでの解像度 による画像の並べ替え」という記事)に出会い、コメントを読み、Rubyで似たようなものを書きたいと思いました。

画像



私を判断せず、批判するだけにしてください! 実際、最近Rubyを習い始めたばかりか、これが私の最初のプログラムであるため、見つかったエラーはすべて有益です。 プログラムは、RMagickライブラリを使用してRubyで記述されています。 プログラムの仕組み:



1)ユーザーは、写真のあるディレクトリへのパスを入力します(jpg、png、gifは要素を展開します-(jpg | png | gif |その他の形式)

2)次に、写真の解像度に等しい名前のサブディレクトリが作成されるフォルダーへのパス。

3)次に、プログラムは、写真付きのディレクトリを再帰的に「実行」し、拡張子がjpg | png | gifのファイルを見つけ、解像度を決定し、名前に適切な解像度のディレクトリが存在するかどうかを確認します。

4)ファイルをコピーし、コピーされたファイルの数をカウントします。



Rubyコードは少しきれいに見えます-私の意見です。 ファイルを移動するのではなくコピーするプログラムでのみ、同様のRubyとPythonコードを比較することに興味がある人もいると思います。



まだ行われていないこと:コピーされたファイルの存在を確認し、存在する場合は名前を変更します。



Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  1. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  2. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  3. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  4. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  5. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  6. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  7. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  8. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  9. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  10. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  11. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  12. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  13. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  14. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  15. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  16. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  17. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  18. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  19. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  20. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  21. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  22. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  23. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  24. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  25. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  26. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  27. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  28. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  29. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  30. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  31. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  32. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )



  33. Copy Source | Copy HTML require 'find' require 'fileutils' require 'RMagick' def image_sort (unsort_path, sort_path) count = 0 if ! File .directory?(sort_path) FileUtils .mkdir sort_path end if File .directory?(unsort_path) Find .find(unsort_path) { |fname| if /\.(jpg|png|gif)$/i =~ fname count += 1 img = Magick::Image::read(fname).first wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s if ! File .directory?(sort_path + wh_dir_name) FileUtils .mkdir sort_path + wh_dir_name end FileUtils .cp(fname, sort_path + wh_dir_name) puts fname + ' copied' end } else puts unsort_path + ' not found' end puts count .to_s + ' file(s) copied' end puts 'Path to pictures: ' pd = gets pd. chomp ! puts 'Path to new sort directory: ' sd = gets sd. chomp ! image_sort ( pd, sd )







結果:

画像

画像

そのため、テスト用の写真のサイズが異なっていました。



All Articles