[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

convert PNG to hex

jeljer te Wies

3/22/2009 9:20:00 PM

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!
--
Posted via http://www.ruby-....

19 Answers

pjb

3/22/2009 10:00:00 PM

0

jeljer te Wies <jeljer@me.com> writes:

> 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_Networ...
http://www.libpng.org/pub/png/spec/iso/index-o...

--
__Pascal Bourguignon__

Phlip

3/22/2009 10:05:00 PM

0

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

3/22/2009 10:06:00 PM

0

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_Networ...
> http://www.libpng.org/pub/png/spec/iso/index-o...

Wohw thanxs for the fast reply Pascal! ...
though it doesn't work
error: "undefined method `to_hex'"

--
Posted via http://www.ruby-....

jeljer te Wies

3/22/2009 10:11:00 PM

0

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

--
Posted via http://www.ruby-....

Eric Jacoboni

3/22/2009 10:31:00 PM

0

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"

jeljer te Wies

3/22/2009 10:36:00 PM

0

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!
--
Posted via http://www.ruby-....

pjb

3/22/2009 10:41:00 PM

0

jeljer te Wies <jeljer@me.com> writes:

> 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_Networ...
>> http://www.libpng.org/pub/png/spec/iso/index-o...
>
> 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.

--
__Pascal Bourguignon__

jeljer te Wies

3/22/2009 10:44:00 PM

0


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])
--
Posted via http://www.ruby-....

Ben Bleything

3/22/2009 10:47:00 PM

0

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

pjb

3/22/2009 10:49:00 PM

0

jeljer te Wies <jeljer@me.com> writes:

> 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

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 Bourguignon__