Rubyã§è¤æ°ã®ç»åã®ãµã¤ãºãä¸æ¬ã§èª¿ã¹ã¦Excelã«æ¸ãåºãã
ã
ããããã«
Photoshopãªã©ã§æ¨ªé·ã»ç¸¦é·ã®ç»åã§ã¢ã¯ã·ã§ã³ãå¤æ´ãããæã«
沢山ã®ç»åã®ä¸ããããããç»åã®ãµã¤ãºã調ã¹ã¦ã横é·ã»ç¸¦é·ããã©ã«ãã«ä»åããã®ãé¢åã ã£ãã®ã§ä½ãã¾ããã
å¾æ¥ãã£ããã£ã®ããããã
æ©è½
調ã¹ããç»åã®ãããã£ã¬ã¯ããªã§å®è¡ããã¨
ãã£ã¬ã¯ããªå
ã®ç»åï¼jpg|jpeg|pngï¼ã対象ã«ç»åã®ãµã¤ãºã調ã¹ã¾ãã
# ãªãã·ã§ã³
åºåã¯ExcelãCSVã§æ¸ãåºãã¾ãã
å¿
è¦ã§ããã°ãèªåã§ä»¥ä¸ã®ãã©ã«ãã«ç»åãä»åãã¾ãã
ãSameãã縦横ã®ãµã¤ãºãåã
ãWidthãã横é·
ãHeightãã縦é·
ãUnknownãããªãããããããããã©ãé§ç®ã£ã½ã
ãERRORããç»åã®ãµã¤ãºãèªã¿è¾¼ããªã
ã
â» Excelã®æ¸ãåºãã¯Windowsã®ã¿ã§ãã
â» ãã¡ã¤ã«ã®ç§»åã¯ãªãªã¸ãã«ãã¡ã¤ã«ã移åããã®ã§æ³¨æã§ãã
åºåä¾ãExcel
file name | width | height | large |
---|---|---|---|
error.jpg | ? | ? | ERROR |
height19y396f96tf6tef.jpeg | 500 | 501 | Height |
same12312yydsdhf76.png | 500 | 500 | Same |
width126et7tr7w6t67re.jpg | 501 | 500 | Width |
ã
ã³ã¼ã Ruby
Github READMEã¾ã æ¸ãã¦ãªãã§ããâ¦
# image_size_search.rb
# coding: utf-8 require 'rubygems' require 'image_size' require 'win32ole' class ImageSizeSearch def initialize() @file = [] @file_table = [] @file_table << ['file name', 'width', 'height', 'large'] @path = Dir.pwd.gsub('/', '\\') << '\\' @timestamp = Time.now.to_i end def file_get Dir.glob('*') do |f| # support: bmp, gif, jpeg, pbm, pcx, pgm, png, ppm, psd, swf, tiff, xbm, xpm if /.*?\.(jpg|jpeg|png)/ =~ f @file << f end end end def file_open @file.each do |file| open(file, 'rb') do |f| img = ImageSize.new(f.read) if img.format.nil? @file_table << [file, '?', '?', 'ERROR'] else size = img.width - img.height if size === 0 large = 'Same' elsif size > 0 large = 'Width' elsif size < 0 large = 'Height' else large = 'Unknown' end @file_table << [file, img.width, img.height, large] end end end end def write_csv File.open("image_size_#{@timestamp}.csv", 'w') do |file| @file_table.each do |size| file.puts size.join(',') end end end def write_excel excel = WIN32OLE.new('Excel.Application') # excel.visible = true workbook = excel.workbooks.add @file_table.each_with_index do |array, i| array.each_with_index do |size, j| workbook.sheets[1].rows[i+1].columns[j+1] = size end end workbook.saveAs "#{@path}image_size_search_#{@timestamp}.xlsx" # excel.quit end def mv_file Dir.mkdir("#{@path}Same") unless Dir.exist?("#{@path}Same") Dir.mkdir("#{@path}Width") unless Dir.exist?("#{@path}Width") Dir.mkdir("#{@path}Height") unless Dir.exist?("#{@path}Height") Dir.mkdir("#{@path}Unknown") unless Dir.exist?("#{@path}Unknown") Dir.mkdir("#{@path}ERROR") unless Dir.exist?("#{@path}ERROR") @file_table.each_with_index do |file, i| if !(i === 0) File.rename(file[0], "#{@path}#{file[3]}/#{file[0]}") end end end end iss = ImageSizeSearch.new() iss.file_get iss.file_open # ===== Option ===== # iss.write_csv # iss.write_excel # iss.mv_file # ===== Option =====
ã
æºå
- ãimage_sizeããå¿ è¦ã§ãã
gem install image_size
- ãwin32oleãã¯Ruby1.8以ä¸ã§æ¨æºã§å ¥ã£ã¦ãã¿ããã§ã
ã
ãªãã·ã§ã³
- ã³ã¡ã³ãã¢ã¦ãã解é¤ãã¦ä½¿ç¨ãã¾ãããâ»è¤æ°å¯
# iss.write_csvããâ CSVã§æ¸ãåºãå ´å # iss.write_excelãâ Excelã§æ¸ãåºãå ´å # iss.mv_fileãããâ ãã©ã«ãã«ä»åããå ´å
ã
使ãæ¹
- ãimage_size_search.rbãã調ã¹ããç»åã®ãããã©ã«ãã«ç§»åããã¦å®è¡ãã¦ä¸ããã
ruby image_size_search.rb
ã
EXEå
- ãã¡ãã¡ã³ãã³ãã§å®è¡ããã®ããã©ããã¼ã£ã¦è¨ãå ´åã¯EXEåãã¦ã¿ãã¨è¯ããã
gem install ocra ocra --windows image_size_search.rb
ä½æãããEXEãã¡ã¤ã«ã調ã¹ãããã©ã«ãã«ç§»åããã¦å®è¡ããã°å¤§ä¸å¤«ã§ãã
ã
ãããã«
Rubyå ¨ç¶æ¸ãã¦ãªãã£ãã®ã§ãã³ã¼ããæªããããã