Function Use
def base64_to_image(base64_code): img_data = base64.b64decode(base64_code) img_array = (img_data, numpy.uint8) # img_array = (image_bytes, dtype=np.uint8) # optional image_base64_dec = (img_array, cv2.COLOR_RGB2BGR) return image_base64_dec def image_to_base64(full_path): with open(full_path, "rb") as f: data = () image_base64_enc = base64.b64encode(data) image_base64_enc = str(image_base64_enc, 'utf-8') return image_base64_enc #pass base64 img_bytes = ["img_stream"] img_cv = base64_to_image(img_bytes) uuid_str = str(uuid.uuid1()) img_path = uuid_str +".jpg" (img_path,img_cv)
1. Image to base64 encoding
import cv2 import base64 def cv2_base64(image): img = (image) binary_str = ('.jpg', img)[1].tostring()# encoding base64_str = base64.b64encode(binary_str)#Decode base64_str = base64_str.decode('utf-8') myjson={"bs64":cv2_base64("")} print(myjson) return base64_str
2. Image to binary encoding
import cv2 import base64 def cv2_binary(image): img = (image) binary_str = ('.jpg', img)[1].tostring()# encoding print(binary_str) # base64_str = base64.b64encode(binary_str)# decode # base64_str = base64_str.decode('utf-8') # print(base64_str) return binary_str cv2_binary("") # Or image_file =r"" image_bytes = open(image_file, "rb").read() print(image_bytes)# Binary data
3. Save the image as a binary file and read the binary
# python+OpenCV code for reading images and converting them to binary format files # coding=utf-8 ''' Created on March 24, 2016 Use Opencv to read an image to save it as a binary format file, then read that binary file and convert it to an image for display @author: hanchao ''' import cv2 import numpy as np import struct image = ("") # imageClone = (([0],[1],1),np.uint8) # [0] for rows # [1] for cols # [2] for channels # = (480,640,3) rows = [0] cols = [1] channels = [2] # Convert images to binary files # python write binary file, f = open('name','wb') # Only wb writes binaries # fileSave = open('', 'wb') for step in range(0, rows): for step2 in range(0, cols): (image[step, step2, 2]) for step in range(0, rows): for step2 in range(0, cols): (image[step, step2, 1]) for step in range(0, rows): for step2 in range(0, cols): (image[step, step2, 0]) () # Converting binary to image and displaying it # python reads binary files with rb # (n) in n is the number of bytes to be read, after reading need to be decoded, using ("B", (1)) function # Where "B" is an unsigned integer, occupying one byte, and "b" is a signed integer, occupying one byte. # "c" is a char type and takes up one byte. # "i" is of type int, occupying four bytes, and I is signed plastic, occupying four bytes # "h", "H" for the short type, occupies four bytes, corresponding to the signed, unsigned # "l", "L" for the long type, occupies four bytes, corresponding to the signed, unsigned fileReader = open('', 'rb') imageRead = (, np.uint8) for step in range(0, rows): for step2 in range(0, cols): a = ("B", (1)) imageRead[step, step2, 2] = a[0] for step in range(0, rows): for step2 in range(0, cols): a = ("b", (1)) imageRead[step, step2, 1] = a[0] for step in range(0, rows): for step2 in range(0, cols): a = ("b", (1)) imageRead[step, step2, 0] = a[0] () ("source", image) ("read", imageRead) ("",imageRead) (0)
4. Binary to image
def binary_cv2(bytes): file = open("","wb") (bytes) binary_cv2("bytes") # or from PIL import Image import io img = (("bytes")) ("")
5.base64 to image
def base64_cv2(base64code): img_data = base64.b64decode(base64code) file = open("","wb") (img_data) () base64_cv2("base64code") ============================================ with open("","r") as f: img_data = base64.b64decode(()) file = open("","wb") (img_data) ()
6. Interchange
def base64_to_image(base64_code): img_data = base64.b64decode(base64_code) img_array = (img_data, numpy.uint8) image_base64_dec = (img_array, cv2.COLOR_RGB2BGR) return image_base64_dec # image matrix, need to write ("",img) def image_to_base64(full_path): with open(full_path, "rb") as f: data = () image_base64_enc = base64.b64encode(data) image_base64_enc = str(image_base64_enc, 'utf-8') return image_base64_enc
7.binary to base64
def binary_base64(binary): img_stream = base64.b64encode(binary) bs64 = img_stream.decode('utf-8') print(bs64)
8.base64 to binary
import base64 bs64 = "" img_data = base64.b64decode(bs64) print(img_data)
Above is Python to realize the image of the binary and base64 conversion of the details, more about Python image binary to base64 information please pay attention to my other related articles!