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:CODESdata"
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数组中获得该值?
RGB通道值将为3的图像格式。如果其灰度图像值为1。
下面的示例代码给出了完整的图像尺寸[height, width, channel]
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