Premshree Pillai ([info]premshree) wrote,
@ 2005-03-18 03:15:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Current music:Jewel - Foolish Games

While things compile...
You do this:

FIREFOXFIREFOXFIREFOXFIREFOXFIREF
OXFIREFOXFIREFOXFIREFOXFIREFOXFI
REFOXFIREFOXFIREFOXFIREFOXFIREFO
XFIREFOXFIREFOXFIREFOXFIREFOXFIR
EFOXFIREFOXFIREFOXFIREFOXFIREFOX
FIREFOXFIREFOXFIREFOXFIREFOXFIRE
FOXFIREFOXFIREFOXFIREFOXFIREFOXF
IREFOXFIREFOXFIREFOXFIREFOXFIREF
OXFIREFOXFIREFOXFIREFOXFIREFOXFI
REFOXFIREFOXFIREFOXFIREFOXFIREFO
XFIREFOXFIREFOXFIREFOXFIREFOXFIR
EFOXFIREFOXFIREFOXFIREFOXFIREFOX
FIREFOXFIREFOXFIREFOXFIREFOXFIRE
FOXFIREFOXFIREFOXFIREFOXFIREFOXF
IREFOXFIREFOXFIREFOXFIREFOXFIREF
OXFIREFOXFIREFOXFIREFOXFIREFOXF

You do know what this is, no?

#!/usr/local/bin/ruby
# $premshree$ $2005-03-18 03:13$

require 'GD'
include GD

img = Image.newFromPng(File.new('firefox.png', 'rb'))
text = 'FIREFOX'

width = img.width
height = img.height
temps = [0]*3
skip_pixels = 1
skip_lines = 2
char_next = 0
print '<pre style="background: #FFFFFF">'
height.times { |i|
	if temps[2] == 0
		width.times { |j|
			if temps[1] == 0
				idx = img.getPixel(j, i)
				color = img.rgb(idx)
				print "<span style=\"color: rgb(#{color[0]}, #{color[1]},
				 #{color[2]}); font-size:10px;\">#{text.split('')[char_next]}</span>"
				char_next = char_next + 1
				if char_next == text.split('').length then char_next = 0 end
			end
			temps[1] = temps[1] + 1
			if temps[1] == skip_pixels then temps[1] = 0 end
			if temps[0] == width
				print '<br />'
				temps[0] = 0
			end
			temps[0] = temps[0] + 1			
		}
	end
	temps[2] = temps[2] + 1
	if temps[2] == skip_lines then temps[2] = 0 end
}
print '</pre>'
Original image here.


(Post a new comment)


[info]shoan
2005-03-18 03:56 am UTC (link)
Neat.

(Reply to this)


[info]fox2mike
2005-03-18 04:20 am UTC (link)
kewl!

I think it would look better with firefox rather than FIREFOX...

(Reply to this)(Thread)


[info]premshree
2005-03-18 12:45 pm UTC (link)
Lowercase letters, especially when you have alphabets like d, f, g, etc., don’t look nice.

(Reply to this)(Parent)(Thread)


[info]bluesmoon
2005-03-18 10:55 pm UTC (link)
shudder! you said alphabets.

(Reply to this)(Parent)(Thread)


[info]premshree
2005-03-18 11:05 pm UTC (link)
Holy shit! I should drown in some beer.

(Reply to this)(Parent)


[info]jackol
2005-03-18 06:05 am UTC (link)
Heh... Nice!

(Reply to this)


[info]mansu
2005-03-18 09:59 am UTC (link)
Does this peice of code work for any image and text.
I am unable to understand this ruby code..so can't decipher.

(Reply to this)(Thread)


[info]teemus
2005-03-18 10:16 am UTC (link)
Does this peice of code work for any image and text.
Yes it will.

(Reply to this)(Parent)(Thread)

While I am bored...
[info]teemus
2005-03-18 11:13 am UTC (link)
The PHP version, if you please! :D
<?php
       /**
		Idea by Premshree
		Transliterated to PHP by Sumeet. ;)
		2005-03-18
	*/
	$img = imagecreatefrompng("firefox.png");
	$text = "firefox";
	$width = imagesx($img);
	$height = imagesy($img);
	$temps = array(0,0,0);
	$skipPixels = 1;
	$skipLines = 1;
	$charNext = 0;
	
	echo "<pre style='background: #ffffff'>";
	
	for($i = 0; $i < $height; $i++) {
		if($temps[2] == 0) {
			for($j = 0; $j < $width; $j++) {
				if($temps[1] == 0) {
					
					$idx = imagecolorat($img, $j, $i);
					$rgb = imagecolorsforindex($img, $idx);
					$r = dechex($rgb['red']);
					$g = dechex($rgb['green']);
					$b = dechex($rgb['blue']);
					$color = '#'.$r.$g.$b;
					$char = $text[$charNext];
					$charNext = $charNext + 1;
					
					if ($charNext == strlen($text) ) {
						$charNext = 0;
					}
					echo "<span style='color:$color; font-size:10px;'>$char</span>";
				}
				$temps[1] = $temps[1] + 1;
				if($temps[1] == $skipPixels) {
					$temps[1] = 0;
				}
				if($temps[0] == $width) {
					echo "<br />";
					$temps[0] = 0;
				}
				$temps[0] = $temps[0] + 1;
			}
		}
		$temps[2] = $temps[2] + 1;
		if($temps[2] == $skipLines) {
			$temps[2] = 0;
		}
	}
	
	echo "</pre>";
?>

(Reply to this)(Parent)(Thread)

Re: While I am bored...
[info]premshree
2005-03-18 12:45 pm UTC (link)
PHP code looks so... ancient, no? :D

(Reply to this)(Parent)

Re: While I am bored...
[info]code_martial
2005-03-18 08:53 pm UTC (link)
Couldn't you have used list() to initialise $r, $g, and $b? Also, I prefer +=. Wonder if $temps[0]++ does the Right Thing™ ;-)

(Reply to this)(Parent)

http://www.codemartial.org/mosaic.php
[info]code_martial
2005-03-18 10:44 pm UTC (link)

<?php
$img
= imagecreatefrompng("met.png");
$text = "METALLICA";
$textwidth = strlen($text);
$width = imagesx($img);
$height = imagesy($img);
$charNext = 0;

echo
"<div align='center' style='background: #000000'>";
for(
$i = 0; $i < $height; $i = $i + 2)
{
   for(
$j = 0; $j < $width; ++$j)
   {
      
$idx = imagecolorat($img, $j, $i);
      list(
$r, $g, $b) = array_values(imagecolorsforindex($img, $idx));
      
$color = "rgb($r, $g, $b)";
      
$char = $text[$charNext];
      
$charNext = (++$charNext)%$textwidth;
      echo
"<span style='color:$color; font-size:10px;'>$char</span>";
   }
   echo
"<br />";
}
echo
"</div>";
?>


(Reply to this)(Parent)(Thread)

Re: http://www.codemartial.org/mosaic.php
[info]premshree
2005-03-19 12:41 am UTC (link)
$textwidth = strlen($text);
..
$charNext = (++$charNext)%$textwidth;


PHP sure does screw up conventions. :-)

(Reply to this)(Parent)(Thread)

Re: http://www.codemartial.org/mosaic.php
[info]code_martial
2005-03-19 12:57 am UTC (link)
I assume all responsibility for that. This is exactly how I'd have done it in C++ as well

char_next = char_next + 1
if char_next == text.split('').length then char_next = 0 end

doesn't look any more conventional either :p Conditionals are, IMHO, difficult to read and understand.

(Reply to this)(Parent)(Thread)

Re: http://www.codemartial.org/mosaic.php
[info]premshree
2005-03-19 01:12 am UTC (link)
I was refering to the disconnect in the variable naming convention.

(Reply to this)(Parent)(Thread)

Re: http://www.codemartial.org/mosaic.php
[info]code_martial
2005-03-19 01:22 am UTC (link)
Are you referring to the style inconsistencies (charNext and textwidth)?

(Reply to this)(Parent)(Thread)

Re: http://www.codemartial.org/mosaic.php
[info]premshree
2005-03-19 02:39 am UTC (link)
yes.

(Reply to this)(Parent)

Re: http://www.codemartial.org/mosaic.php
[info]teemus
2005-03-19 04:55 am UTC (link)
Nicely done, Tahir saab.

(Reply to this)(Parent)


[info]premshree
2005-03-18 04:03 pm UTC (link)
Works for any image and text.

I am unable to understand this ruby code..so can't decipher.
If you skip all the ifs, it’ll probably be simpler to understand. All those ifs are basically for limiting the number of pixels to read.

PS: I easily managed to crash Firefox and Kate with a 200x200 image.

(Reply to this)(Parent)(Thread)


[info]mansu
2005-03-19 12:30 pm UTC (link)
"If you skip all the ifs, it’ll probably be simpler to understand. All those ifs are basically for limiting the number of pixels to read."

The If's were not a problem.

These were:
height.times { |i| If..

Upon looking at the php code i understood that these were continuations which IIRC ruby supports.
Am i right?

(Reply to this)(Parent)(Thread)


(Anonymous)
2005-03-19 01:17 pm UTC (link)
Actually this is just closures. It's pieces of code that can be attached to methods. In the above example the block just gets invoked height times and i will be a number from 0 to height - 1 telling you how often you already have been invoked.

(Reply to this)(Parent)


[info]premshree
2005-03-19 10:42 pm UTC (link)
It’s kind of like passing a parameter to a method.

(Reply to this)(Parent)


[info]teemus
2005-03-18 10:14 am UTC (link)
WOW. I just got another idea for a meme. :D Thanks dude!

(Reply to this)(Thread)


[info]teemus
2005-03-18 10:15 am UTC (link)
You do know what this is, no?
A mosaic, if I am not mistaken? :>

(Reply to this)(Parent)


[info]premshree
2005-03-18 02:54 pm UTC (link)
userpic mosaic, eh? :)

(Reply to this)(Parent)


[info]code_martial
2005-03-18 10:29 am UTC (link)
Shouldn't the last column be the first?

(Reply to this)(Thread)


[info]premshree
2005-03-18 12:47 pm UTC (link)
I think so... but didn’t bother trying to find the problem.

(Reply to this)(Parent)(Thread)


[info]code_martial
2005-03-18 08:59 pm UTC (link)
[info]teemus might have got it right. Bad initial value for skip_lines?

(Reply to this)(Parent)(Thread)


[info]code_martial
2005-03-18 10:45 pm UTC (link)
Nope, just overly complicated logic that was difficult to get right.

(Reply to this)(Parent)


[info]coreid
2005-03-20 07:09 am UTC (link)
Cool! this is inspiring me to learn ruby now :)

(Reply to this)(Thread)


[info]premshree
2005-03-22 09:55 am UTC (link)
Glad to hear that. :-)

Don’t learn, start using it.

(Reply to this)(Parent)


[info]manubhardwaj
2005-03-28 07:59 pm UTC (link)
#!/ur/local/bin/ruby

Is "/ur/" a special facet of Ruby or of Yahoo! FreeBSD, or does Ruby live a unique existence in that directory?!

(Reply to this)(Thread)


[info]premshree
2005-03-29 07:52 am UTC (link)
No, it’s a human error. :-) Corrected.

(Reply to this)(Parent)(Thread)


(Anonymous)
2005-08-25 04:41 am UTC (link)
You should use "#!/usr/bin/ENV ruby" for ultimate compatibility.

(Reply to this)(Parent)


Create an Account
Forgot your login?
Login w/ OpenID
English • Español • Deutsch • Русский…