How to decode jpg image from memory?

Viewed 12574

I can read a jpg image from disk via PIL, Python OpenCV, etc. into a numpy array via some built-in functions such as (in the case of OpenCV) arr= cv2.imread(filename).

But how do I decode a jpg in binary format directly from memory?

Use case: I want to put a jpg image into a database in binary format and then read it from the db into memory and decode it to a numpy array.

Is this possible?

3 Answers

Fetching Images from Url to Jpg

    import requests
    from io import BytesIO

    response = requests.get("https://optse.ztat.net/teaser/ES/CW15_ES_bermuda_men.jpg")
    my_img_In_byts = BytesIO(response.content).read()


    path="C:/Users/XX/Desktop/TryingPython/downloadedPic.jpg"

    my_fprinter = open(path, mode='wb')
    print( my_fprinter .write(my_img_In_byts))
    my_fprinter.close()
    print("Done")
Related