将图像直接从url转换为base64而不保存为文件python的方法

时间:2023-02-06 08:15:46

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.

我想将基于Web的图像转换为base64。我现在知道如何将图像保存为.jpg文件,然后使用base64库将.jpg文件转换为base64字符串。

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

我想知道我是否可以先跳过保存图像的步骤?

Thanks!

1 个解决方案

#1


19  

Using the requests library:

使用请求库:

import base64

import requests

def get_as_base64(url):
    return base64.b64encode(requests.get(url).content)

#1


19  

Using the requests library:

使用请求库:

import base64

import requests

def get_as_base64(url):
    return base64.b64encode(requests.get(url).content)