Save an image into bytes.
Read an image from bytes.
The bytes can be stored in database as binary.
from io import BytesIO
from PIL import Image
import cv2
import numpy as np
img = Image.open(r'C:\temp\sample.jpg')
bytes_io = BytesIO()
img.save(bytes_io, 'JPEG', quality = 95)
content = bytes_io.getvalue()
#open bytes using cv2
np_array = np.frombuffer(content, np.uint8)
img2 = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
#convert to image
img2 = Image.fromarray(img2)
#open bytes using Image directly
img3 = Image.open(BytesIO(content))