Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Text Image (#50)
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 61 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Ruby Quiz  
View profile  
 More options Oct 7 2005, 9:01 am
Newsgroups: comp.lang.ruby
From: Ruby Quiz <ja...@grayproductions.net>
Date: Fri, 7 Oct 2005 22:01:55 +0900
Local: Fri, Oct 7 2005 9:01 am
Subject: [QUIZ] Text Image (#50)
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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jim Freeze  
View profile  
 More options Oct 7 2005, 9:52 am
Newsgroups: comp.lang.ruby
From: Jim Freeze <j...@freeze.org>
Date: Fri, 7 Oct 2005 22:52:39 +0900
Local: Fri, Oct 7 2005 9:52 am
Subject: Re: [QUIZ] Text Image (#50)
Ahh, a half-toning algorithm.

On 10/7/05, Ruby Quiz <ja...@grayproductions.net> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hal Fulton  
View profile  
 More options Oct 7 2005, 7:24 pm
Newsgroups: comp.lang.ruby
From: Hal Fulton <hal9...@hypermetrics.com>
Date: Sat, 8 Oct 2005 08:24:35 +0900
Local: Fri, Oct 7 2005 7:24 pm
Subject: Re: [QUIZ] Text Image (#50)

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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Edward Gray II  
View profile  
 More options Oct 7 2005, 8:35 pm
Newsgroups: comp.lang.ruby
From: James Edward Gray II <ja...@grayproductions.net>
Date: Sat, 8 Oct 2005 09:35:29 +0900
Local: Fri, Oct 7 2005 8:35 pm
Subject: Re: [QUIZ] Text Image (#50)
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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Britt  
View profile  
 More options Oct 8 2005, 8:06 pm
Newsgroups: comp.lang.ruby
From: James Britt <jame...@neurogami.com>
Date: Sun, 9 Oct 2005 09:06:44 +0900
Local: Sat, Oct 8 2005 8:06 pm
Subject: Re: [QUIZ] Text Image (#50)

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.jsp?forum=123&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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kero  
View profile  
 More options Oct 9 2005, 8:24 am
Newsgroups: comp.lang.ruby
From: Kero <k...@chello.single-dot.nl>
Date: Sun, 09 Oct 2005 12:24:34 GMT
Local: Sun, Oct 9 2005 8:24 am
Subject: Re: [QUIZ] Text Image (#50)

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

> Might even make a few bucks, no?

> http://www.artima.com/forums/flat.jsp?forum=123&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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christian Neukirchen  
View profile  
 More options Oct 9 2005, 11:54 am
Newsgroups: comp.lang.ruby
From: Christian Neukirchen <chneukirc...@gmail.com>
Date: Mon, 10 Oct 2005 00:54:00 +0900
Subject: Re: [QUIZ] Text Image (#50)

Kero <k...@chello.single-dot.nl> writes:
>>> I never participate in these, but this one is tempting.

>> Might even make a few bucks, no?

>> http://www.artima.com/forums/flat.jsp?forum=123&thread=130735

> do all contributors get a free sample?
> *grin*

+1 :D

> +--- Kero ------------------------- kero@chello@nl ---+

--
Christian Neukirchen  <chneukirc...@gmail.com>  http://chneukirchen.org

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Edward Gray II  
View profile  
 More options Oct 9 2005, 12:31 pm
Newsgroups: comp.lang.ruby
From: James Edward Gray II <ja...@grayproductions.net>
Date: Mon, 10 Oct 2005 01:31:10 +0900
Local: Sun, Oct 9 2005 12:31 pm
Subject: Re: [QUIZ] Text Image (#50)
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.jsp?forum=123&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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Simon Kröger  
View profile  
 More options Oct 9 2005, 5:56 pm
Newsgroups: comp.lang.ruby
From: Simon Kröger <SimonKroe...@gmx.de>
Date: Mon, 10 Oct 2005 06:56:38 +0900
Local: Sun, Oct 9 2005 5:56 pm
Subject: [SOLUTION] Text Image (#50)
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(50)

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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Edward Gray II  
View profile  
 More options Oct 9 2005, 6:06 pm
Newsgroups: comp.lang.ruby
From: James Edward Gray II <ja...@grayproductions.net>
Date: Mon, 10 Oct 2005 07:06:49 +0900
Local: Sun, Oct 9 2005 6:06 pm
Subject: Re: [SOLUTION] Text Image (#50)
On Oct 9, 2005, at 4:56 PM, Simon Kröger wrote:

> Hi,

> here is my attempt of solving this weeks quiz:

[snip]

Wow, I really like that!  Too cool.

Here's the code used in the quiz:

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

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

require "RMagick"

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

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

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

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

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

__END__

James Edward Gray II


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Timothy Hunter  
View profile  
 More options Oct 9 2005, 6:18 pm
Newsgroups: comp.lang.ruby
From: Timothy Hunter <cycli...@nc.rr.com>
Date: Sun, 09 Oct 2005 22:18:40 GMT
Local: Sun, Oct 9 2005 6:18 pm
Subject: [SOLUTION] Text Image (#50)
My solution relies on having RMagick do all the work. Here's the output
for the test image:

+-------------------------------------------------------------------------+
|                      .::+====+::                                        |
|                  .:==o======ooo*oo+.                                    |
|                 +oo=====++++===oo***+                                   |
|               :oo==++++===++==ooooo*^*.                                 |
|              :oo====++++====oooooooo*^*                                 |
|             .oo=====++++========ooo**^^o                                |
|            .ooo=====++++++======ooo**^^%:                               |
|           :*oo*o==============oooo****^^^.                              |
|          :ooo$WW^===========oooo****^^^^^=                              |
|          ooo*WWW%=========oooo^^*^**^^^^^^                              |
|          oooo=o::o=====oooo*%WWW$^^*^^^^*^:                             |
|          =ooo=:+====ooooo**o^WWW$***^****^:                             |
|          .*o*o*ooooo==ooo*^*+====o*^*****^.                             |
|           :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====oo=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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ryan Leavengood  
View profile  
 More options Oct 9 2005, 7:57 pm
Newsgroups: comp.lang.ruby
From: Ryan Leavengood <leaveng...@gmail.com>
Date: Mon, 10 Oct 2005 08:57:01 +0900
Local: Sun, Oct 9 2005 7:57 pm
Subject: Re: [SOLUTION] Text Image (#50)
I'm surprised how easy this Quiz was because of ImageMagick and
RMagick (based on the other submissions, I didn't try it.) Kudos to
the developers of those projects (though I imagine only the RMagick
developer will be reading this.)

Of course you quiz developers deserve kudos for your clever solutions
using RMagick.

I wonder how long the code would be for a solution that did NOT use
RMagick or any other image library? Maybe future submissions will
answer this question for me...

Ryan


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Edward Gray II  
View profile  
 More options Oct 9 2005, 8:45 pm
Newsgroups: comp.lang.ruby
From: James Edward Gray II <ja...@grayproductions.net>
Date: Mon, 10 Oct 2005 09:45:07 +0900
Local: Sun, Oct 9 2005 8:45 pm
Subject: Re: [SOLUTION] Text Image (#50)
On Oct 9, 2005, at 6:57 PM, Ryan Leavengood wrote:

> I'm surprised how easy this Quiz was because of ImageMagick and
> RMagick (based on the other submissions, I didn't try it.)

Indeed.  I only found RMagick two weeks ago, but it impressed me  
enough to make the quiz.  It's a bit of a bear to get installed  
(because of ImageMagick, not the Ruby bindings), but worth the effort.

I second the thanks to the developer for giving us such great tools  
to work with!

James Edward Gray II


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Burt  
View profile  
 More options Oct 10 2005, 4:41 am
Newsgroups: comp.lang.ruby
From: "Dave Burt" <d...@burt.id.au>
Date: Mon, 10 Oct 2005 08:41:48 GMT
Local: Mon, Oct 10 2005 4:41 am
Subject: Re: [QUIZ] Text Image (#50)
Hi,

I have a solution written five years ago by Brian Fundakowski Feldman. Is
that allowed?

This code, which is not mine, uses the GD library.

This is not my code:

http://www.dave.burt.id.au/ruby/img2ascii.rb

Cheers,
Dave


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Simon Kröger  
View profile  
 More options Oct 10 2005, 5:02 pm
Newsgroups: comp.lang.ruby
From: Simon Kröger <SimonKroe...@gmx.de>
Date: Tue, 11 Oct 2005 06:02:45 +0900
Local: Mon, Oct 10 2005 5:02 pm
Subject: Re: [SOLUTION] Text Image (#50)
James Edward Gray II wrote:

> Wow, I really like that!  Too cool.

Thanks, yes it turned out better than expected.

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

Unfortunately my console has another aspect ratio than most mail readers
(more like 1.5/1 than 2/1) so i had to tweak it a bit for the image above.

the original is here:
http://perso.wanadoo.fr/psylo-vision/_images/wallpapers/_first/Tomb-R...
(i made no other optimisation than choosing a suitable region)

cheers

Simon


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rob Rypka  
View profile  
 More options Oct 10 2005, 7:01 pm
Newsgroups: comp.lang.ruby
From: Rob Rypka <rascal1...@gmail.com>
Date: Tue, 11 Oct 2005 08:01:11 +0900
Local: Mon, Oct 10 2005 7:01 pm
Subject: Re: [SOLUTION] Text Image (#50)
I tried this one out.  I'm not sure if I was successful or not...

My first instinct was to rank characters for intensity levels
programmaticly.  I then came to the realization that not all
characters have the same distribution of intensity, so I thought I
would try splitting them up, and matching them with sets of pixel
intensities (instead of just one).

ruby create_tables.rb num [extended]
does the first part - pass it a number (number of pixels sqare that
will be replaced by one character).  uses extended ASCII if the second
argument is applied.

ruby i2a.rb src_img outfile num [normalized]
does the second part.  takes num x num clumps of pixels from src_image
and finds the closest character from the appropriate table (by
Euclidean distance), puts it out to outfile.  If normalized is
supplied, the image is normalized for maximum contrast in the source
image.

You can find the code here:
http://www.rabble-rooster.org/rubyquiz/50/

Results are in the results folder.

The underscore also rendered to pure white, so I had to remove it
manully from the YAML tables.  Those tables are available also.

I know this code needs some refactoring/reengineering, and I didn't
take the time to look into how *Magick did things like scaling.  Feel
free to chide me on any points that are inefficient/bad/wrong/stupid.

--
Rob


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "[Solution] Text Image (#50)" by Brian Schröder
Brian Schröder  
View profile  
 More options Oct 10 2005, 9:35 pm
Newsgroups: comp.lang.ruby
From: Brian Schröder <ruby.br...@gmail.com>
Date: Tue, 11 Oct 2005 10:35:53 +0900
Local: Mon, Oct 10 2005 9:35 pm
Subject: [QUIZ] [Solution] Text Image (#50)
Hello Everyone,

I went into this quiz using the following approach. I split the image
into pieces and find for each piece of the image the letter most
similar to this piece. I did it all with image magick. There is room
for improvement in the distance function, but it works ok. I'm sure it
is the slowest solution so far, but it works.

You can find the sourcecode and examples at

http://ruby.brian-schroeder.de/quiz/asciiview/

best regards,

Brian

                        _,,uuuuu,,_
                    _u:EEEEEEEEEE{{[p,_
                  _:EEEEEEEEEEEEEEE{{[Ou
                 :EEEEEEEEEEEEEEEE{{{{[O3_
                UEEEEEEEEEEEEEEEE{{E{{{[O3_
               UEEEEEEEEEEEEEEEEEEEEE{{[S3B
              ;{EEEEEEEEEEEEEEEEEEEE{{[SO3B$
             ;{{{EEEEEEEEEEEEEEEEEE{{[[O3333u
            {[[SQQ{EEEEEEEEEEEEEEE{{[[OO3333B
           E{{[###${EEEEEEEEEEEE{{[SSSS333333q
           E{{{###PEEEEEEEEEEE{SSQQ3333O333333
           {{{{ " LEEEEEEEE{{[S$####$333333333
           {{{{{:cEEEEEE{{{[[SZZ####Z3O3333OS3
           F{{{{{{EEjjEEE{[SO35EEEFEEOO3OOOO33
            F{]###########QQ3993SSSOSOO[[OS33~
             "f################$Q3O[[[[OSS33~                    L-F"FL
               I#IIII#############Q[SSOO33P"                 L--"     FE.
               P######$##IIII####T3SS333P'            LL---F          -EEU
                ##############T3OOO3OOQ$$$9SSUUUUEEEEEEF-             -EEEE
             .;{[I##########TS3333S3$IIII$$B3O[{EEEEEFF               -EEEE{
           LEEEE{O#$I#####@BBB33333$$$II]$$B3O[{EEEEEF-               -EEEE{L
         LEEEEEEE[O3$$$$$$$$$$$$9BB$$$$$$@B3O[{EEEEEE--             --EEEEE{{
       LEEEEEEEEEE[3B$$]I]]$$$$$BBBBBBB333O[[{EEEEEEF--          ---EEEEEEE{{
     LEFFFFFFEEEEE{[3B$$]]$$$$B333OOO[[[{{{EEEEEEEEFF         .EE--EEEEEEEE{{"
    -FFF-- --FEEEEE{[3B@@@@@B33O[[{{EEEEEEEEEEEFFF     L   E .LEEEEEEEEEEEE{{
   -FFFF-    -FFEEEEE{[O22OOO[[{EEEEEEEEEFFFEEF  ---  LE  -F .EEEEEEEEEEEE{{
  LEF---      --FFEEEEEEEEEEEEEEEFFF-FEF    EE   LE  LE" -E EEEEEEEEEEEE{{"
  EEE---       --FFFEEEEEEEEEEEFF   LEF    LE   LE  -EE LEEEEEEEEEEEEEE{{"
 EEEEEF-          ---FFEEEFEEFF   -EE"   -EF   LEFL-EEEEEEEEEEEEEEEEEE{E"
 EEEE-F---         --F-FFFFFF    -EF   LLE  LLEFEEEEEEEEEEEEEEEEEEEE{{E
 EEEEEE---------     --- ---    LE   LLEFFLEEEEEEEEEEEEEEEEEEEEEEE{{EF
 EEEEEEEEE--F------ - -L--LLLLLEEELLEEE-LEEEEEEEEEEEEEEEEEEEEEEEE{EE"
 FEEEEEEEEEEEE-----L-LLEEEEFF" .""FF"LEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
  .EEEEEEEEEEEEEEEEEEEEEEEEEELLLEELLLEEEEEEEEEEEEEEEEEEEEEEEEEEEEF
  .FEEEEEEEEEEEEEEEEEEEEEEEEEEEFEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEF
   FEEEEEEEEEEEEEEEEEEEEEEEELLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEF
    FEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
     "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE{{E{EEEEEEEEEF"
       "FEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE{EEEEEEF"
          FFEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEFF"
             "FFEEEEEEEEEEEEEEEEEEEEEEEEEEEFFF"
                  "FFFFFFEEEEEEEEEEEEEEFF"
                             FF"""""

                     _yv*/^^^^^^^/#Yy_
                   y#^              '^#y
                 y#                    ^#,
               _#^                       Ty
              _#                          #y
              #               ,-           #_
             #^                             #
           _#'                              ^g
          y#  _Y/Xy                          #
          #   #   Tg                         ^$
          #   #,^ y^            y**v_         #
          $    ###^            # _. #         #
          #     ^             _$_'_,#         #
          #y _yyyyyyv*gyy_    ^###IIP         #
           #y#$        ^ ^P*yy_ ''           yf                   ,yvy_
            ^#$               ^P*y_         y#                _y#^^  ,^#y
             '# ^P/*+ ,_         _T*      _#^           __yv*P^      '  ^#_
              Ty~/**^^M ^P**#^v/^y#     _g#yyyyyyyyvY*#P^^                #y
              y#      '^rr**~'y#^^  _vP^'                                  #_
           y#P^ #P_         y#^     T                                     Y #
         _#^    ^#C/xvv-_yYP'                                               ^$
       y#^        ^^Pr/~^'                                                   #
     _#^                                                                     #
    g^                                                                      yT
   #^                                                       #               #
  #                                                     F   ^              $^
 #'                                    _P     @    #   g   #              y^
y^                                    ,^     /    g    ^                 y^
#                                    v                                  y^
#                                  _/     y    _                       #^
#                                 Y^    _.    ,                      ,#'
#                                      r^    _^                     g^
#                             r/   Yr      *^   _x       '        y#
^y                                     _       *'   _-          y#^
 #                            __              r               _#^
  #                          '^      '      '               y#^
  ^#                                     -                y#^
    #y                                              _  _YP'
     ^#,                                       r    _y#^
       ^#y                                r      _v#^
          ^#y_                -x     `~ -r   _y*P^
             ^/*y_               ~/***r__yY/^^
                 ^^P**YvyyyyyyyyyvvY*/P^^
--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rob Rypka  
View profile  
 More options Oct 11 2005, 10:40 am
Newsgroups: comp.lang.ruby
From: Rob Rypka <rascal1...@gmail.com>
Date: Tue, 11 Oct 2005 23:40:08 +0900
Local: Tues, Oct 11 2005 10:40 am
Subject: Re: [QUIZ] [Solution] Text Image (#50)
On 10/10/05, Brian Schröder <ruby.br...@gmail.com> wrote:

> I went into this quiz using the following approach. I split the image
> into pieces and find for each piece of the image the letter most
> similar to this piece. I did it all with image magick. There is room
> for improvement in the distance function, but it works ok. I'm sure it
> is the slowest solution so far, but it works.

> You can find the sourcecode and examples at

> http://ruby.brian-schroeder.de/quiz/asciiview/

I do believe I got pWn3d!!1!...  Nice work, Brian.

I think my solution is probably slower, though :-).  I'm too used to
doing this kind of thing in C/++.

--
Rob


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brian Schröder  
View profile  
 More options Oct 11 2005, 2:03 pm
Newsgroups: comp.lang.ruby
From: Brian Schröder <ruby.br...@gmail.com>
Date: Wed, 12 Oct 2005 03:03:18 +0900
Local: Tues, Oct 11 2005 2:03 pm
Subject: Re: [QUIZ] [Solution] Text Image (#50)

> [snip]

> > You can find the sourcecode and examples at

> > http://ruby.brian-schroeder.de/quiz/asciiview/

> I do believe I got pWn3d!!1!...  Nice work, Brian.

Thank you, rob. Though I can't decipher sms-code, though I hope it's
good for me ;-)

greetings,

Brian

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "unfinished [SOLUTION] Text Image (#50)" by Simon Kröger
Simon Kröger  
View profile  
 More options Oct 11 2005, 5:48 pm
Newsgroups: comp.lang.ruby
From: Simon Kröger <SimonKroe...@gmx.de>
Date: Wed, 12 Oct 2005 06:48:51 +0900
Local: Tues, Oct 11 2005 5:48 pm
Subject: unfinished [SOLUTION] Text Image (#50)
Hi,

i'm completely new to ncurses, so could someone with more exprerience
(or another OS) try this and tell me whats happening?
On windows all the colors are very pale (are there consts for lighter
colors?)
-----------------------------------------------------------------------
require 'RMagick'
require "ncurses"

Ncurses.initscr
puts "Usage: #{$0} <img> [size]" or exit if !ARGV[0]
puts "Sorry, no colors!" or exit unless Ncurses.has_colors?

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

COLORS =
   [[Ncurses::COLOR_BLACK, [0, 0, 0]], [Ncurses::COLOR_RED, [1, 0, 0]],
   [Ncurses::COLOR_GREEN, [0, 1, 0]], [Ncurses::COLOR_BLUE, [0, 0, 1]],
   [Ncurses::COLOR_YELLOW, [1, 1, 0]], [Ncurses::COLOR_MAGENTA, [1, 0, 1]],
   [Ncurses::COLOR_CYAN, [0, 1, 1]], [Ncurses::COLOR_WHITE, [1, 1, 1]]]

GRADIENT = [[0, ' '], [50, ':'], [100, '|'], [150, 'I'], [200, '#']]
COLORMAP = {}

COLORS.size.times do |bg|
   COLORS.size.times do |fg|
     next if fg == bg
     i = (bg*COLORS.size) + fg
     Ncurses.init_pair(i, COLORS[fg][0], COLORS[bg][0])
     GRADIENT.each do |gr, c|
       r = COLORS[fg][1][0] * gr + COLORS[bg][1][0] * (255-gr)
       g = COLORS[fg][1][1] * gr + COLORS[bg][1][1] * (255-gr)
       b = COLORS[fg][1][2] * gr + COLORS[bg][1][2] * (255-gr)
       COLORMAP[[r, g, b]] = [i, c]
     end
   end
end

#(16*16*4).times do |i|
#  Ncurses.stdscr.attrset(Ncurses.COLOR_PAIR(i))
#  Ncurses.stdscr.mvaddstr(i/16, (i%16)*4, 'TEST')
#end
#Ncurses.refresh

pixels = img.get_pixels(0, 0, img.columns, img.rows)
img.rows.times do |y|
   img.columns.times do |x|
     p = pixels[y*img.columns + x]
     r, g, b, best, dist = p.red, p.green, p.blue, -1, 255
     COLORMAP.each do |k, v|
       d = Math.sqrt((r-k[0])**2 + (g-k[1])**2 + (b-k[2])**2)
       (best, dist = v, d) if d < dist
     end
     Ncurses.stdscr.attrset(Ncurses.COLOR_PAIR(best[0]))
     Ncurses.stdscr.mvaddstr(y, x, best[1])
   end
end

Ncurses.refresh
-----------------------------------------------------------------------

maybe someone can help..

cheers

Simon


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "[Solution] Text Image (#50)" by Harold Hausman
Harold Hausman  
View profile  
 More options Oct 11 2005, 9:23 pm
Newsgroups: comp.lang.ruby
From: Harold Hausman <hhaus...@gmail.com>
Date: Wed, 12 Oct 2005 10:23:09 +0900
Local: Tues, Oct 11 2005 9:23 pm
Subject: Re: [QUIZ] [Solution] Text Image (#50)

Hi,

Best quiz yet. Er, well, quiz which held my attention longest anyway. :)

My solution is um, less... elegant, than some of the others, but in a lot of
ways, like a parent who mistakenly thinks their ugly child is cute I kinda
like it. I don't really share my code a lot (maybe for obvious reasons) but
I'm just so pleased with how easily ruby lets me hack things to high heaven
and this one in particular makes me smile. Take special note that it only
works on 24 bit .bmp files and put your hard hat on if you think you're
going to feed anything but that into it. ;) Okay, enough blather, without
further ado, the first code I've shared with you guys:

#It only supports 24bit bmp files, and it even chokes on most of them ;)
#It creates one character per pixel (which has obvious implications)
#But it's 40 lines of pure ruby no lib use binary file up-hackery...
#And quite frankly, thats what I do.

MyPixel = Struct.new( 'MyPixel', :r, :g, :b )

the_gradient = %w|D Y 8 S 6 5 J j t c + i ! ; : .|
###############PUT YOUR FILENAME
HERE#########################################
the_file = File.new('ducky.bmp', 'rb')
the_file.read(2) #BM
the_file.read(4).unpack('V')[0] #filesize
the_file.read(4) #unused
the_file.read(4).unpack('V')[0] #offset from beginning to bitmap data
the_file.read(4).unpack('V')[0] #size of bitmap header
image_x = the_file.read(4).unpack('V')[0] #width x in pixels
image_y = the_file.read(4).unpack('V')[0] #height y in pixels
the_file.read(2).unpack('v')[0] #planes?
the_file.read(2).unpack('v')[0] #bits per pixel
the_file.read(24) #unused

the_bitmap = []
puts "CRRRRUNCHHHH --- please wait, reading file..."
image_y.times do |row|
the_bitmap[row] = []
image_x.times do |col|
the_bitmap[row][col] = MyPixel.new( 0, 0, 0 )
the_bitmap[row][col].b = the_file.read(1).unpack('c')[0]
the_bitmap[row][col].g = the_file.read(1).unpack('c')[0]
the_bitmap[row][col].r = the_file.read(1).unpack('c')[0]
end
end

puts "output coming:"
the_output = File.new('output.asciiart', 'w')
(image_y-1).downto(0) do |row|
image_x.times do |col|
the_avg =
(the_bitmap[row][col].b+the_bitmap[row][col].g+the_bitmap[row][col].r)/3
the_output.write(the_gradient[the_avg>>4])
end
the_output.write("\n")
end


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Burt  
View profile  
 More options Oct 11 2005, 9:59 pm
Newsgroups: comp.lang.ruby
From: "Dave Burt" <d...@burt.id.au>
Date: Wed, 12 Oct 2005 01:59:01 GMT
Local: Tues, Oct 11 2005 9:59 pm
Subject: Re: [QUIZ] [Solution] Text Image (#50)
Harold Hausman:
#It only supports 24bit bmp files, and it even chokes on most of them ;)

Thanks for submitting this - well done, Harold.

(If I was going to solve this quiz, I'd have done a BMP-reader.)

Cheers,
Dave


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
daz  
View profile  
 More options Oct 12 2005, 8:03 am
Newsgroups: comp.lang.ruby
From: "daz" <do...@d10.karoo.co.uk>
Date: Wed, 12 Oct 2005 13:03:58 +0100
Local: Wed, Oct 12 2005 8:03 am
Subject: Re: [QUIZ] [Solution] Text Image (#50)

Harold Hausman wrote:
> Hi,

> [...] the first code I've shared with you guys:

Hi Harold - and thanks for doing that.

> #It creates one character per pixel (which has obvious implications)

Not obvious to me until I tried it.
That's a fair-sized hunk of duck ;-))

> #But it's 40 lines of pure ruby no lib use binary file up-hackery...

Excellent.
I refactored a bit:

#------------------------------------------------------------------------
Gradient = %w{ D Y 8 S 6 5 J j t c + i ! ; : . }

# http://www.d10.karoo.net/ruby/quiz/50/duck.bmp  (NOTE: 800KB  BMP)
bmp = File.open('duck.bmp', 'rb') { |fi| fi.read }
bmo = bmp[10, 4].unpack('V')[0]             # offset to bitmap data
image_x, image_y = bmp[18, 8].unpack('VV')  # width x / height y (pixels)
by_start = bmo + ((image_y - 1) * (image_x * 3))

File.open('output.txt', 'w') do |fo|
  by_start.step(bmo, -(image_x * 3)) do |by_ptr|
    image_x.times do |x|
      t = 0;  3.times {|n| t += bmp[by_ptr + (x * 3) + n] }
      fo.putc( Gradient[ (t / 3 ) >> 4 ] )
    end
    fo.puts
  end
end
#------------------------------------------------------------------------

There's one change in effect from your original and I suspect
you may have intended it differently ...

  the_file.read(1).unpack('c')[0]

... gives a signed byte so, when you take the average by adding and
dividing by 3, you can be adding negatives.  Replacing those with ('C')
gives you unsigned bytes and the overall result matches the output from
the code above.

Maybe we'll see a smaller fowl soon :-)

daz


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robbie Carlton  
View profile  
 More options Oct 12 2005, 8:39 am
Newsgroups: comp.lang.ruby
From: Robbie Carlton <robbie.carl...@gmail.com>
Date: Wed, 12 Oct 2005 21:39:21 +0900
Local: Wed, Oct 12 2005 8:39 am
Subject: Re: [QUIZ] [Solution] Text Image (#50)

thank you very very much. I've been struggling with a problem in the
graphics library I'm using for lisp (lispworks capi library) which requires
me to generate a bitmap file from an array of pixels. I couldn't decipher
any of the specs I've found for the format because they go into loads of
extraneous detail about color tables and compression which I don't need to
know about. Your code is the clearest definition of the spec I need
(assuming it's correct). So thanks

On 10/12/05, Harold Hausman <hhaus...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dominik Bathon  
View profile  
 More options Oct 12 2005, 10:18 am
Newsgroups: comp.lang.ruby
From: "Dominik Bathon" <dba...@gmx.de>
Date: Wed, 12 Oct 2005 23:18:10 +0900
Local: Wed, Oct 12 2005 10:18 am
Subject: Re: [QUIZ] [Solution] Text Image (#50)

On Wed, 12 Oct 2005 14:07:08 +0200, daz <do...@d10.karoo.co.uk> wrote:

> Harold Hausman wrote:
>> Hi,

>> [...] the first code I've shared with you guys:

> Hi Harold - and thanks for doing that.

Yes, a really nice solution.

> ...
> Maybe we'll see a smaller fowl soon :-)

I am not sure if you meant golfing by "a smaller fowl", but anyway here  
are my attempts:

The first version version is just a refactoring like yours:

GRADIENT = %w|D Y 8 S 6 5 J j t c + i ! ; : .|
file = File.new(ARGV.shift || "ducky.bmp", "rb")
file.read(2+4+4+4+4) # headers
image_x, image_y = file.read(8).unpack("VV") # width / height
file.read(2+2+24) # headers

puts((0...image_y).collect do |row|
   (0...image_x).collect do |col|
     GRADIENT[(file.read(3).unpack("CCC").inject { |a,b| a+b } / 3) >> 4]
   end.join
end.reverse)

It writes to stdout and optionally uses ARGV.shift as input.

And here are two different short version, they need ARGV[0] and also write  
to stdout:

f=open$*[0],"rb";w,h=f.read(54)[18,8].unpack"VV"
puts((1..h).collect{(1..w).collect{"DY8S65Jjtc+i!;:."[f.read(3).unpack(
"CCC").inject{|a,b|a+b}/48,1]}.join}.reverse)

d=IO.read$*[0];puts  
d[54..-1].scan(/.../m).map{|s|"DY8S65Jjtc+i!;:."[s.unpack(
"CCC").inject{|a,b|a+b}/48,1]}.join.scan(/.{#{d[18,4].unpack"V"}}/).reverse

The second one might be problematic on Windows (I am not sure if IO.read  
reads binary or not).

Dominik


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 61   Newer >
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google