Convert PNG to hex

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!

jeljer te Wies [email protected] writes:

image = f.readlines

puts image.to_hex

hope somebody can give me a kick in the right direction

http://www.libpng.org/pub/png/spec/iso/index-object.html

Pascal J. Bourguignon wrote:

jeljer te Wies [email protected] writes:

image = f.readlines

puts image.to_hex

hope somebody can give me a kick in the right direction

PNG - Wikipedia
Portable Network Graphics (PNG) Specification (Second Edition)

Wohw thanxs for the fast reply Pascal! …
though it doesn’t work
error: “undefined method `to_hex’”

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) …

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…

jeljer te Wies [email protected] 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”

Eric J. wrote:

jeljer te Wies [email protected] 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! :stuck_out_tongue:
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!

This is what i now tried (image is ofcourse a array so I did 2 just to
test it) but
it’s not working :frowning:

f = File.new(“PNG.png”)
image = f.readlines

puts sprintf ("%X", image[2])

jeljer te Wies [email protected] writes:

PNG - Wikipedia
Portable Network Graphics (PNG) Specification (Second Edition)

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.

On Sun, Mar 22, 2009 at 3:43 PM, jeljer te Wies [email protected] 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 :frowning:

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

jeljer te Wies [email protected] 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.

Pascal J. Bourguignon wrote:

jeljer te Wies [email protected] 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

Ben B. wrote:

On Sun, Mar 22, 2009 at 3:43 PM, jeljer te Wies [email protected] 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 :frowning:

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

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…

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.html#get_pixels] or
#view [http://studio.imagemagick.org/RMagick/doc/image3.html#view].

jeljer te Wies [email protected] 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.

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 ! :stuck_out_tongue:
It is for a website . Hack This Site
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 !

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 )

jeljer te Wies wrote:

Haha ok ok ! … i will confess ! :stuck_out_tongue:

It is for a website . Hack This Site
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!:wink:

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 :stuck_out_tongue: