Skip to main content

WOLFRAM vs PYTHON / For the Love of Animated GIF

Not sure where to start writing this topic, but it has been in my head for a while. I will jump around, but all will be related to animated GIF.



But basically when it comes to dealing with GIF (animated) using code or programmably, there seems to be no easy way. For example, there is no Python module that deals with Animated GIF elegantly.  I found it really strange.

In my previous experience as graphic designer, I have used Photoshop to edit and create animated GIF. Editing GIF is simple enough in Photoshop via the interface and menu.

As many years pass by, I tried to challenge myself to try not to rely too much on Photoshop.

The truth is I want to be very flexible with my computing habits these days (it needs to be portable) and also I am becoming a little bit more comfortable in programming.

So, with open source languages like Python, Processing, or Wolfram Cloud, I supposed I should be able to produce animated GIF quite easily right? Well, not that simple apparently. Read on!

iOS Environment for GIF

Now, I use the iPhone and iPad a lot nowadays. It introduces a new tricks and challenges.

iPhone user should be familiar with concept of LIVE Photos (photo with embedded -1.5 and +1.5, for now a total 3 seconds of video with sound that can be viewed by pressing the screen).

And iPhone also can take Burst Shots natively. There is an app called BurstIO that deals with Burst Shots on iPhone and produces video based on that 7-10 fps of burst.

Each frame of burst shots can be viewed and selected easily.

Burst is not exactly GIF, but the idea of turning a sequential frames into a moving images that interest me, in a same way GIF loops beautifully.

Recent iPhone and iPad can actually deal with Animated GIF more elegantly. There are few apps already out there that can import and stitch sequence of images as GIF. Although most of them has limitation in size, sometimes the resulting GIF is too low res.

There is this Workflow app that enable user to "program" using stack to generate procedure to generate animated GIF. This is by far the most interesting one.

I supposed Pythonista app also can create animated GIF, but we will look into that at some point.

NOTE:
Remember that animated GIF needs to be 256 in colors, and with that limited color palette, I want the result that is good enough and will work when shared.

Google Photos App for GIF

Now, I gotta mention Google's way when dealing with GIF. Apparently Google Photos can do this quite elegantly and automatically.

For all photos uploaded to Google Photos, apparently a sequential image will automagically turned into Animated GIF. And quite high resolution too. Around 1 MegaPixels of animated GIF. We can easily share the Animated GIF as GIF or download it as video.

In recent Google Photos update, they allow users to specify up to 50 frames of photos and it will stitch it into Animated GIF (Animation feature).

This is handy, but we need to upload the photos to Google Photos and need Internet.

Mac OS X for GIF

There must be OS X Apps that can make GIF, to capture screen and export it out as animated GIF like Giphy App. I am sure there are already a bunch of apps that this job. Maybe GIFBrew?

I want to point out one cool thing, is that Mac OS Preview app can natively import and preview GIF frames and we can scroll and see the GIF moving frame by frame. We can actually extract GIF frames and output it as static sequential images.


MY TASK IS...

The tasks are simple, with available programming language of my choice (Wolfram or Python or Processing), I should be able to do belows :

  • Extracting animated GIF into sequential image frames, carefully named
  • IMPORT bunch of images and EXPORT it as animated GIF
  • IMPORT GIF and deal with each image frames contained in the GIF, perhaps EDIT the image selectively depending on the need

WOLFRAM WAY

I am using 2nd Trial version of the Wolfram Mathematica. I have 10 days left. 

Now, Wolfram Mathematica has a FUN way when dealing with Images as Object or Symbol or what not they called it. Once we load something and import it as Image entity, we can apply all kind of Functions and Expressions to further process the Image and output all kind of cool stuffs as DATA. 

Importing GIF to Wolfram Mathematica and we will get bunch of images automatically. This is very neat, I found:


Assigning those list of IMAGE into a VARIABLE, I can easily access any photo via INDEX:

Like in Python, I can also selectively deal with some elements of the list. I am using Take[] below:


NOTE: There is also Partition[] and ImagePartition[] but that is for different task.

Next, we can further process any part of the GIF. For example below I am using esoteric way Wolfram Mathematica can do "Edge Detect" whether it is as pure function, or function with argument. I am still familiarzing myself with this kind of way to make expression.



Another fun thing I figure out is how we can use Manipulate [] to view image and give user a bit of control to investigate animated GIF, frame by frame:


I like those ability of Mathematica. Once we undersand certain Expression, we can write a seriously concised "one liner" of code that can do interesting thing.

To import bunch of images and saving it as GIF, was not easy find, but apparently we can use Table[] and Export[].

Below code is a bit weird, but once understood, it's quite interesting.


Table[EdgeDetect[
  Import[FileNames["*.png", 
     "/Users/jimmygunawan/bruce_gif_extracted"][[num]]]], {num, 25}]


Export["/Users/jimmygunawan/Desktop/bruce_edge4.gif", frames]


From the most inner to outer:
  1. FileNames check for file and generate List of Strings
  2. Import will import Images specified via path strings list Index 
  3. Edge Detect will work on the List of Images Element
  4. Table helps Import to iterate over each element to import.
One more thing, extracting GIF with Wolfram:


Above probably not the most elegant, but it is the most clear and understandable for me. We can do something like below too, but I am not sure how to iterate.

Export /@ brucegif
Export["blah.png", #] & /@ brucegif


PYTHON AND JUPYTER NOTEBOOK WAY

Now, with Python, I really thought that PIL (Pillow) Module could handle GIF, and from quick research, apparently we need module called images2gif or something to do this.

There is one module I found that can extract GIF as sequence of images. But combining the images back as GIF was broken.

Maybe PIL can do it, but somewhat not very elegant.

Then I stumble into something like below:

I was surprised that apparently it is easier for Python to generate GIF from videos, rather than simply importing images and stitch them as GIF.

I don't know, I am probably wrong or missing information somewhere.

Although, my accidental finding of Python MoviePy module above that can convert videos into GIF was kind of nice. 

I supposed, I really want Python to be my language to do stuff that Wolfram Mathematica can do. In order to do that sometimes, things are not as simple.

Python should be able to:
- Import Images as Images
- Process Image (maybe using SciKit module)
- Import and Export GIF elegantly

Sometimes I am still confused on: which module can best read and edit process image as pixel or in higher level?

OpenCV for example, can handle video frames in quite elegant way. I believe MoviePy is using OpenCV and ffmpeg.

The thing about Open Source, sometimes we can do something, but have to be so clever in using all the available modules.

As you may already notice, I use Python inside Jupyter Notebook. This is to emulate the Wolfram Mathematica style (there is a lot of things that I like from Wolfram, despite it is an expensive commercial and licensed sofware).

After a while of using Python inside Jupyter Notebook, I noticed that the experience can be pretty elegant, almost like Wolfram. Albeit Python is still Python and a bit wordy. 

Below I am using yet another module to quickly download YouTube and receive video for conversion as GIF above.


Everything can be done in one go.

So...

I cannot give conclusion with this, in one way, we have Wolfram Mathematica (paid, commercial software) that seems to come with "all the tools" with some knowledge based of things. Wolfram itself has its own quirks and esoteric programming language. 

On the other hand, we got Python (free open source) with all the availabe modules out there.

Myself, with self interest and motivation to learn programming for data visualization and analytic, still have to know both, I think.

IF one day Wolfram Mathematica comes pre-installed with Apple computer... just like NeXT computers, I think that would be awesome.

However, again Open Source and always available is always a bigger something.




Comments

Popular posts from this blog

PYTHON PROCESSING / It only really begins ...

Back into Processing again, with Python! While Daniel Shiffman is continuously inspiring us with his CODING TRAIN series on YouTube, using Processing with Java and Javascript language, I decided to free my mind and trying to really do something using Processing and Python language. Installing Python language version on Processing 3 is easy enough, just first download the latest Processing and install the Python language mode via Add Mode button. Other link that might help: https://github.com/jdf/processing.py http://py.processing.org/tutorials/ BLANK MODE As soon as Processing Python Mode, opens up and running I am presented with a  blank environment. Suddenly I recalled my journey in learning programming from zero until now... With Python, outside Processing, commonly people will be introduced to Python IDE or IDLE environment. Something that looks like Console Window or Command Prompt, where we type single liners command. Python Command Line and IDE normally have t

PYTHON / OpenCV, Recreate Uncanny Manga - Anime Style

Can you tell what it is? Computer Vision. Yesterday, I spend almost whole day exploring this opencv module using Python. What I discovered was revealing. Even at the very basic level, I could produce some interesting Image and Video manipulation using all the code collected from documentation and many, many blog tutorials. If you are a total noob like me, I am still getting used to knowing that the CV in OpenCV means Computer Vision! Actuallly, I recalled that I did try to get into OpenCV few years back ago, when I knew no Python and when Python opencv module was probably still early. It was all C++ code and it was a little bit too hard for me. I read a couple of books about opencv at the library, I did not understand a single thing. That was back then. Today, for strange reason, with a bit of knowledge of Python, I can go a little further. EDGE DETECT IN OPENCV Me holding you know what. What leads me this far is my curiosity on how we can replicate Wolfram Langu

WOLFRAM / Making Text With Rainbow Color

Continuing with my Wolfram Mathematica Trial Experience... I watched and went through some more Mathematica introduction videos, read lots of Mathematica documentation and also going through the Wolfram Lab Online again a few times. There are some major learning curves and Mathematica is a lot different from normal programming language. Sometimes there is a lot of interesting "shortcuts", say like: FindFaces[] , WordCloud[] . Sometimes I got a little confused on how one can do iterations. Normally FOR LOOP concept is introduced early, but in Wolfram, because everything is EXPRESSIONS and ENTITY (like OBJECTS), sometimes it gets quite quirky. Mind you I am still in the first impression and having to look at many tutorials. Lots of NEAT EXAMPLES from documentation, but sometimes I got lost. I found Wolfram to be really awesome with LIST and generating list. It's almost too easy when it works visually. I cannot explain them with my own words yet, but there are