0

I'm working on a script to deconstruct an image, and one of the key components is for the script to be as lightweight as possible. So I'm attempting to use as few imports as absolutely possible:

import sys, os, re ,fnmatch
import numpy as np
import cv2
import matplotlib.pyplot as plt

And that's it. The problem is, that one of the images I'm testing on is a GIF:

enter image description here which OpenCV doesn't support, and despite searching for a while I can't seem to find a way to read GIFs (or convert them to png) in pure python.

Every solution I can find uses either Pillow or PIL, both of which are way to big, and really, even OpenCV is pushing it. The GIF is a still image. Is there a easy solution here?

Rich
  • 1,103
  • 1
  • 15
  • 36
  • Pillow/PIL is most popular image module - so it is easier solution. – furas Oct 02 '16 at 06:47
  • So that's a no? – Rich Oct 02 '16 at 06:52
  • you can try to code the loader your self. My gif decoder in C++ is slightly less then 50 KByte of source code. Take a look at this: [How to find where does Image Block start in GIF images?](http://stackoverflow.com/a/32369298/2521214) in the 3MF link you will find all the info you need – Spektre Oct 02 '16 at 07:51
  • I have no idea how to do that. Why can't I just use how PIL does it? – Rich Oct 02 '16 at 15:45
  • @Rich If you do not know how to code then you are left only with use of 3th party libs for this. You can use any but as you mentioned you need something lightweight I provided you with info how much source code GIF requires (it is with encoder/decoder included). I do not know of any lightweight GIF loaders and those I came in contact with was too buggy for my taste. (even nowadays browsers are buggy with GIF's for example without workaround animations does not loop in any of them) – Spektre Oct 03 '16 at 09:13
  • @Rich you need to add `@nick` to your comments so the user `nick` is notified of your comment otherwise (s)he does not know you wrote anything. You can notify only `nick`s that are present in the thread you are commenting in (Question and each Answer are separate threads). The author of the thread is notified automatically. – Spektre Oct 03 '16 at 09:20

1 Answers1

0

a little late maybe for you but there is now a library called gif2numpy which can convert gif images to numpy images so that you can use them with opencv, matplotlib, scipy or the like. It does not depend on PIL or pillow but only on opencv-python, numpy and kaitaistruct. It goes like this:

from __future__ import print_function
import gif2numpy
import cv2

images = "Images/audrey.gif", "Images/hopper.gif", "Images/testcolors.gif"
for image in images:
    np_images, extensions, image_specs = gif2numpy.convert(image)
    print("type of image:", image, type(np_images[0]))
    cv2.imshow("np_image", np_images[0])
    cv2.waitKey()
    cv2.destroyWindow("np_image")

Install it with

pip install gif2numpy

or get it directly from github https://github.com/bunkahle/gif2numpy

bunkus
  • 975
  • 11
  • 19