Opencv - Saving Images to a Particular Folder of Choice

OpenCV - Saving images to a particular folder of choice

The solution provided by ebeneditos works perfectly.

But if you have cv2.imwrite() in several sections of a large code snippet and you want to change the path where the images get saved, you will have to change the path at every occurrence of cv2.imwrite() individually.

As Soltius stated, here is a better way. Declare a path and pass it as a string into cv2.imwrite()

import cv2
import os
img = cv2.imread('1.jpg', 1)
path = 'D:/OpenCV/Scripts/Images'
cv2.imwrite(os.path.join(path , 'waka.jpg'), img)
cv2.waitKey(0)

Now if you want to modify the path, you just have to change the path variable.

Edited based on solution provided by Kallz

Saving an image to another folder of my choice (with ~ ) using OpenCV

You can use something like that:

cv2.imwrite(os.path.expanduser(os.path.join(path , 'waka.jpg')), img)

The problem is with the "~". os.path.expanduser(...) will change that with the appropriate path.

Saving Images in a For Loop with Different Names Without Using a User Function (sckit - Python)

you can put 'waka_{}.tif'.format(i) this will save one image every time it goes through the loop and you will then have waka_1.tif, waka_2.tif, etc.

Python + OpenCV - Saving an image to a specific directory

The right way to combine paths is with os.path.join:

import os
cv2.imwrite(os.path.join(path, 'your_image.jpg'), img)

Also, ensure that str(img) is a valid name for a file, otherwise, use something else.

How to use a loop to save images in different folders in python-opencv

Are you asking how to correct the format of the saved images? Or why the saved images are different than your input ones?

In case you are asking the later:

Assuming your snippet is inside a loop that reads each image separately:

I believe your error is on img[index]. You are saving the image as a column, including all of the rows thus creating an image as shown. Keep in mind that an image is a 2d array and with [] you can specify which values you want to manipulate.

Image[columns, rows]

Since openvcv documentation says that imwrite takes parameters first the filename and second the image, change your code to:

cv2.imwrite('./uniform_luminance/Normal' + str(index) + '.jpg', img)

or

cv2.imwrite('./uniform_luminance/Normal' + str(index) + '.jpg', img[:,:])

img[:, :] the : is used to include all values.

How to open a random image from specified folder/directory in Python OpenCV

This is one of the small mistakes that we usually overlook while working on it. It is mainly because os.listdir returns the contents of a folder. When you are using os.listdir, it just returns file name. As a result it is running like capture = cv2.imread("file_name.png") whereas it should be capture = cv2.imread("path/file_name.png")

So when you are working, try to use the code snippet:

path = './images'
file = random.choice(os.listdir("./images/"))
capture = cv2.imread(os.path.join(path,file))

This will help you run the code.



Related Topics



Leave a reply



Submit