Home > other >  how to add one dimension in the front of an image?
how to add one dimension in the front of an image?

Time:12-09

Now I have change a image (32,32) to (32,32,1) by these methods

img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = np.expand_dims(img, axis=-1)
img = img.astype(np.float32)/255
img = tf.image.resize(img, [32,32])

But now, I want to change from (32,32,1) to (1,32,32,1), so I tried to use

img = img.reshape(1,32,32,1)

However, it shows 'EagerTensor' object has no attribute 'reshape'. So what other method can I use?

CodePudding user response:

Try:

img = tf.random.normal((64, 64, 1))
img = tf.image.resize(img, [32,32])

img = tf.reshape(img, (1,32,32,1))

Or

img = tf.expand_dims(img, axis=0)
  • Related