Home > OS >  I wanted to convert image to tensors which I'm not able to do
I wanted to convert image to tensors which I'm not able to do

Time:08-28

Here is the code:

from os import listdir
from os.path import isfile, join

vvy_imgs = []
#images to tensors
for img in np.array([f for f in listdir('E:/vachan') if isfile(join('E:/vachan', f))]): # filepaths 
    print(img) # shows it's of jpg extension
    tensor = tf.io.decode_jpeg(img, channels=3)
    vvy_imgs = np.append(vvy_imgs, tensor)

but it is showing error

InvalidArgumentError: Unknown image file format. One of JPEG, PNG, GIF, BMP required. [Op:DecodeJpeg]

though all the images are of '.jpg' extension.

CodePudding user response:

Seems like img is a path to an image and not an image. Maybe try:

image = tf.io.read_file(img)
tensor= tf.io.decode_jpeg(image)
...
  • Related