简体   繁体   中英

convert image array to string

Below code can convert actual image to string, but I want to convert image array (32, 32, 3) to string, how to do that without constructing array to actual image?

import base64

with open('Icon_yes.JPG', "rb") as image_file:
    encoded_string = base64.b64encode(image_file.read())

Thank you

if you want a simple(without constructing array to actual image) and use base64
maybe you shoule use ply or cv2

import matplotlib.pyplot as plt
import base64
# this this the arr
img_arr = plt.imread(r"1.jpg")

plt.imsave('Icon_yes.jpg', img_arr)
with open('Icon_yes.JPG', "rb") as image_file:
    encoded_string = base64.b64encode(image_file.read())

Assuming your image array is a numpy array you can simply use the .tostring() method.

import numpy as np

img_arr = np.random.rand(32, 32, 3)
img_arr.tostring()

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