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
Cannot Find Reference 'Xxx' in _Init_.Py
Print 5 Items in a Row on Separate Lines for a List
Python SQL Select With Possible Null Values
Removing Non-Breaking Spaces from Strings Using Python
How to Remove Carriage Return in a Dataframe
Fill With Nan When Length of Values Does Not Match Length of Index
Pandas: Merge Data Frames on Datetime Index
How to Check the Version of Python Modules
Pythonic Way to Find Maximum Value and Its Index in a List
Using Tkinter in Python to Edit the Title Bar
Pick Dictionary Keys:Values Randomly
Check to See If Python Script Is Running
How to Extract Table Names and Column Names from SQL Query
How to Use a Pre-Trained Neural Network With Grayscale Images
Valueerror: Too Many Values to Unpack (Expected 2) in Django
Open a Putty Window and Run Ssh Commands - Python
Most Efficient Way to Forward-Fill Nan Values in Numpy Array