首页 > 解决方案 > python) 读取图像数组

问题描述

import tensorflow as tf
from keras_preprocessing.image import ImageDataGenerator
import numpy as np
from PIL import Image

import matplotlib.pyplot as plt

Training_data_dir = "D:\CODES\data"
batch_size = 4


training_data_Augmentation = ImageDataGenerator()


train_generator = training_data_Augmentation.flow_from_directory(
    Training_data_dir,
    batch_size=batch_size, #batch size 설정
    target_size=(50, 150), #픽셀값
    class_mode='categorical', #분류 방식에 대해 지정한다고 하는데 잘 모르겠음 
    shuffle=True #데이터 순서를 랜덤하게 가져온다.
)

img, label = next(train_generator)
plt.figure(figsize=(20, 20))
r=g=b = 0

for i in range(4):
    image_name = img[i]
    im = Image.open(img[i])
    rgb_im = im.convert('RGB')
    r,g,b = rgb_im.getpixel((15, 17))
    print('R:'+ str(r) + ' G:' + str(g) + ' B:' + str(b))

首先,对不起我简短的英语我是韩语...

我想从图像数组中获取“RGB”值。

如何从 python 数组中获取该值?

标签: pythontensorflow

解决方案


RGB 通道值在图像格式中为 3。如果它的灰度图像值为1。

下面的示例代码给出了完整的图像大小 [高度、宽度、通道]

import tensorflow as tf
from PIL import Image
img = Image.open('Image.jpeg')
img = tf.keras.preprocessing.image.array_to_img(img)
array = tf.keras.preprocessing.image.img_to_array(img)
tensor_img = tf.convert_to_tensor(array, dtype=tf.float32)
tensor_img

输出

<tf.Tensor: id=0, shape=(168, 300, 3), dtype=float32, numpy=[]>

或仅获取 RGB 值

from PIL import Image
img = Image.open('Image.jpeg')
img.mode

输出

RGB

推荐阅读