Skip to content

Instantly share code, notes, and snippets.

@foo9
Last active December 14, 2015 16:19
Show Gist options
  • Save foo9/5114761 to your computer and use it in GitHub Desktop.
Save foo9/5114761 to your computer and use it in GitHub Desktop.
# encoding: utf-8
require 'csv'
require 'rubygems'
require 'bundler/setup'
require 'rmagick'
def create_image(width, height, filename, ext)
img = Magick::Image.new(width, height) {
self.background_color = random_color()
}
imglist = Magick::ImageList.new
imglist << Magick::Image.new(img.columns, img.rows) {
self.background_color = "white"
}
imglist[0].alpha = Magick::ActivateAlphaChannel
imglist[0] = imglist.fx("(i+j)%8==0?#BBDDFF:0", Magick::AlphaChannel)
output_img = img.composite(imglist[0], Magick::CenterGravity, 0, 0, Magick::OverCompositeOp)
output_img.write("#{filename}.#{ext}")
end
def mkdir2(path)
return if FileTest.exist? path
parent = File.dirname path
mkdir2 parent
Dir.mkdir path
end
def random_color
hex = "%06x" % (rand * 0xffffff)
"##{hex}"
end
# $ ruby mockimg.rb hoge.csv
INPUT_FILE = ARGV[0]
OUTPUT_DIR = 'mock_images'
unless File.exists?(OUTPUT_DIR)
Dir.mkdir(OUTPUT_DIR)
end
CSV.foreach(INPUT_FILE, {headers: true, header_converters: :symbol}) do |row|
unless row.header_row?
p row[:width]
p row[:height]
p row[:filename]
p row[:ext]
mkdir2("#{OUTPUT_DIR}/" + File.dirname(row[:filename]))
create_image(row[:width].to_i, row[:height].to_i, "#{OUTPUT_DIR}/" + row[:filename], row[:ext])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment