Skip to content

Instantly share code, notes, and snippets.

@cxx
Created September 26, 2011 13:44
Show Gist options
  • Save cxx/1242259 to your computer and use it in GitHub Desktop.
Save cxx/1242259 to your computer and use it in GitHub Desktop.
Twitterで圧縮されにくいアイコン生成
require 'RMagick'
include Magick
depth = 16
range = (1 << depth) - 1
orig_size = 48
if ARGV.length > 0
orig_img = Image.read(ARGV[0]).first
orig_img.set_channel_depth(AllChannels, depth)
orig_img = orig_img.resize_to_fill(orig_size)
else
orig_img = Image.new(orig_size, orig_size) do
self.depth = depth
end
(0...orig_size).each do |y|
(0...orig_size).each do |x|
orig_img.pixel_color(x, y, Pixel.new(*(0...3).map { Random.rand(range) }))
end
end
end
mag = 7
size = orig_size * mag
img = Image.new(size, size) do
self.depth = depth
end
rng = Random.new
(0...orig_size).each do |oy|
(0...orig_size).each do |ox|
orig_pixel = orig_img.pixel_color(ox, oy)
orig_vals = [:red, :green, :blue].map {|c| orig_pixel.send(c) }
ranges = orig_vals.map {|v| [v, range-v].min }
(0...mag).each do |my|
(0...mag).each do |mx|
mag_vals = (0...3).map do |i|
r = ranges[i]
orig_vals[i] + rng.rand(-r..r)
end
img.pixel_color(ox*mag+mx, oy*mag+my, Pixel.new(*mag_vals))
end
end
end
end
img.write("heavy.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment