Issue
I am trying to resize some images using tensorflows image resizing method. I have a loop where I load in the image, resize and then try to write it back out using cv2.imwrite(). However, it continues to error out, does this not output an image? Here's my code:
j=0
for i in images:
skyr_img = imread(my_data_dir+'/'+str(i)) #converts image to an array
skyr_img=tf.image.resize(skyr_img,[171,256],antialias=True,method='bilinear')
print(skyr_img.shape,j)
cv2.imwrite(my_data_dir+'/train/img'+str(j),skyr_img)
j=j+1
and this is the error I get:
(171, 256, 4) 0
---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-49-80ea3aae6498> in <module>
4 skyr_img=tf.image.resize(skyr_img,[171,256],antialias=True,method='bilinear')
5 print(skyr_img.shape,j)
----> 6 cv2.imwrite(my_data_dir+'/train/img'+str(j),skyr_img)
7 j=j+1
error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'imwrite'
> Overload resolution failed:
> - img is not a numpy array, neither a scalar
> - Expected Ptr<cv::UMat> for argument 'img'
Solution
If you are using colab you can mount your drive to colab using
from google.colab import drive
drive.mount('/content/drive', force_remount=True)
You can save images to your drive using
img = cv2.imread('/content/gold.jpeg')
resized_img = tf.image.resize(img, (1050, 1610)).numpy()
path ='/content/drive/MyDrive'
cv2.imwrite(os.path.join(path , 'fish.jpg'), resized_img)
Please refer to this working gist. Thank You.
Answered By - TFer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.