Skip to content

Instantly share code, notes, and snippets.

@yasuoza
Created June 10, 2012 05:10
Show Gist options
  • Save yasuoza/2903890 to your computer and use it in GitHub Desktop.
Save yasuoza/2903890 to your computer and use it in GitHub Desktop.
Retina image converter
require 'rubygems'
require 'RMagick'
convertedCnt = 0
ARGV.each do |filename|
img_type = File.extname(filename).downcase
if (img_type==".png")||(img_type==".jpg")||(img_type==".tif")
ext = File.extname(filename)
base = File.basename(filename,ext)
dir = File.dirname(filename)
next if dir == "converted"
if /^#{dir}\/(.+)@2x(#{ext})$/ =~ filename
exportdir = dir+"/converted"
exportname = exportdir+"/"+$1+$2
folderFlag = true
if !FileTest.exist?(exportdir)
Dir.mkdir(exportdir)
else
if FileTest.file?(exportdir)
puts "Error!: #{exportdir} exists."
folderFlag = false
end
end
if folderFlag
original = Magick::Image.read(filename).first
width = original.columns
height = original.rows
# Resize
image = original.resize(width/2,height/2,filter=Magick::LanczosFilter,0.9)
image.write(exportname)
convertedCnt += 1
end
end
end
end
puts "Converted #{convertedCnt} images."
@yasuoza
Copy link
Author

yasuoza commented Jun 10, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment