Last Updated: February 25, 2016
·
546
· rane

Scale pixel art images to 200%

1x
2x

Usage: ruby 2x.rb [-f] image.png another_image.png

https://gist.github.com/raine/9390642

#!/usr/bin/env ruby

# [sudo] gem install rmagick
require 'rmagick'

def prepend_ext(path, with)
  with_ext = File.basename path
  ext      = File.extname path
  no_ext   = File.basename path, ext
  path.sub with_ext, "#{no_ext}#{with}#{ext}"
end

if ARGV.first === '-f'
  ARGV.shift
  force = true
end

if ARGV.empty?
  puts "Usage: #{__FILE__} [-f] FILE(S)"
  exit
end

ARGV.each do |path|
  img    = Magick::Image.read(path).first
  width  = img.columns
  height = img.rows
  dst    = (if force then path else "#{prepend_ext(path, '_2x')}" end)
  img.resize!(width * 2, height * 2, Magick::BoxFilter, 0)
  img.write dst
  puts "Wrote to #{dst}"
end