Skip to content

Instantly share code, notes, and snippets.

@yhirano
Created October 8, 2017 03:50
Show Gist options
  • Save yhirano/d12816aae2ae42fb33864c0f36d4e645 to your computer and use it in GitHub Desktop.
Save yhirano/d12816aae2ae42fb33864c0f36d4e645 to your computer and use it in GitHub Desktop.
指定したPNG画像の外周に透明の1ピクセルを追加するスクリプト。Androidの9patch用。
#!/usr/bin/env ruby
require 'pathname'
require 'rmagick'
if ARGV.empty?
puts 'Usage: convert9patch.rb [png]'
exit
end
if !File.exist?(ARGV[0])
puts "No such file #{ARGV[0]}."
exit
end
path_name = Pathname.new(ARGV[0])
dir_name = path_name.dirname
ext_name = path_name.extname
file_name = path_name.basename(ext_name)
img = Magick::ImageList.new(ARGV[0])
new_img = Magick::ImageList.new.new_image(img.columns + 4, img.rows + 4) {
self.background_color = 'none'
}
new_img.composite!(img, Magick::CenterGravity, Magick::OverCompositeOp)
new_img.write([dir_name, "#{file_name}.9#{ext_name}"].join(File::SEPARATOR))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment