简体   繁体   中英

Way to convert image straight from URL to base64 without saving as a file in Python

I'm looking to convert a web-based image to base64. I know how to do it currently by saving the image as a .jpg file and then using the base64 library to convert the .jpg file to a base64 string.

I'm wondering whether I could skip out the step of saving the image first? Thanks!

Using the requests library:

import base64
import requests


def get_as_base64(url):

    return base64.b64encode(requests.get(url).content)

Since requests is not an official package, I prefer to use urllib.

from urllib.request import urlopen 
import base64

base64.b64encode(urlopen("http://xxx/yyy/abc.jpg").read())

    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM