Getting the image count

I'm doing some work with images, and want to support displaying files that can contain multiple images, such as GIF, TIFF and ICO. If I know the number of contained images ahead of time everything works great, but if I attempt to load using an index greater than the number of images, I get a segfault; this means that a solution based on try/except will not work.

  Is there a way to inspect an image file ahead of time to determine the number of images, and hence the maximum index that can be used?

-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com

Ed Leafe wrote:

    I'm doing some work with images, and want to support displaying files that can contain multiple images, such as GIF, TIFF and ICO. If I know the number of contained images ahead of time everything works great, but if I attempt to load using an index greater than the number of images, I get a segfault; this means that a solution based on try/except will not work.

    Is there a way to inspect an image file ahead of time to determine the number of images, and hence the maximum index that can be used?

wx.Image.GetImageCount("image-filename")

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

That doesn't seem to work for GIF files; the docs seem to imply that this is only for TIFF and ICO formats, too. Is there anything else I can try?

-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com

···

On Sep 22, 2007, at 4:00 PM, Robin Dunn wrote:

    I'm doing some work with images, and want to support displaying files that can contain multiple images, such as GIF, TIFF and ICO. If I know the number of contained images ahead of time everything works great, but if I attempt to load using an index greater than the number of images, I get a segfault; this means that a solution based on try/except will not work.
    Is there a way to inspect an image file ahead of time to determine the number of images, and hence the maximum index that can be used?

wx.Image.GetImageCount("image-filename")

ImageMagick? It's free, cross-platform, open-source with a
GPL-compatible license.

And, of course, it has a Python interface:
http://www.imagemagick.org/script/api.php#python

- Tal

···

On 9/23/07, Ed Leafe <ed@leafe.com> wrote:

On Sep 22, 2007, at 4:00 PM, Robin Dunn wrote:

>> I'm doing some work with images, and want to support
>> displaying files that can contain multiple images, such as GIF,
>> TIFF and ICO. If I know the number of contained images ahead of
>> time everything works great, but if I attempt to load using an
>> index greater than the number of images, I get a segfault; this
>> means that a solution based on try/except will not work.
>> Is there a way to inspect an image file ahead of time to
>> determine the number of images, and hence the maximum index that
>> can be used?
>
> wx.Image.GetImageCount("image-filename")

        That doesn't seem to work for GIF files; the docs seem to imply that
this is only for TIFF and ICO formats, too. Is there anything else I
can try?

Thanks, but as this is for the Dabo framework, I can't add dependencies without greatly limiting its usefulness.

-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com

···

On Sep 23, 2007, at 8:16 AM, Tal Einat wrote:

ImageMagick? It's free, cross-platform, open-source with a
GPL-compatible license.
ImageMagick – Mastering Digital Image Alchemy

Ed Leafe wrote:

    I'm doing some work with images, and want to support displaying files that can contain multiple images, such as GIF, TIFF and ICO. If I know the number of contained images ahead of time everything works great, but if I attempt to load using an index greater than the number of images, I get a segfault; this means that a solution based on try/except will not work.
    Is there a way to inspect an image file ahead of time to determine the number of images, and hence the maximum index that can be used?

wx.Image.GetImageCount("image-filename")

    That doesn't seem to work for GIF files; the docs seem to imply that this is only for TIFF and ICO formats, too.

Hmm... The GIF handler should be able to do it too, you may want to enter a bug report about it and see if anybody picks up the task.

Is there anything else I can try?

The Animation class can do it:

  >>> import wx
  >>> import wx.animate
  >>> a = wx.animate.Animation('AG00178_.gif')
  >>> a.GetFrameCount()
  17

···

On Sep 22, 2007, at 4:00 PM, Robin Dunn wrote:

  >>>

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Thanks! Combined with the imghdr module, I can detect GIFs and use that code, but use GetFrameCount() with TIFF and ICO.

  Dabo's dImage control now allows you to choose the frame to display in multi-image files simply by setting the PictureIndex property. It also has a FrameCount property that works independently of image type.

-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com

···

On Sep 24, 2007, at 6:05 PM, Robin Dunn wrote:

The Animation class can do it:

>>> import wx
>>> import wx.animate
>>> a = wx.animate.Animation('AG00178_.gif')
>>> a.GetFrameCount()
17
>>>