Skip to content

Instantly share code, notes, and snippets.

@codecaffeine
Last active February 21, 2024 22:22
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecaffeine/10292934 to your computer and use it in GitHub Desktop.
Save codecaffeine/10292934 to your computer and use it in GitHub Desktop.
Random Image Generator (imagemagick + ruby)
#!/usr/bin/env ruby
# Script to generate random images. Based on http://www.imagemagick.org/Usage/canvas/#random_blur
require 'optparse'
options = {}
optparse = OptionParser.new do |opts|
opts.banner = "Usage: randimg [options] [filename1] [filename2] ..."
opts.on( '-s', '--size HxV', 'create image of size HxV (400x400 default)' ) do |size|
options[:size] = size
end
options[:size] ||= "400x400"
opts.on( '-t', '--type TYPE', 'random generator type (noise|plasma)' ) do |size|
case size
when 'plasma'
options[:type] = "plasma:"
end
end
options[:type] ||= "xc: +noise Random"
opts.on( '-n', '--number NUMBER', 'number of files to create (ignores filenames)' ) do |number|
options[:number] = number.to_i
end
opts.on( '-h', '--help', 'Display this screen' ) do
puts opts
exit
end
end
optparse.parse!
if options[:number]
for i in 1..options[:number]
`convert -size #{options[:size]} #{options[:type]} 'file #{i}.png'`
end
else
files = (!ARGV.empty? && ARGV) || ["random.png"]
files.each do |f|
`convert -size #{options[:size]} #{options[:type]} #{f}`
end
end
@codecaffeine
Copy link
Author

randimg -n 100 will create 100 random files named file 0.png to file 100.png

@ZakharDay
Copy link

Thanks for the script! Good work. One thought is to replace space to underscore in file name (line 40). It will be easier to use generated files on the web.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment