OpenCV-Python cv2.imdecode() and cv2.imencode() picture decoding and coding

Keywords: encoding network

cv2.imdecode() function reads data from specified memory cache and converts (decodes) data into image format; it is mainly used to recover images from network transmission data.
cv2.imencode() function is to convert (encode) the image format into streaming data and assign it to memory cache. It is mainly used for compressing image data format to facilitate network transmission.


imdecode() uses


Read image data from the network and convert it into image format:

# -*- coding: utf-8 -*-
import numpy as np
import urllib
import cv2

url = 'http://www.pyimagesearch.com/wp-content/uploads/2015/01/google_logo.png'
resp = urllib.urlopen(url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
cv2.imshow('URL2Image',image)
cv2.waitKey()

Display pictures:





imencode() uses


Encoding the picture into the cache and saving it locally:

# -*- coding: utf-8 -*-
import numpy as np
import urllib
import cv2

img = cv2.imread('0122.jpg')
# '.jpg'means that the img of the current picture is encoded in jpg format, and the result of encoding in different formats is different.
img_encode = cv2.imencode('.jpg', img)[1]
# imgg = cv2.imencode('.png', img)

data_encode = np.array(img_encode)
str_encode = data_encode.tostring()

# Cached data is saved locally
with open('img_encode.txt', 'w') as f:
    f.write(str_encode)
    f.flush


imencode()+imdecode() is used


Picture encoding is saved locally, read local file, decode and restore to picture format:

# -*- coding: utf-8 -*-
import numpy as np
import urllib
import cv2

img = cv2.imread('0122.jpg')
# '.jpg'means that the img of the current picture is encoded in jpg format, and the result of encoding in different formats is different.
img_encode = cv2.imencode('.jpg', img)[1]
# imgg = cv2.imencode('.png', img)

data_encode = np.array(img_encode)
str_encode = data_encode.tostring()

# Cached data is saved locally and in txt format
with open('img_encode.txt', 'w') as f:
    f.write(str_encode)
    f.flush

with open('img_encode.txt', 'r') as f:
    str_encode = f.read()

nparr = np.fromstring(str_encode, np.uint8)
img_decode = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
cv2.imshow("img_decode", img_decode)
cv2.waitKey()

Or:

# -*- coding: utf-8 -*-
import numpy as np
import urllib
import cv2

img = cv2.imread('0122.jpg')
# '.jpg'means that the img of the current picture is encoded in jpg format, and the result of encoding in different formats is different.
img_encode = cv2.imencode('.jpg', img)[1]
# imgg = cv2.imencode('.png', img)

data_encode = np.array(img_encode)
str_encode = data_encode.tostring()

# Cached data is saved locally and in txt format
with open('img_encode.txt', 'w') as f:
    f.write(str_encode)
    f.flush

with open('img_encode.txt', 'r') as f:
    str_encode = f.read()

image = np.asarray(bytearray(str_encode), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
cv2.imshow('img_decode',image)
cv2.waitKey()


Posted by dcole on Wed, 06 Feb 2019 16:30:16 -0800