简体   繁体   English

Keras ImageDataGenerator 在保存到目录时无意中更改了我的图像的颜色

[英]Keras ImageDataGenerator unintentionally changes the color of my images when saving to directory

I'm trying to take an input image and save 10 augmented versions of that image using the flow method of a ImageDataGenerator object.我正在尝试使用 ImageDataGenerator object 的流方法获取输入图像并保存该图像的 10 个增强版本。 The issue is that it's unintentionally changing the color of my image, even when I pass no arguments into the ImageDataGenerator class.问题是它无意中改变了我的图像颜色,即使我没有将 arguments 传递给 ImageDataGenerator class。

Here's my input image and code with my output image below.这是我的输入图像和代码,下面是我的 output 图像。 I'm using Tensorflow 2.2.0我正在使用 Tensorflow 2.2.0

input image输入图像

from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator

img = image.load_img(r'C:\Users\me\Desktop\test.jpg')
img_array = image.img_to_array(img)
img_array = img_array.reshape((1,) + img_array.shape)

datagen = ImageDataGenerator() #no arguments, no augmentations
save_to_dir = r'C:\Users\me\Desktop'

i = 0
for batch in datagen.flow(img_array, batch_size=1, save_to_dir=save_to_dir, save_format='jpg'):
    if i == 9:
        break
    i += 1

output image output 图像

The color of the image is drastically different.图像的颜色完全不同。 Any help would be appreciated.任何帮助,将不胜感激。

The issue is that the ImageDataGenerator.flow method automatically re-scales your image without being able to disable it while using the save_to_dir argument.问题是 ImageDataGenerator.flow 方法会自动重新缩放您的图像,而无法在使用 save_to_dir 参数时禁用它。 I was able to fix this issue with the following code.我能够使用以下代码解决此问题。

from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator

img = image.load_img(r'C:\Users\me\Desktop\test.jpg')
img_array = image.img_to_array(img)
img_array = img_array.reshape((1,) + img_array.shape)

datagen = ImageDataGenerator() #no arguments, no augmentations
save_to_dir = r'C:\Users\me\Desktop'

i = 0
for batch in datagen.flow(img_array, batch_size=1, save_format='jpg'):
    img_save = image.array_to_img(batch[0], scale=False) #scale=False did the trick
    img_save.save(save_to_dir + fr'\augment_{i}.jpg') #save image manually
    if i == 9:
        break
    i += 1

Its because the NumPy array that comes from datagen has dtype as float32 which is not supported dtype.这是因为来自datagen的NumPy数组的dtype为float32,不支持dtype。 Whenever the dype is float32 it will clip the value of array before saving or displaying.每当 dype 为float32时,它将在保存或显示之前剪切数组的值。
Try converting the dtype of img_array that comes from datagen to int32 or uint8 before saving.在保存之前尝试将来自 datagen 的 img_array 的 dtype 转换为int32uint8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Keras:如何在使用带有 flow_from_dataframe / flow_from_directory 的 ImageDataGenerator 时禁用调整图像大小? - Keras: how to disable resizing of images when using an ImageDataGenerator with flow_from_dataframe / flow_from_directory? 使用ImageDataGenerator的Tensorboard和Keras图像 - Tensorboard and Keras Images with ImageDataGenerator Keras ImageDataGenerator不同的图像 - Keras ImageDataGenerator different images 使用keras ImageDataGenerator时,如何在测试阶段对图像应用归一化? - How to apply normalization to images in testing phase when using keras ImageDataGenerator? 使用ImageDataGenerator时Keras冻结 - Keras freezes when using ImageDataGenerator 如何在 keras 中使用 ImageDataGenerator 和 flow_from_directory 保存增强图像 - How to save augmented images using ImageDataGenerator and flow_from_directory in keras Keras ImageDataGenerator 方法 flow_from_directory - Keras ImageDataGenerator method flow_from_directory 具有color_mode的keras ImageDataGenerator.flow - keras ImageDataGenerator.flow with color_mode Keras ImageDataGenerator 用于在单独的目录中使用图像和掩码进行分割 - Keras ImageDataGenerator for segmentation with images and masks in separate directories Keras ImageDataGenerator 返回具有意外失真的图像 - Keras ImageDataGenerator returns images with unexpected distortion
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM