Skip to content

Instantly share code, notes, and snippets.

@chocomelonchan
Last active August 29, 2015 14:00
Show Gist options
  • Save chocomelonchan/11101240 to your computer and use it in GitHub Desktop.
Save chocomelonchan/11101240 to your computer and use it in GitHub Desktop.
require 'rmagick'
# file name
img_file_name = ARGV[0]
/(.+).png/ =~ img_file_name
base_img_file_name = $1
# get image data
img_data = Magick::ImageList.new(img_file_name)
xxhdpi_width = img_data.columns
xxhdpi_height = img_data.rows
RESIZE_IMAGES = [
{
'dir' => 'mdpi',
'scale' => 1.0
},
{
'dir' => 'hdpi',
'scale' => 1.5
},
{
'dir' => 'xhdpi',
'scale' => 2.0
},
{
'dir' => 'xxhdpi',
'scale' => 3.0
}
]
# resize image
mdpi_width = xxhdpi_width / 3
mdpi_height = xxhdpi_height / 3
img = Magick::Image.read(img_file_name).first
Dir.mkdir(base_img_file_name, 0755)
RESIZE_IMAGES.each do |resize_images|
dir = base_img_file_name + '/' + resize_images['dir']
width = mdpi_width * resize_images['scale']
height = mdpi_height * resize_images['scale']
Dir.mkdir(dir, 0755)
img.resize(width, height).write(dir + '/' + img_file_name)
end
#!/bin/sh
#xxhdpiから適当に各解像度毎の画像を生成
#ldpiはmdpi用意しておけば良いので要らない(公式)
#xxxhdpiにも対応したい。というか引数で指定するようにしてmutableな感じにしたい
if [ -z $1 ] ; then
echo "Please set \$1 xxhdpi file"
exit
fi
if [[ $1 =~ ^(.+).png$ ]] ; then
file_name=${BASH_REMATCH[0]}
dir_name=${BASH_REMATCH[1]}
# Get width
xxhdpi_width=`identify -format "%w" $1`
mdpi_width=`expr $xxhdpi_width / 3`
hdpi_width=`expr $mdpi_width + $mdpi_width / 2`
xhdpi_width=`expr $mdpi_width \* 2`
# Create files
`mkdir -p $dir_name/mdpi`
`convert -verbose -resize $mdpi_width $1 $dir_name/mdpi/$file_name`
`mkdir -p $dir_name/hdpi`
`convert -verbose -resize $hdpi_width $1 $dir_name/hdpi/$file_name`
`mkdir -p $dir_name/xhdpi`
`convert -verbose -resize $xhdpi_width $1 $dir_name/xhdpi/$file_name`
`mkdir -p $dir_name/xxhdpi`
`convert -verbose -resize $xxhdpi_width $1 $dir_name/xxhdpi/$file_name`
else
echo "\$1 is not png file"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment