How to use image.Decode()

2,195 views
Skip to first unread message

Mark Summerfield

unread,
Oct 27, 2010, 11:51:11 AM10/27/10
to golang-nuts
Hi,

I imported "image" and tried image.Decode() but all I got was an "image:
unknown format" error.

According to the image.Decode() docs:

"Decode decodes an image that has been encoded in a registered
format. ... Format registration is typically done by the init method
of the codec-specific package"

So then I tried importing "image/png" and "image/jpg" since the images
I'm using are in one or other of these formats. But now of course I get
an "imported and not used: image/png" error since I don't explicitly use
anything from their APIs.

Or should I just check the file extension and use the png.Decode() or
jpg.Decode() methods as appropriate?

Thanks!

--
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
C++, Python, Qt, PyQt - training and consultancy
"Advanced Qt Programming" - ISBN 0321635906
http://www.qtrac.eu/aqpbook.html

Evan Shaw

unread,
Oct 27, 2010, 11:55:36 AM10/27/10
to Mark Summerfield, golang-nuts
On Wed, Oct 27, 2010 at 10:51 AM, Mark Summerfield <li...@qtrac.plus.com> wrote:
> So then I tried importing "image/png" and "image/jpg" since the images
> I'm using are in one or other of these formats. But now of course I get
> an "imported and not used: image/png" error since I don't explicitly use
> anything from their APIs.

Try

import _ "image/png"

This will give you the side effect of importing, but you can avoid
"not used" errors.

- Evan

Mark Summerfield

unread,
Oct 27, 2010, 12:20:49 PM10/27/10
to Evan Shaw, golang-nuts

Thanks, that worked like a charm.

Reply all
Reply to author
Forward
0 new messages