Method: Pixelart::Image.convert
- Defined in:
- lib/pixelart/convert.rb
.convert(dir, from: 'jpg', to: 'png', outdir: nil, overwrite: true) ⇒ Object
helper to convert (all) image in directory
chech: move to ImageUtils.convert or such - why? why not?
what about the name e.g. rename to convert_dir or
batch_convert such - why? why not?
11 12 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 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/pixelart/convert.rb', line 11 def self.convert( dir, from: 'jpg', to: 'png', outdir: nil, overwrite: true ) outdir = dir if outdir.nil? files = Dir.glob( "#{dir}/*.#{from}" ) puts "==> found #{files.size} image(s) to convert from #{from} to #{to} (overwrite mode set to: #{overwrite})" files.each_with_index do |file,i| dirname = File.dirname( file ) extname = File.extname( file ) basename = File.basename( file, extname ) ## skip convert if target / dest file already exists next if overwrite == false && File.exist?( "#{outdir}/#{basename}.#{to}" ) ## note: make sure outdir exists (magic will not create it??) FileUtils.mkdir_p( outdir ) unless Dir.exist?( outdir ) cmd = "magick convert #{dirname}/#{basename}.#{from} #{outdir}/#{basename}.#{to}" puts " [#{i+1}/#{files.size}] - #{cmd}" ## todo/fix: check return value!!! magick comand not available (in path) and so on!!! system( cmd ) if from == 'gif' ## check for multi-images for gif ## save image-0.png to image.png path0 = "#{outdir}/#{basename}-0.#{to}" path = "#{outdir}/#{basename}.#{to}" ## note: image-0.png only exists (gets generated) for multi-images if File.exist?( path0 ) puts " saving #{path0} to #{path}..." blob = File.open( path0, 'rb' ) { |f| f.read } File.open( path, 'wb' ) { |f| f.write( blob ) } end end end end |