[Answered ]-How to return image in python file? for django

2👍

You have the URL of the image resource in article.top_image.src so it should just be a matter of downloading the image and returning it. You can use requests module for the downloading part:

import requests

def extract(url):
    article = Goose().extract(url)
    if article.top_image is None or article.top_image.src is None
        return "none"

    r = requests.get(article.top_image.src)
    return r.content

This will return the actual image data from the function.

Possibly you want to return that image as a HTTP response, in which case you can return this from your view function:

return HttpResponse(extract(url), content_type="image/jpeg")
👤mhawke

Leave a comment