Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [QUIZ] Text Image (#50)

Reply
Thread Tools

[QUIZ] Text Image (#50)

 
 
Ruby Quiz
Guest
Posts: n/a
 
      10-07-2005
The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.

2. Support Ruby Quiz by submitting ideas as often as you can:

http://www.rubyquiz.com/

3. Enjoy!

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

This week's Ruby Quiz is to create a tool that will allow you to preview an
image file at the command-line in pure text.

Your program will need to read in an image, in whatever format you want to
support, and respond with a text representation of the image that fits in the
terminal.

For example, given the image:

http://rubyquiz.com/images/Ducky.png

Your program might respond with something like:

........--**####**::::::::::::........
......--##oooooo\\**::::::::..........
......**oo==oooo\\\\::::..............
....--\\oooooooo\\$$**::..............
....**&&$$oooo\\$$$$%%................
....**\\oooo\\<<<<$$##................
....!!<<<<<<<<<<\\\\##..........::::::
....::%%<<<<<<<<\\$$^^....::**++**....
....^^%%<<<<<<<<$$$$##**""==++++==****
....**oo<<<<<<$$<<<<\\oo==++++++==""""
..!!==oo<<<<<<<<$$$$oo====++++==oo####
^^====oo\\<<$$\\oooooo==++======oo****
**++++====oooo====++++======oooo\\^^^^
++====++++========++======oooooo**....
""====++++==========oooooooooo##--....
""==================oooooooooo^^......
!!oo==oo======oooooooooo\\##^^::......
..""oooooooooooooooo\\oo""^^..........
..::**====oooooooooo==**::............
....::^^**""""""""!!..--::............
....::^^**""""""""!!..--::............

Go for as much accuracy as you can possibly squeeze out of it.


 
Reply With Quote
 
 
 
 
Jim Freeze
Guest
Posts: n/a
 
      10-07-2005
Ahh, a half-toning algorithm.

On 10/7/05, Ruby Quiz <(E-Mail Removed)> wrote:
> The three rules of Ruby Quiz:
>
> 1. Please do not post any solutions or spoiler discussion for this quiz =

until
> 48 hours have passed from the time on this message.
>
> 2. Support Ruby Quiz by submitting ideas as often as you can:
>
> http://www.rubyquiz.com/
>
> 3. Enjoy!
>
> -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=

=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D=
-=3D-=3D-=3D
>
> This week's Ruby Quiz is to create a tool that will allow you to preview =

an
> image file at the command-line in pure text.
>
> Your program will need to read in an image, in whatever format you want t=

o
> support, and respond with a text representation of the image that fits in=

the
> terminal.
>
> For example, given the image:
>
> http://rubyquiz.com/images/Ducky.png
>
> Your program might respond with something like:
>
> ........--**####**::::::::::::........
> ......--##oooooo\\**::::::::..........
> ......**oo=3D=3Doooo\\\\::::..............
> ....--\\oooooooo\\$$**::..............
> ....**&&$$oooo\\$$$$%%................
> ....**\\oooo\\<<<<$$##................
> ....!!<<<<<<<<<<\\\\##..........::::::
> ....::%%<<<<<<<<\\$$^^....::**++**....
> ....^^%%<<<<<<<<$$$$##**""=3D=3D++++=3D=3D****
> ....**oo<<<<<<$$<<<<\\oo=3D=3D++++++=3D=3D""""
> ..!!=3D=3Doo<<<<<<<<$$$$oo=3D=3D=3D=3D++++=3D=3Doo ####
> ^^=3D=3D=3D=3Doo\\<<$$\\oooooo=3D=3D++=3D=3D=3D=3D =3D=3Doo****
> **++++=3D=3D=3D=3Doooo=3D=3D=3D=3D++++=3D=3D=3D=3D =3D=3Doooo\\^^^=

^
> ++=3D=3D=3D=3D++++=3D=3D=3D=3D=3D=3D=3D=3D++=3D=3D =3D=3D=3D=3Dooo=

ooo**....
> ""=3D=3D=3D=3D++++=3D=3D=3D=3D=3D=3D=3D=3D=3D=3Doo oooooooo##--...=

 
Reply With Quote
 
 
 
 
Hal Fulton
Guest
Posts: n/a
 
      10-07-2005
Ruby Quiz wrote:
> This week's Ruby Quiz is to create a tool that will allow you to preview an
> image file at the command-line in pure text.
>
> Your program will need to read in an image, in whatever format you want to
> support, and respond with a text representation of the image that fits in the
> terminal.


I never participate in these, but this one is tempting.

What about ANSI color?


Hal



 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      10-08-2005
On Oct 7, 2005, at 6:24 PM, Hal Fulton wrote:

> I never participate in these, but this one is tempting.


Ah, why not? We do try to have a little fun.

Perhaps that means you need to send in a problem *you* can't resist!

> What about ANSI color?


Very clever. I didn't think of that...

James Edward Gray II


 
Reply With Quote
 
James Britt
Guest
Posts: n/a
 
      10-09-2005
Hal Fulton wrote:
> Ruby Quiz wrote:
>
>> This week's Ruby Quiz is to create a tool that will allow you to
>> preview an
>> image file at the command-line in pure text.
>>
>> Your program will need to read in an image, in whatever format you
>> want to
>> support, and respond with a text representation of the image that fits
>> in the
>> terminal.

>
>
> I never participate in these, but this one is tempting.


Might even make a few bucks, no?

http://www.artima.com/forums/flat.js...&thread=130735

James

--

http://www.ruby-doc.org - The Ruby Documentation Site
http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys


 
Reply With Quote
 
Kero
Guest
Posts: n/a
 
      10-09-2005
>> I never participate in these, but this one is tempting.
>
> Might even make a few bucks, no?
>
> http://www.artima.com/forums/flat.js...&thread=130735


do all contributors get a free sample?
*grin*

+--- Kero ------------------------- kero@chello@nl ---+
| all the meaningless and empty words I spoke |
| Promises -- The Cranberries |
+--- M38c --- http://members.chello.nl/k.vangelder ---+
 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      10-09-2005
On Oct 9, 2005, at 7:26 AM, Kero wrote:

>>> I never participate in these, but this one is tempting.
>>>

>>
>> Might even make a few bucks, no?
>>
>> http://www.artima.com/forums/flat.js...&thread=130735
>>

>
> do all contributors get a free sample?


Contributors of quizzes and code used in the book will be contacted
very soon now...

James Edward Gray II



 
Reply With Quote
 
Simon Kröger
Guest
Posts: n/a
 
      10-09-2005
Hi,

here is my attempt of solving this weeks quiz:

This solution uses the RMagick lib to read the image and
to find edges because trying to find apropiate letters for
halftoning is hard on all the different terminals.

I had another version without the generator/enumerator
stuff and it was a lot faster. But speed isn't an issue this
week and it's so much smoother than fiddling with indexes.

cheers

Simon

----------------------------------------------------------------
require 'RMagick'
require 'generator'
require 'enumerator'

puts "Usage: #{$0} <img> [size]" or exit if !ARGV[0]

img, size = Magick::ImageList.new(ARGV[0]), (ARGV[1]||40).to_f
factor = [size*1.5/img.rows, size/img.columns].min

img.resize!(img.columns*factor, 2*(img.rows*factor*0.75).round)
img = img.edge.despeckle.despeckle.normalize.threshold(5 0)

pixels = img.get_pixels(0, 0, img.columns, img.rows).map{|c| c.red.zero?}

pixels.to_enum(:each_slice, img.columns).each_slice(2) do |l|
puts SyncEnumerator.new(*l).map{|p1, p2|
[' ', "'", ".", ":"] [(p1 ? 0 : 1) + (p2 ? 0 : 2)]}.join('')
end
----------------------------------------------------------------

sample output:

.:'''''''.
.: :.
.: :.
: :
:' ':
.: :
: .':. ::
:'.' : .. ':
: :..' :' ': :
:. ' : .: :
': '::'':.. ':' . :
:: ''.. :' .:'':.
:: . ': .: ..' . '.
': ''''' .: .:::::''' :.
:: .:': :
.: : .:' :
.' '' '' :
:' :
: :
' :
: ' :'
: .:
' : .:
' :
: .:
: .:
: .'
'. :'
:. .'
'. .:'
':. .:'
':. .:'
''..........''


 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      10-09-2005
On Oct 9, 2005, at 4:56 PM, Simon Kr=F6ger wrote:

> Hi,
>
> here is my attempt of solving this weeks quiz:


[snip]

> sample output:
>
> .:'''''''.
> .: :.
> .: :.
> : :
> :' ':
> .: :
> : .':. ::
> :'.' : .. ':
> : :..' :' ': :
> :. ' : .: :
> ': '::'':.. ':' . :
> :: ''.. :' .:'':.
> :: . ': .: ..' . '.
> ': ''''' .: .:::::''' :.
> :: .:': :
> .: : .:' :
> .' '' '' :
> :' :
> : :
> .' :
> : ' :'
> : .:
> ' : .:
> ' :
> : .:
> : .:
> : .'
> '. :'
> :. .'
> '. .:'
> ':. .:'
> ':. .:'
> ''..........''


Wow, I really like that! Too cool.

Here's the code used in the quiz:

#!/usr/local/bin/ruby -w

unless ARGV.size =3D=3D 1 and File.exists? ARGV.first
puts "Usage: #{File.basename($0)} IMAGE_FILE"
exit
end

require "RMagick"

text =3D %w{. : - ^ ! * + " =3D % o # \\ $ < &}

image =3D Magick::Image.read(ARGV.shift).first

image =3D image.quantize(text.size)
image.scale!([39.0 / image.columns, 20.0 / image.rows].min)
image =3D image.quantize(text.size)

pixels =3D Array.new
0.upto(image.rows) do |y|
0.upto(image.columns) do |x|
pixel =3D image.pixel_color(x, y).intensity
pixels << pixel unless pixels.include? pixel
end
end
pixels.sort! { |a, b| b <=3D> a }

0.upto(image.rows) do |y|
0.upto(image.columns) do |x|
2.times { print text[pixels.index(image.pixel_color(x, =20
y).intensity)] }
end
puts
end

__END__

James Edward Gray II



 
Reply With Quote
 
Timothy Hunter
Guest
Posts: n/a
 
      10-09-2005
My solution relies on having RMagick do all the work. Here's the output
for the test image:

+-------------------------------------------------------------------------+
| .::+====+:: |
| .:==o======ooo*oo+. |
| +oo=====++++===oo***+ |
| o==++++===++==ooooo*^*. |
| o====++++====oooooooo*^* |
| .oo=====++++========ooo**^^o |
| .ooo=====++++++======ooo**^^%: |
| :*oo*o==============oooo****^^^. |
| oo$WW^===========oooo****^^^^^= |
| ooo*WWW%=========oooo^^*^**^^^^^^ |
| oooo=o:=====oooo*%WWW$^^*^^^^*^: |
| =ooo=:+====ooooo**o^WWW$***^****^: |
| .*o*o*ooooo==ooo*^*+====o*^*****^. |
| %$$$$$WW$$$%%^^^^**********^^= |
| .oWWWWWWWWWWWWWW$%%^**o****^^= :::::: |
| %$$$$$$$W$$W$WWWW$^****^^o. ...::::::::==: |
| =WWWW$$$W$$$$WW$$%^^****+. .........:::::::::::::::::=o+ |
| :$WWWWWWWWWWWW%^****^%%%%%^^**oo====++:::::::::::: :::+=o+ |
| :+oo*$$WWWWWWW$%^*^^^^^%$$$%%%^**o===+:::::::::::: :::::++==o: |
| .:====o*^%$$$$$%^^%%^^^^%%%%%%%^^**o===+:::::::::: ::::::::===oo |
| .:+++++==o*^%%%%%%$%%%%^^^^%%%^^^**oo===++:::::::: :::::::::+==ooo |
| .:+::::+++=oo*^%%%$%%%%%^^^^^*****ooo===+++::::::: :::::::::++===oo= |
| :++::::::+++=oo*^%%%%%%^^****ooooo=====++::::::::: ::::++:::++====o*. |
| :+::::::::::+===o**^^^^**ooo=====+=+++++:::::::::: ::+::==++++===ooo+ |
| :+::::::::::::+++==oooooo====+::::+:::::+::::+::++ ::+::+==++=====oo= |
| .+::::::::::::::::++=+=====+::::::+:::::+::::+:::+ :::+++========ooo= |
| :+::::::::::::::::::+:++++::::::++::::::::::+:::++ :++==========ooo+ |
|.++++:::::::::::::::::::::::::::+:::::+::::+:::++ ++===========oooo: |
|.++++::::::::::::::::::::::::::::::++:+::+=+++=== ===========oooo=. |
| +++++++::::::::::::::::::::::++::++::+==+=+======= ====o===ooo=: |
| :==++++::::+:::::::::+:+:::::::::+++===++========= ====ooooo=: |
| .===++++++++++::+++::+++:+++++::+===++====o======= ===o=o==:. |
| :=======+++++++++++++++++:++===============oo====o o=o==:. |
| :====================++++++==+=========o=====ooo== ==: |
| .==================================ooooooo=oo===+: |
| :+=============================oooo=o==oo==::. |
| ::==========================o====oo===::. |
| ::++==============+===========+::. |
| ..:::++++==++=+++++++++::.. |
| ....:.::::::... |
+-------------------------------------------------------------------------+


require 'RMagick'

CHARS = ['W', 'M', '$', '@', '#', '%', '^', 'x', '*', 'o', '=', '+',
':', '~', '.', ' ']
FONT_ROWS = 8
FONT_COLS = 4

img = Magick::Image.read(ARGV[0] || "Flower_Hat.jpg").first

# Resize too-large images. The resulting image is going to be
# about twice the size of the input, so if the original image is too
# large we need to make it smaller so the ASCII version won't be too
# big. The `change_geometry' method computes new dimensions for an
# image based on the geometry argument. The '320x320>' argument says
# "If the image is too big to fit in a 320x320 square, compute the
# dimensions of an image that will fit, but retain the original aspect
# ratio. If the image is already smaller than 320x320, keep the same
# dimensions."
img.change_geometry('320x320>') do |cols, rows|
img.resize!(cols, rows) if cols != img.columns || rows != img.rows
end

# Compute the image size in ASCII "pixels" and resize the image to have
# those dimensions. The resulting image does not have the same aspect
# ratio as the original, but since our "pixels" are twice as tall as
# they are wide we'll get our proportions back (roughly) when we render.
pr = img.rows / FONT_ROWS
pc = img.columns / FONT_COLS
img.resize!(pc, pr)

img = img.quantize(16, Magick::GRAYColorspace)
img = img.normalize

# Draw the image surrounded by a border. The `view' method is slow but
# it makes it easy to address individual pixels. In grayscale images,
# all three RGB channels have the same value so the red channel is as
# good as any for choosing which character to represent the intensity of
# this particular pixel.
border = '+' + ('-' * pc) + '+'
puts border
img.view(0, 0, pc, pr) do |view|
pr.times do |i|
putc '|'
pc.times { |j| putc CHARS[view[i][j].red/16] }
puts '|'
end
end
puts border

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FF bug? Or IE bug? Text doesn't wrap around image in list elementwhen image is aligned. iwasjoeking HTML 9 06-09-2008 11:43 PM
Dynamic text: modify text by image/link click David Housman Javascript 1 02-02-2007 10:41 AM
Controlling text in a Text Area or Text leo ASP General 1 12-05-2005 01:13 AM
wx.Image: Couldn't add an image to the image list. Laszlo Zsolt Nagy Python 1 01-26-2005 09:55 PM
replace instances of text on page with image or other text? juglesh Javascript 16 12-27-2004 04:14 AM



Advertisments