[[+content_image]]
N
N
nerklion2021-11-13 14:37:43
Python
nerklion, 2021-11-13 14:37:43

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

[[+comments_count]] answer(s)
V
Vindicar, 2021-11-13
@Vindicar

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

V
Vladimir Kuts, 2021-11-13
@fox_12

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)

618fb0849826b433569879.jpeg

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question