Код: Выделить всё
import cv2
import numpy as np
random_image = np.random.randint(255, size=(4,4,3), dtype=np.uint8)
cv2.imshow('Image', random_image)
cv2.waitKey()
_, img_encoded = cv2.imencode('.jpg', random_image)
img_string = img_encoded.tostring()
npimg = np.fromstring(img_string, dtype=np.uint8)
img = cv2.imdecode(npimg, 1)
cv2.imshow('Image', img)
cv2.waitKey()
# Does not happen with png
_, img_encoded = cv2.imencode('.png', random_image)
img_string = img_encoded.tostring()
npimg = np.fromstring(img_string, dtype=np.uint8)
img = cv2.imdecode(npimg, 1)
cv2.imshow('Image', img)
cv2.waitKey()
Исходное:

JPG

PNG
Повторно отредактировано с использованием изображений 512x512
Оригинал 512x512
JPG 512x512
PNG 512x512
Подробнее здесь: https://stackoverflow.com/questions/643 ... f-an-image
Мобильная версия