如何保存大于 90 KB 的 PNG 图像?

How can I save a PNG image greater than 90 KB?

我有一张图片,我正在将它们转换成 NumPy 数组。然后经过一些处理后,我使用 save_img 命令将这些处理过的图像保存到 PNG 中。我想知道如何使用以下代码仅保存大于 90KB 的图像?我想使用 "save_img" 函数

from keras.preprocessing.image import load_img
from keras.preprocessing.image import save_img
from keras.preprocessing.image import img_to_array
# load image as as RGB
img = load_img('bondi_beach.png')
# convert image to a numpy array
img_array = img_to_array(img)
# save the image with a new filename
save_img('bondi_beach_grayscale.png', img_array)

您可以使用以下方法计算图像的权重。

import os
print(os.stat('small_tree.png').st_size/1024,'KB')

获得权重后,您可以根据阈值大小编写一个条件来保存或不保存图像。 希望这可以帮助。谢谢

检查下方 link:https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/ImageDataGenerator

这个问题可以通过使用ImageDataGenerator来解决。您无需将大型数据集转换为内存,而是成批访问它们。使用 model.flow_from_directory、model.fit_generator 等。我使用的是手机,因此无法正确格式化。