To write the images you need to convert the output into the right format (assuming outputs are in the 0,1 range):
# Convert outputs from 0-1 to 0, 255
img_in *= 255.0
# Convert floats to bytes
img_in = img_in.astype(np.uint8)
# Transpose the images from channel first (4, 96, 96) to channel last (96, 96, 4)
image1 = img_in[0, :, :, :].transpose(2, 1, 0)
image2 = img_in[1, :, :, :].transpose(2, 1, 0)
Then it's just a matter of saving the images:
cv2.imwrite('./example_path/image1.png', image1)
cv2.imwrite('./example_path/image2.png', image2)
Answer from 0xd34dc0de on Stack OverflowTo write the images you need to convert the output into the right format (assuming outputs are in the 0,1 range):
# Convert outputs from 0-1 to 0, 255
img_in *= 255.0
# Convert floats to bytes
img_in = img_in.astype(np.uint8)
# Transpose the images from channel first (4, 96, 96) to channel last (96, 96, 4)
image1 = img_in[0, :, :, :].transpose(2, 1, 0)
image2 = img_in[1, :, :, :].transpose(2, 1, 0)
Then it's just a matter of saving the images:
cv2.imwrite('./example_path/image1.png', image1)
cv2.imwrite('./example_path/image2.png', image2)
You would want to split your image into two essentially and then save them individually.
import numpy as np
import cv2
img = np.ones((2,4,96,96),dtype=np.uint8) #creating a random image
img1 = img[0,:,:,:] #extracting the two separate images
img2 = img[1,:,:,:]
img1_reshaped = img1.transpose() #reshaping them to the desired form of (w,h,c)
img2_reshaped = img2.transpose()
cv2.imwrite("img1.png",img1_reshaped) #save the images as .png
cv2.imwrite("img2.png",img2_reshaped)
Hey,
i have a program that reads an image, processes it and returns an image with the contours i need.
I then use cv.imwrite() to save the image on my SSD. Is there a way to NOT override the saved image when i run my program again but to save all images, i.e. starting with image1.png, image2.png, image3.png etc.? With cv.imwrite it doesnt seem to work.