Skip to content

Instantly share code, notes, and snippets.

@zmalltalker
Created January 22, 2014 13:08
Show Gist options
  • Save zmalltalker/8558407 to your computer and use it in GitHub Desktop.
Save zmalltalker/8558407 to your computer and use it in GitHub Desktop.
Convert image asset to Android *dpi versions
require "pathname"
require "fileutils"
original = "{query}"
original_path = Pathname(original)
command = "sips -g pixelWidth #{original}"
output = `#{command}`
size = output.split("\n").last.split(/\s/).last.to_i
variants = {"mdpi" => 0.75*size,"xhdpi" => 1.5*size,"xxhdpi" => 2.0*size}
base_path = original_path.dirname.realpath
filename = original_path.basename
variants.each {|name, size|
d = base_path + "drawable-#{name}"
d.mkpath
new_file_name = (d + filename).to_s
command = "sips --resampleWidth #{size.to_i} #{original} --out #{new_file_name}"
`#{command}`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment