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
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) …
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.
to_hex doesn’t exist in Ruby. You probably want sprintf “%X”
well to explain why I want to do this! …
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
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
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.
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:
get the image in my program
get each pixel and decide if it is white or black
thats all !.. but well … it seams it is harder then it sounds
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
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…
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:
get the image in my program
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.
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 !
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 !
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 )
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.