Skip to content

Instantly share code, notes, and snippets.

@Keishake
Last active August 29, 2015 14:08
Show Gist options
  • Save Keishake/823bb4457814daecc51a to your computer and use it in GitHub Desktop.
Save Keishake/823bb4457814daecc51a to your computer and use it in GitHub Desktop.
ゲーム開発時のダミー画像を生成するスクリプト
#!/usr/bin/env ruby
require 'RMagick'
require "colorable"
require 'optparse'
include Colorable
def cmdline
args = {}
OptionParser.new do |parser|
parser.on('-s [VALUE]', '--size [VALUE]', 'size(ex: 100x150)') {|v| args[:size] = v}
parser.on('-f VALUE', '--filename VALUE', 'filename') {|v| args[:filename] = v}
parser.on('-n [VALUE]', '--count [VALUE]', 'image count') {|v| args[:num] = v}
parser.on('-d [VALUE]', '--directory [VALUE]', 'directory') {|v| args[:dir] = v}
parser.on('-c', '--circle', 'create circle image') {|v| args[:circle] = v}
parser.parse!(ARGV)
end
args
end
args = cmdline
size = args[:size] || "100x100"
filename = args[:filename]
num = args[:num] || 1
dir = args[:dir] || "."
sizeArr = size.split("x")
unless sizeArr.length == 2
puts "wrong size argment"
return false
end
width = sizeArr[0].to_i
height = sizeArr[1].to_i
textSize = height*0.1
if width < height
textSize = width*0.1
end
s = 64
v = 98
h = rand(360) + 1
for n in 1..num.to_i do
color = Color.new(HSB.new(h, s, v)).hex
h = h + 80
if h > 360
h = h - 360
end
filename_id = "#{filename}_#{n}"
caption = "#{filename_id}\n#{width}x#{height}"
file_path = "#{dir}/#{filename_id}.png"
canvas = Magick::Image.new(width, height) do |c|
if args[:circle]
c.background_color = "Transparent"
else
c.background_color = color
end
end
if args[:circle]
c = Magick::Draw.new
c.circle(width/2, height/2, width/2, height*0.9)
c.fill = color
c.draw(canvas)
end
text = Magick::Draw.new
text.gravity = Magick::CenterGravity
text.pointsize = textSize
text.annotate(canvas, width, height, 0, 0,caption)
canvas.write(file_path)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment