21 lines
479 B
Python
21 lines
479 B
Python
|
|
import base64
|
||
|
|
import io
|
||
|
|
|
||
|
|
try:
|
||
|
|
import cv2
|
||
|
|
except Exception:
|
||
|
|
cv2 = None
|
||
|
|
from PIL import Image
|
||
|
|
|
||
|
|
|
||
|
|
def get_image_base64(img_url):
|
||
|
|
if cv2 is not None:
|
||
|
|
img = cv2.imread(img_url)
|
||
|
|
_, img_encoded = cv2.imencode(".jpg", img)
|
||
|
|
return base64.b64encode(img_encoded.tobytes()).decode("utf-8")
|
||
|
|
image = Image.open(img_url).convert("RGB")
|
||
|
|
buf = io.BytesIO()
|
||
|
|
image.save(buf, format="JPEG")
|
||
|
|
return base64.b64encode(buf.getvalue()).decode("utf-8")
|
||
|
|
|