Answer the question
In order to leave comments, you need to log in
[[+content_image]]
Python cv2 how to convert multidimensional array to image?
Python cv2, you need to convert a multidimensional array to a picture, for example:
Given:
[['255', '255', 255', '190', '190', 160', '76', '45', '78'] ,
['255', '255', 255', '190', '190', 160', '76', '45', '78'],
['255', '255', 255', ' 190', '190', 160', '76', '45', '78']]
Where every 3 values are pixel colors
The result should be a 3 by 3 pixel image with defined colors:
White Gray Purple
White Gray Purple
White Gray Purple
(Could not attach a picture)
Answer the question
In order to leave comments, you need to log in
src = [['255', '255', '255', '190', '190', '160', '76', '45', '78'],
['255', '255', '255', '190', '190', '160', '76', '45', '78'],
['255', '255', '255', '190', '190', '160', '76', '45', '78']]
int_src = [ list(map(int, row)) for row in src ]
red = [ row[0::3] for row in src ] #подразумеваю, что у тебя составляющие идут в порядке RGB
green = [ row[1::3] for row in src ]
blue = [ row[2::3] for row in src ]
image = cv2.merge((blue, green, red)) #opencv по умолчанию хранит изображения в BGR, а не RGB
import cv2
import matplotlib.pyplot as plt
data = [
['255', '255', '255', '190', '190', '160', '76', '45', '78'],
['255', '255', '255', '190', '190', '160', '76', '45', '78'],
['255', '255', '255', '190', '190', '160', '76', '45', '78']
]
to_chunks = lambda x, n:[x[i*n:i*n+n] for i in range(len(x) // n)]
img = [to_chunks(list(map(int, row)), 3) for row in data]
f,ax = plt.subplots(1,1)
ax.imshow(img)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question