python - 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 数组中获取该值?
解决方案
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
推荐阅读
- python-3.x - Python 帮助 - 添加基于日期范围创建列
- python - 如何在 MacOS Catalina 上使用 Python 中的模块创建全局可用的文件夹?
- python - 如何在 SQL 中传递多个 cli 参数以检索月份
- python - 在另一个线程上编写视频(Python OpenCV)
- django - Django - 当页面不存在或用户未通过身份验证时返回更改答案
- r - 线性回归显示了 R 中所有分类选项的结果
- git - 创建存储库后创建 .gitignore 文件并发布 .idea 文件?
- node.js - 尝试使用聚合管道执行 addField 并且它不起作用
- css - 从父样式补充子样式(SCSS + CSS-Modules)
- url - 如何在docusign中撤销发件人网址:获取404