Last Updated: February 25, 2016
·
563
· selead

wallpapers

#!/usr/bin/env ruby

# 
# Resize file for set of predefined screen sizes (center crop)
# using Imagemagick
#
def resize_file(f)
  ext = File.extname f
  out = f[0...f.length-ext.length]
  sizes = [[640, 960], [960, 640], [640, 1136], [1136, 640], [1024, 768], [768, 1024]]
  sz = `identify -format "%[fx:w] %[fx:h]" #{f}`
  c = sz.split
  x = Integer c[0]
  y = Integer c[1]
  sizes.each { |s|
    new_x = s[0]
    new_y = s[1]
    if new_x > new_y                     #  landscape
      y1 =  y * new_x / x
      puts  "convert -resize #{new_x}x#{y1}! -quality 95% -gravity Center -crop #{new_x}x#{new_y}+0+0 #{f} #{out}-#{new_x}x#{new_y}#{ext}"
      `convert -resize #{new_x}x#{y1}! -quality 95% -gravity Center -crop #{new_x}x#{new_y}+0+0 #{f} #{out}-#{new_x}x#{new_y}#{ext}`

    else                        # portrait
      x1 = x * new_y / y
      puts  "convert -resize #{x1}x#{new_y}! -quality 95% -gravity Center -crop #{new_x}x#{new_y}+0+0 #{f} #{out}-#{new_x}x#{new_y}#{ext}"
      `convert -resize #{x1}x#{new_y}! -quality 95% -gravity Center -crop #{new_x}x#{new_y}+0+0 #{f} #{out}-#{new_x}x#{new_y}#{ext}`

    end
    puts "#{out}-#{new_x}x#{new_y}#{ext} ready"

  }
end

# make this with all files
ARGV.each do |a|
  resize_file a
end

usage:

wallparers.rb first.jpg second.png