Conversion of pictures between byte stream and base64

1.pillow converts the picture into a byte stream

from PIL import Image

import io

import base64

img = Image.open("1.jpg")

imgByteArr = io.BytesIO()

img.save(imgByteArr, format='JPEG')

imgByteArr = imgByteArr.getvalue()

2.pillow converts binary to image

img = Image.fromarray(data)

img.save("1.jpg", quality=100)

3.base64 converts imagebase64 into a picture

avatar_str = data.replace('data:image/png;base64,', '')

avatar_bytes = base64.b64decode(avatar_str)

with open('1.png', 'wb+') as fp:

        fp.write(avatar_bytes)

4. Convert the image to imagebase64

        png = open('1.png','rb')

        res = png.read()

        s = base64.b64encode(res)

        data = 'data:image/png;base64,' + s

5. Convert binary bytes to the format read by opencv

image_data = BytesIO(data)

img = Image.open(image_data)

query = np.asarray(img)

cv2.imwrite('a.jpg', query)

Guess you like

Origin blog.csdn.net/jiulinghouxiao/article/details/125936603