반응형
numpy array to image¶
In [1]:
import numpy as np
from PIL import Image
# Generate a random integer between 0 and 255
arr = np.random.randint(0,255, size=(34, 44), dtype=np.uint8)
arr
Out[1]:
array([[ 32, 201, 105, ..., 28, 29, 98], [207, 219, 114, ..., 117, 201, 237], [ 70, 89, 43, ..., 177, 221, 146], ..., [153, 251, 47, ..., 78, 115, 159], [184, 160, 229, ..., 98, 0, 248], [124, 203, 101, ..., 89, 201, 67]], dtype=uint8)
In [2]:
# Generate an RGB frame with a shape of 340x440x3.
rgb_frame = np.zeros((34,44,3), dtype=np.uint8)
rgb_frame
Out[2]:
array([[[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]], ..., [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]]], dtype=uint8)
In [3]:
# Overwriting the "array" variable with the first value in the innermost dimension.
rgb_frame[:,:,0] = arr
rgb_frame
Out[3]:
array([[[ 32, 0, 0], [201, 0, 0], [105, 0, 0], ..., [ 28, 0, 0], [ 29, 0, 0], [ 98, 0, 0]], [[207, 0, 0], [219, 0, 0], [114, 0, 0], ..., [117, 0, 0], [201, 0, 0], [237, 0, 0]], [[ 70, 0, 0], [ 89, 0, 0], [ 43, 0, 0], ..., [177, 0, 0], [221, 0, 0], [146, 0, 0]], ..., [[153, 0, 0], [251, 0, 0], [ 47, 0, 0], ..., [ 78, 0, 0], [115, 0, 0], [159, 0, 0]], [[184, 0, 0], [160, 0, 0], [229, 0, 0], ..., [ 98, 0, 0], [ 0, 0, 0], [248, 0, 0]], [[124, 0, 0], [203, 0, 0], [101, 0, 0], ..., [ 89, 0, 0], [201, 0, 0], [ 67, 0, 0]]], dtype=uint8)
In [4]:
# Representing an array as an image using the Image function in PIL
Image.fromarray(rgb_frame, "RGB")
Out[4]:
반응형
'python' 카테고리의 다른 글
파이썬 타입이 시간인 데이터 만들기 datatime (0) | 2023.08.06 |
---|---|
파이썬 타입 확인 isinstance (0) | 2023.08.05 |
넘파이 어레이 크기 확인 shape (0) | 2023.08.03 |
넘파이 어레이 값 타입 확인 dtype (0) | 2023.08.03 |
넘파이 최대값 위치 찾기 / 넘파이 최소값 위치 찾기 (0) | 2023.08.03 |
댓글