1

I am creating one of those cool moving photograph frames, eventually with my own pictures, but for now I just want to search giphy and save/display a gif.

Here's the code I gathered would be useful from their API.

import giphy_client as gc
from giphy_client.rest import ApiException
from random import randint

api_instance = gc.DefaultApi()
api_key = 'MY_API_KEY'
query = 'art'
fmt = 'gif'

try:
    response = api_instance.gifs_search_get(api_key,query,limit=1,offset=randint(1,10),fmt=fmt)
    gif_id = response.data[0]
except ApiException:
    print("Exception when calling DefaultApi->gifs_search_get: %s\n" % e)

with open('test.txt','w') as f:
    f.write(type(gif_id))

I get an object of type: class 'giphy_client.models.gif.Gif', I want to save this gif and display it on a monitor. I understand that I am a far way off on this but I am still learning about API and how to use them. If anyone can help me find a way to save this gif or display it directly from their website, that would be much appreciated!

Checo R
  • 862
  • 14
  • 22
dbarth
  • 33
  • 4

1 Answers1

1

Welcome dbarth!

I see your code does successfully retrieve a random image, that is good. There are 3 steps needed to get the image:

  1. Get the GIF URL.

That giphy_client client you are using, is made with Swagger, so, you can access the REST Response elements like any other object, or print them.

For example:

>>> print(gif_id.images.downsized.url)
'https://media0.giphy.com/media/l3nWlvtvAFHcDFKXm/giphy-downsized.gif?cid=e1bb72ff5c7dc1c67732476c2e69b2ff'

Note that when I print this, I get an URL. The Gif object you got, called gif_id, has a bunch of URLs to download the GIF or MP4 at different resolutions. In this case, I went with the downsized GIF. You can see all the elements retrieved using print(gif_id)

So, I will add this to your code:

gif_url = gif_id.images.downsized.url
  1. Download the GIF

Now that you have a URL, it's time to download the GIF. I will use the requests library to do this, install it with pip if you don't have in your environment. Seems that you already tried to do this, but with an error.

import requests
[...]
with open('test.gif','wb') as f:
    f.write(requests.get(url_gif).content)
  1. Display the GIF

There are a bunch of GUIs for Python to do this, or you can even invoke a browser to show it. You need to investigate which GUI adapts better to your needs. For this case, I will use the example posted here, with a few modifications,to display the Gif using TKinter. Install Tkinter if isn't included with your Python installation.

Final code:

import giphy_client as gc
from giphy_client.rest import ApiException
from random import randint
import requests
from tkinter import *
import time
import os

root = Tk()

api_instance = gc.DefaultApi()
api_key = 'YOUR_OWN_API_KEY'
query = 'art'
fmt = 'gif'

try:
    response = api_instance.gifs_search_get(api_key,query,limit=1,offset=randint(1,10),fmt=fmt)
    gif_id = response.data[0]
    url_gif = gif_id.images.downsized.url
except ApiException:
    print("Exception when calling DefaultApi->gifs_search_get: %s\n" % e)

with open('test.gif','wb') as f:
    f.write(requests.get(url_gif).content)

frames = []
i = 0
while True:  # Add frames until out of range
    try:
        frames.append(PhotoImage(file='test.gif',format = 'gif -index %i' %(i)))
        i = i + 1
    except TclError:
        break

def update(ind):  # Display and loop the GIF
    if ind >= len(frames):
        ind = 0
    frame = frames[ind]
    ind += 1
    label.configure(image=frame)
    root.after(100, update, ind)

label = Label(root)
label.pack()
root.after(0, update, 0)
root.mainloop()

Example of gif show

Keep learning how to use a REST API, and Swagger, if you want to keep using the giphy_client library. If not, you can make the requests directly using the requests library.

Checo R
  • 862
  • 14
  • 22
  • This was much more thorough than I could have hoped for! This is a great answer because you've given me plenty of jumping points to learn about different facets of this problem. My background is in scientific computing, so learning all this new info is exactly what I needed! Thank you for your hard work! – dbarth Mar 05 '19 at 17:43
  • Hey there, I had another issue I figure you might posses the knowledge to help me again? My new question is here https://stackoverflow.com/questions/55013938/tkinter-displaying-distorted-image – dbarth Mar 06 '19 at 01:04
  • Hello, I'm guessing it has to do something about how the animation of a GIF is done, and the part that Tkinter doesn't support GIFs natively. I will check it out later, in the meanwhile you can check this code to use Qt5 instead of Tkinter https://es.stackoverflow.com/a/205620/16435 (in Spanish) – Checo R Mar 06 '19 at 16:37