Forum: Ruby convert PNG to hex

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5aaa92336b530d04b32d1aa5da4280ee?d=identicon&s=25 Held Opslippers (heldopslippers)
on 2009-03-22 22:23
Hey people! ..

I have a question ... I have to convert a png to hex (#FFFFFF).
I searched now for over 2 hours but can't find anything that works (like
png library or RMagick).


Here is some sample code i made but doesn't work:

f = File.new("PNG.png")
image = f.readlines

puts image.to_hex


hope somebody can give me a kick in the right direction

thanxs!
9e2504e0b74e5384af09ce8a660afac4?d=identicon&s=25 Pascal J. Bourguignon (Guest)
on 2009-03-22 23:05
(Received via mailing list)
jeljer te Wies <jeljer@me.com> writes:

> image = f.readlines
>
> puts image.to_hex
>
>
> hope somebody can give me a kick in the right direction

http://en.wikipedia.org/wiki/Portable_Network_Graphics
http://www.libpng.org/pub/png/spec/iso/index-object.html
5aaa92336b530d04b32d1aa5da4280ee?d=identicon&s=25 Held Opslippers (heldopslippers)
on 2009-03-22 23:08
Pascal J. Bourguignon wrote:
> jeljer te Wies <jeljer@me.com> writes:
>
>> image = f.readlines
>>
>> puts image.to_hex
>>
>>
>> hope somebody can give me a kick in the right direction
>
> http://en.wikipedia.org/wiki/Portable_Network_Graphics
> http://www.libpng.org/pub/png/spec/iso/index-object.html

Wohw thanxs for the fast reply Pascal! ...
though it doesn't work
error: "undefined method `to_hex'"
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-03-22 23:10
(Received via mailing list)
jeljer te Wies wrote:

> I have a question ... I have to convert a png to hex (#FFFFFF).
> I searched now for over 2 hours but can't find anything that works (like
> png library or RMagick).

Are you asking how to convert the PNG file to one of its colors?

Use ImageMagick (or GraphicsMagick, via the command line, or MiniMagick,
or
RMagick) to convert the PNG file to the most dirt-simple raster format
possible,
such as YUV. Then read the first few bytes in each bit-plane, in that
format's
encoding, and hash them together.

There are also get-pixel commands in RMagick...
5aaa92336b530d04b32d1aa5da4280ee?d=identicon&s=25 Held Opslippers (heldopslippers)
on 2009-03-22 23:14
Phlip wrote:
> jeljer te Wies wrote:
>
>> I have a question ... I have to convert a png to hex (#FFFFFF).
>> I searched now for over 2 hours but can't find anything that works (like
>> png library or RMagick).
>
> Are you asking how to convert the PNG file to one of its colors?
>
> Use ImageMagick (or GraphicsMagick, via the command line, or MiniMagick,
> or
> RMagick) to convert the PNG file to the most dirt-simple raster format
> possible,
> such as YUV. Then read the first few bytes in each bit-plane, in that
> format's
> encoding, and hash them together.
>
> There are also get-pixel commands in RMagick...

yes I want to convert it to one of its colors ... but the image has only
two different colors (black or white) ..
It is not really about the colors but I want to know what each pixel has
for color (so black or white) ...
C5eecd44fa818c7985d4f31bc2c42ac9?d=identicon&s=25 Eric Jacoboni (Guest)
on 2009-03-22 23:35
(Received via mailing list)
jeljer te Wies <jeljer@me.com> writes:


> Here is some sample code i made but doesn't work:
>
> f = File.new("PNG.png")
> image = f.readlines
>
> puts image.to_hex


Without knowing exactly what you want to do...

1) f.readlines gives an *Array* of strings
2) to_hex doesn't exist in Ruby. You probably want sprintf "%X"
5aaa92336b530d04b32d1aa5da4280ee?d=identicon&s=25 Held Opslippers (heldopslippers)
on 2009-03-22 23:39
Eric Jacoboni wrote:
> jeljer te Wies <jeljer@me.com> writes:
>
>
>> Here is some sample code i made but doesn't work:
>>
>> f = File.new("PNG.png")
>> image = f.readlines
>>
>> puts image.to_hex
>
>
> Without knowing exactly what you want to do...
>
> 1) f.readlines gives an *Array* of strings
> 2) to_hex doesn't exist in Ruby. You probably want sprintf "%X"

well to explain why I want to do this! :P ..
here is my ehm... job discription:

The pixels in the above image are numbered 0..99 for the first row,
100..199 for the second row etc. White pixels represent ascii codes. The
ascii code for a particular white pixel is equal to the offset from the
last white pixel. For example, the first white pixel at location 65
would represent ascii code 65 ('A'), the next at location 131 would
represent ascii code (131 - 65) = 66 ('B') and so on

The text contained in the image is the answer encoded in Morse, where "a
test" would be encoded as ".- / - . ... -"

This is what I have to do.. that's why I need some kind of hex codes in
a long string or array so I can see what pixels are white and which one
are black

Thanxs for the fast reply!
5aaa92336b530d04b32d1aa5da4280ee?d=identicon&s=25 Held Opslippers (heldopslippers)
on 2009-03-22 23:46
This is what i now tried (image is ofcourse a array so I did 2 just to
test it) but
it's not working :(


f = File.new("PNG.png")
image = f.readlines

puts sprintf ("%X", image[2])
9e2504e0b74e5384af09ce8a660afac4?d=identicon&s=25 Pascal J. Bourguignon (Guest)
on 2009-03-22 23:46
(Received via mailing list)
jeljer te Wies <jeljer@me.com> writes:

>> http://en.wikipedia.org/wiki/Portable_Network_Graphics
>> http://www.libpng.org/pub/png/spec/iso/index-object.html
>
> Wohw thanxs for the fast reply Pascal! ...
> though it doesn't work
> error: "undefined method `to_hex'"

Of course.  You have to implement yourself.  You know, you start typing:

    def to_hex
      ...
    end

and fill in the dots.  That's called programming.  That's what is done
with programming languages and brains.
90a73d9875462aaa9fab2feffafbffe7?d=identicon&s=25 Ben Bleything (Guest)
on 2009-03-22 23:50
(Received via mailing list)
On Sun, Mar 22, 2009 at 3:43 PM, jeljer te Wies <jeljer@me.com> wrote:
> This is what i now tried (image is ofcourse a array so I did 2 just to
> test it) but
> it's not working :(

Of course it's not.  The image appears as an array when you view it as
an image, but it's encoded and compressed inside the PNG.

You'll need to find or craft a library that can decode the PNG format
(many exist; googling should help you here) and use it to then read
each pixel from the image.

Ben
9e2504e0b74e5384af09ce8a660afac4?d=identicon&s=25 Pascal J. Bourguignon (Guest)
on 2009-03-22 23:51
(Received via mailing list)
jeljer te Wies <jeljer@me.com> writes:

>>
> 100..199 for the second row etc. White pixels represent ascii codes. The
> are black
And what's the relationship with lines and hex?

You want to write methods named Image.width, Image.height,
Image.pixelAt(x,y), Pixel.isWhite?, Pixel.isBlack?, things like that.
5aaa92336b530d04b32d1aa5da4280ee?d=identicon&s=25 Held Opslippers (heldopslippers)
on 2009-03-22 23:55
Attachment: PNG.png (155 Bytes)
Pascal J. Bourguignon wrote:
> jeljer te Wies <jeljer@me.com> writes:
>
>>>
>> 100..199 for the second row etc. White pixels represent ascii codes. The
>> are black
> And what's the relationship with lines and hex?
>
> You want to write methods named Image.width, Image.height,
> Image.pixelAt(x,y), Pixel.isWhite?, Pixel.isBlack?, things like that.

now it is a task I would like to complete..
The only thing I want to know is "Who many black pixels are between the
white pixels".
(I have uploaded the image) ...

So what i want is:
1) get the image in my program
2) get each pixel and decide if it is white or black

thats all !... but well .. it seams it is harder then it sounds
5aaa92336b530d04b32d1aa5da4280ee?d=identicon&s=25 Held Opslippers (heldopslippers)
on 2009-03-22 23:56
Ben Bleything wrote:
> On Sun, Mar 22, 2009 at 3:43 PM, jeljer te Wies <jeljer@me.com> wrote:
>> This is what i now tried (image is ofcourse a array so I did 2 just to
>> test it) but
>> it's not working :(
>
> Of course it's not.  The image appears as an array when you view it as
> an image, but it's encoded and compressed inside the PNG.
>
> You'll need to find or craft a library that can decode the PNG format
> (many exist; googling should help you here) and use it to then read
> each pixel from the image.
>
> Ben

Ah thanxs ! ...I didn't know that !.. I just googled on :
"convert png to hex with ruby"
"convert image to hex with ruby"
"who to get each pixel from a png with ruby"
etc. etc.
what didn't give me an answer
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-03-23 00:00
(Received via mailing list)
The summary of your requirements: You have a PNG that looks like an
old-fashioned punched card, with dots in specific locations that you
must decode.

The reason behind this requirement now becomes an object of curiosity!
You will
get the best answer if you confess what it is...

jeljer te Wies wrote:

> f = File.new("PNG.png")
> image = f.readlines
>
> puts sprintf ("%X", image[2])

Your "fast reply" was just a Google hit for "PNG". Please always google
before
posting.

And read the cites! The third byte of a PNG file is _not_ the third
pixel from
the top! A PNG file is _probably_ a header with several variable-length
extents
of configurational data (how many colors, what dimensions, the author's
name,
etc.) followed by a _compressed_ block of data for the bit planes in the
image.

Use ImageMagick's convert utility to convert your file to PNM format, to
remove
the compression...
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2009-03-23 00:04
(Received via mailing list)
jeljer te Wies wrote:
>>
> ascii code for a particular white pixel is equal to the offset from the
>
> Thanxs for the fast reply!

With RMagick you can get the pixel values with #get_pixels
[http://studio.imagemagick.org/RMagick/doc/image2.h...] or
#view [http://studio.imagemagick.org/RMagick/doc/image3.html#view].
9e2504e0b74e5384af09ce8a660afac4?d=identicon&s=25 Pascal J. Bourguignon (Guest)
on 2009-03-23 00:10
(Received via mailing list)
jeljer te Wies <jeljer@me.com> writes:

>
> now it is a task I would like to complete..
> The only thing I want to know is "Who many black pixels are between the
> white pixels".
> (I have uploaded the image) ...
>
> So what i want is:
> 1) get the image in my program
> 2) get each pixel and decide if it is white or black

Well it's easy enough to do that with the methods I proposed
above. Something like:

def decode(image)
   i=0
   lastWhite=0
   bytes=[]
   (0..(image.height)).each {| y |
      (0..(image.width)).each {| x |
         if image.pixelAt(x,y).isWhite? then
           bytes.push(i-lastWhite)
           lastWhite=i
         end
         i=i+1
      }
   }
   bytes
end

(That's the reason why I proposed those methods).


Now you only have to implement them, doing the same: whish for methods
that would make implement the method pixelAt(x,y) easy, and do it.
Then further implement the missing methods.
5aaa92336b530d04b32d1aa5da4280ee?d=identicon&s=25 Held Opslippers (heldopslippers)
on 2009-03-23 00:12
Phlip wrote:
> The summary of your requirements: You have a PNG that looks like an
> old-fashioned punched card, with dots in specific locations that you
> must decode.
>
> The reason behind this requirement now becomes an object of curiosity!
> You will
> get the best answer if you confess what it is...
>
Haha ok ok ! ... i will confess ! :P
It is for a website . http://www.hackthissite.org/missions/programming/
This is a website where you have verry cool programming missions where
you will learn
much about a language.. I already did a lot of them in java... and now I
want to do them in ruby.

> Your "fast reply" was just a Google hit for "PNG". Please always google
> before
> posting.
>
I did.. but not just on PNG.. I didn't thought That would give me
something usefull.

While I am writing this There are already 3 new posts ! ...
thanxs a lot for all the replys !
5aaa92336b530d04b32d1aa5da4280ee?d=identicon&s=25 Held Opslippers (heldopslippers)
on 2009-03-23 00:15
Pascal J. Bourguignon wrote:

> Well it's easy enough to do that with the methods I proposed
> above. Something like:
>
> def decode(image)
>    i=0
>    lastWhite=0
>    bytes=[]
>    (0..(image.height)).each {| y |
>       (0..(image.width)).each {| x |
>          if image.pixelAt(x,y).isWhite? then
>            bytes.push(i-lastWhite)
>            lastWhite=i
>          end
>          i=i+1
>       }
>    }
>    bytes
> end
>
> (That's the reason why I proposed those methods).
>
>
> Now you only have to implement them, doing the same: whish for methods
> that would make implement the method pixelAt(x,y) easy, and do it.
> Then further implement the missing methods.

ah thanxs ! .. I will post the real answer as soon as I have got it ...
(but that could take a while... my RMagick isn;t working... will google
first though:P )
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-03-23 00:25
(Received via mailing list)
jeljer te Wies wrote:

> Haha ok ok ! ... i will confess ! :P

> It is for a website . http://www.hackthissite.org/missions/programming/
> This is a website where you have verry cool programming missions where
> you will learn much about a language..

That's funny. I thought https://github.com/ was a site for random
programming
assignments. (-:

> While I am writing this There are already 3 new posts ! ...
> thanxs a lot for all the replys !

On a non-holiday weekend, you don't get the at-work crowd, and you _do_
get
chatty people.

(And always remember - 4:20 happens twice a day!;)
5aaa92336b530d04b32d1aa5da4280ee?d=identicon&s=25 Held Opslippers (heldopslippers)
on 2009-03-23 02:49
Ok for the people who are interested what the answer is of this verry
long story!
here is the code !

require "rubygems"
require 'RMagick'
include Magick

img = ImageList.new("PNG.png")
i=0
img.each_pixel { |pixel, c, r|
  if pixel.red.to_i== 255 #if white
    puts i
    i=0
  else #else it is black
    i=i+1
  end
}
puts i


Ok it doesn't set the "i" into ascii and then translates it to morsecode
(that will be the next step) ...
AGAIN THANXS GUYS ! really appreciate it :P
This topic is locked and can not be replied to.