convert PNG to hex

J

jeljer te Wies

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!
 
P

Pascal J. Bourguignon

jeljer te Wies said:
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

http://en.wikipedia.org/wiki/Portable_Network_Graphics
http://www.libpng.org/pub/png/spec/iso/index-object.html
 
P

Phlip

jeljer said:
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...
 
J

jeljer te Wies

Phlip said:
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) ...
 
E

Eric Jacoboni

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"
 
J

jeljer te Wies

Eric said:
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!
 
P

Pascal J. Bourguignon

jeljer te Wies said:
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.
 
J

jeljer te Wies

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])
 
B

Ben Bleything

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
 
P

Pascal J. Bourguignon

jeljer te Wies said:
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

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.
 
J

jeljer te Wies

Pascal said:
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

Attachments:
http://www.ruby-forum.com/attachment/3474/PNG.png
 
J

jeljer te Wies

Ben said:
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
 
P

Phlip

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...
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...
 
T

Tim Hunter

jeljer said:
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!

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].
 
P

Pascal J. Bourguignon

jeljer te Wies said:
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.
 
J

jeljer te Wies

Phlip said:
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 !
 
J

jeljer te Wies

Pascal said:
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 )
 
P

Phlip

jeljer said:
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!;)
 
J

jeljer te Wies

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,045
Members
48,769
Latest member
Clifft

Latest Threads

Top