📜 ⬆️ ⬇️

Sort images by resolution using Ruby and RMagick

Good day. While reading Habr, I came across an article “Sorting images by resolution using Python and PIL” (the article “Sorting images by resolution ... on the PowerShell scene” later appeared), read the comments and wanted to write something similar in Ruby.
image

Please do not judge me, but only to criticize! In fact, I just recently started learning Ruby, or rather, this is my first program, so any errors found will only be beneficial. The program is written in Ruby using the RMagick library. How the program works:

1) The user enters the path to the directory with pictures (jpg, png, gif expands elementary - (jpg | png | gif | other formats)
2) Then the path to the folder in which subdirectories will be created with the name equal to the resolution of the images.
3) Next, the program “runs” recursively in directories with images, finding files with the extension jpg | png | gif, determines the resolution and checks for the presence of a directory with the appropriate resolution in the name, if there is no such directory, it creates.
4) Copies a file, counts the number of copied files.

The Ruby code looks a bit prettier - my opinion. I think some people will be interested in comparing similar Ruby and Python code, but in my program, copying files instead of moving.
')
What is not done: check for the existence of the copied file and in the case of existence - renaming.

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 )


Result:
image
image
It so happened that the pictures for the test had a different size.

Source: https://habr.com/ru/post/85609/


All Articles