简体   繁体   English

如何为 tf.image.decode_image 准备 PIL Image.Image

[英]How to prepare PIL Image.Image for tf.image.decode_image

For a file read with:对于文件读取:

import PIL
import tensorflow as tf
from keras_preprocessing.image import array_to_img

path_image = "path/cat_960_720.jpg"

read_image = PIL.Image.open(path_image)
# read_image.show()

image_decode = tf.image.decode_image(read_image)
print("This is the size of the Sample image:", image_decode.shape, "\n")
print("This is the array for Sample image:", image_decode)

resize_image = tf.image.resize(image_decode, (32, 32))
print("This is the Shape of resized image", resize_image.shape)
print("This is the array for resize image:", resize_image)

to_img = array_to_img(resize_image)
to_img.show()

I keep getting error for this line tf.image.decode_image(read_image) :我不断收到此行tf.image.decode_image(read_image)的错误:

ValueError: Attempt to convert a value (<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=960x586 at 0x11AA49F40>) with an unsupported type (<class 'PIL.JpegImagePlugin.JpegImageFile'>) to a Tensor. ValueError:尝试将具有不受支持的类型 (<class 'PIL.JpegImagePlugin.JpegImageFile'>) 的值 (<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=960x586 at 0x11AA49F40>) 转换为 Tensor。

How can I pass imae read with PIL to tensorflow so that I could decode and resize, so that I could resize this big picture to 32x32x3 ?如何将使用 PIL 读取的 imae 传递给 tensorflow 以便我可以解码和调整大小,以便我可以将这张大图片调整为32x32x3

A few options, here is 1 if you have to use PIL :几个选项,如果您必须使用PIL ,这里是 1:

import PIL
import tensorflow as tf
from keras_preprocessing.image import array_to_img
import numpy as np

path_image = "/content/cat.jpg"

read_image = np.asarray(PIL.Image.open(path_image))
resize_image = tf.image.resize(read_image, (32, 32))
print("This is the Shape of resized image", resize_image.shape)
print("This is the array for resize image:", resize_image)

to_img = array_to_img(resize_image)

Here is an option with TF only:这是一个只有TF的选项:

import tensorflow as tf
from keras_preprocessing.image import array_to_img

path_image = "/content/cat.jpg"

read_image = tf.io.read_file(path_image)
read_image = tf.image.decode_image(read_image)
resize_image = tf.image.resize(read_image, (32, 32))
print("This is the Shape of resized image", resize_image.shape)
print("This is the array for resize image:", resize_image)

to_img = array_to_img(resize_image)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM