首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于用户输入生成gif

基于用户输入生成gif
EN

Stack Overflow用户
提问于 2018-12-09 23:41:18
回答 1查看 174关注 0票数 0

我想生成基于用户输入的gif。我可以使用以下代码生成单个gif:`

代码语言:javascript
复制
def getGif(request):
    import time
    import giphy_client
    from giphy_client.rest import ApiException
    from pprint import pprint

    # create an instance of the API class
    api_instance = giphy_client.DefaultApi()
    api_key = 'dc6zaTOxFJmzC'  # str | Giphy API Key.
    q = 'rainbow'  # str | Search query term or prhase.
    limit = 25  # int | The maximum number of records to return. (optional) (default to 25)
    offset = 0  # int | An optional results offset. Defaults to 0. (optional) (default to 0)
    rating = 'g'  # str | Filters results by specified rating. (optional)
    lang = 'en'  # str | Specify default country for regional content; use a 2-letter ISO 639-1 country code. See list of supported languages <a href = \"../language-support\">here</a>. (optional)
    fmt = 'json'  # str | Used to indicate the expected response format. Default is Json. (optional) (default to json)

    try:
        # Search Endpoint
        api_response = api_instance.gifs_search_get(api_key, q, limit=limit, offset=offset, rating=rating, lang=lang,
                                                fmt=fmt)
        # pprint
        img = requests.get(api_response.data[0].images.fixed_height_downsampled.url)
        return HttpResponse(img,content_type='image/gif')
    #return HttpResponse(api_response.data[0].images.fixed_height_downsampled.url, content_type="application/json")
    except ApiException as e:
        print("Exception when calling DefaultApi->gifs_search_get: %s\n" % e)`
EN

回答 1

Stack Overflow用户

发布于 2018-12-09 23:50:18

代码语言:javascript
复制
def getGif(request):
    import time
    import giphy_client
    from giphy_client.rest import ApiException
    from pprint import pprint

    # create an instance of the API class
    api_instance = giphy_client.DefaultApi()
    api_key = 'dc6zaTOxFJmzC'  # str | Giphy API Key.

    # ASK FOR USER INPUT TO ALTER THE QUERY
    q = input("What gifs would you like to find? ")


    limit = 25  # int | The maximum number of records to return. (optional) (default to 25)
    offset = 0  # int | An optional results offset. Defaults to 0. (optional) (default to 0)
    rating = 'g'  # str | Filters results by specified rating. (optional)
    lang = 'en'  # str | Specify default country for regional content; use a 2-letter ISO 639-1 country code. See list of supported languages <a href = \"../language-support\">here</a>. (optional)
    fmt = 'json'  # str | Used to indicate the expected response format. Default is Json. (optional) (default to json)

    try:
        # Search Endpoint
        api_response = api_instance.gifs_search_get(api_key, q, limit=limit, offset=offset, rating=rating, lang=lang,
                                                fmt=fmt)
        # pprint
        img = requests.get(api_response.data[0].images.fixed_height_downsampled.url)
        return HttpResponse(img,content_type='image/gif')
    #return HttpResponse(api_response.data[0].images.fixed_height_downsampled.url, content_type="application/json")
    except ApiException as e:
        print("Exception when calling DefaultApi->gifs_search_get: %s\n" % e)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53693928

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档