Wednesday, November 21, 2012

Ruby png Manipulation

Nice, short ruby script I came across showing basic png usage (albeit with a twisted mutator ;)). [disclaimer] This may not work with ruby-1.9

[sourcecode language="ruby"]
require 'rubygems'
require 'png'

def f(x,y)
((x^y) & ((y-350) >> 3 )) ** 2
end

canvas = PNG::Canvas.new(500.500)

0.upto(499) do |y|
0.upto(499) do |x|
if ((f(x,y) >> 12) & 1) == 1
canvas[x,499-y] = PNG::Color::Black
else
canvas[x,499-y] = PNG::Color::White
end
end
end

png = PNG.new(canvas)
png.save 'ruby.png'
[/sourcecode]

5 comments:

  1. I think this is a powerfull weblog with a lot interesting posts about this stuff. And i just wanna thank you for this. I'll follow your website to see if you post more stuff like these!

    ReplyDelete
  2. Thanks. Yeah I post stuff like this all the time, feel free to follow [:

    ReplyDelete
  3. hello from across the ocean I'm Sarah I'm such a silly girl but I still really appreciated your posts

    ReplyDelete
  4. No prob, thanks for the chillstep mix [:

    ReplyDelete