hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

PNG Dimensions in a Lilliputian Angel's Eyelash #

by why in inspect

Phantasms of pingspark conjoured by Sam Stephenson:

 IO.read('headline.png')[0x10..0x18].unpack('LL')
 => [713, 54]

(Seen on Mikael’s Tumblelog—which we all hope is the beginning of an epidemic, right?)

said on

Hi _why,

‘LL’ should be ‘NN’ or it will only work on some CPUs or operating systems or something else. Which Sam told me about and which I didn’t bother fixing because I gathered no one was reading this stuff anyways.

(You and your iniquitous hyperlinks.)

It is now corrected and I am prepared for a high-rolling glitterous life among bloogosphere stars

said on

Mikael, welcome to blogging! You are really going to like being a famous bloghard. Learn to be snarky, you’re going to need that. Not as much as hyperlinks, but still.

In fact, I’m thinking about quitting blogging because there’s such a demand for my worldwide classes which teach ordinary non-military Internet personnel how to blog. I think they are so successful because they cross all social and language barriers. I exude young confidence, which most people love worldwide.

Since you’re brand-new to blogging, I don’t mind sharing a little anecdote from my day-to-day. Just today I got an e-mail from a young lady, a Korean reader who decided to write me in the only way she knew how: in Korean.

I asked my friend, a young Korean intern to translate the letter for me. He decided to read the letter first, then to explain its contents. So he took several minutes to read over the letter, to digest it, and then, pursing and unpursing his lips several times carefully before speaking, he said to me, “She is at home alone, crying. Because she loves you so much.” And I straightway fell apart, sobbing audibly. You know: because blogging is such hard, unforgiving work that none of you could possibly understand. And I knew this young lady did.

“Oh, wait,” said my young Korean friend, waving his hands, he’d misread the e-mail. “No, no. This is spam mail for some kind of ramen.” But, still, like I was saying: it was written by a young Korean girl. Which is actually pretty cool.

And thankyou for the correction. It will be sorely merged.

said on

I thought I should add that this has been in image_size by Keisuke Minami for the last 4 years.

said on

Spam and ramen…a delicious lunch indeed!

said on

It’s getting time I finish the Tumbleloggers Manifesto… :-)

said on

evl: not as a one-liner though.

Here’s the equivalent in Python (which requires an extra import):

>>> from struct import unpack
>>> unpack('LL', open('monkey.png').read()[0x10:0x18])
(166L, 100L)

You can do it as a one liner with an ugly import hack:

>>> __import__('struct').unpack('LL', open('monkey.png').read()[0x10:0x18])
(166L, 100L)

Nice trick!

said on

Not very effective to read whole file, so better to:

IO.read('headline.png', 8, 0x10).unpack('NN')

ps. ah, and why ruby don’t support ranges in read method? Would be cool to IO.read('headline.png', 0x10..0x18)

said on

class IO
  def self.read_range(file, range)
    read(file, range.first, range.size)
  end
end

said on

There doesn’t seem to be Range#size so I guess that needs to be range.to_a.size.

said on

Now asking myself how many of these pieces of code I could fit on a pinhead.

said on

flgr: sorry, but i took it from the build-in types documentation in prog. ruby book 1st-ed, and you know it was for ruby 1.6

BUT why did they eliminate both “length” and “size” methods from Range objects in 1.8?

said on
range.last - range.first is probably more efficient than range.to_a.size

Comments are closed for this entry.