I want to get the file name in my directory to use it as dynamically in ml model.
import os
i = os.listdir('./upload')[0]
print(i)
# ...read data from file `i`...
The problem with your approach is that os.listdir returns file names not file paths. So you need to add the directory name to your filepath:
cv2.imread(os.path.join('upload', i))
Or you can use glob which returns paths:
import glob
import cv2
for image_path in glob.glob('./upload/*.jpg'):
image = cv2.imread(image_path)
Related
I am using python3.10. To unzip a file I have a zip file in folder 'wowo' if I select folder using path and use only file name the code doesn't work. But, when full path+filename given it works. I don't want go give full path and file name together. I want to define path saperately.
zipdata = zipfile.ZipFile('/Volumes/MacHD/MYPY/wowo/NST_cm.zip')
zipinfos = zipdata.infolist()
for zipinfo in zipinfos:
zipinfo.filename = 'Nst.csv'
zipdata.extract(path=path, member=zipinfo)
You could join the two strings in order to form the full filepath.
filepath = os.path.join(path, filename)
zipfile.ZipFile(filepath)
Or I believe the ZipFile function can take a path and file name expression like this
zipfile.ZipFile(path,'filename')
Replacing filename with the name of the file you wish to work with
You can use pathlib and add the path with the filename in the zipfile.zipfile:
import pathlib
path = pathlib.Path('PATH/TO/FOLDER')
zipfile.ZipFile( path / 'filename')
I have more than 1000 JPG images in a folder having different name. I want to rename images as 0.JPG, 1.jpg, 2.jpg...
I tried different code but having below error:
The system cannot find the file specified: 'IMG_0102.JPG' -> '1.JPG'
The below code is one the codes found in this link: Rename files sequentially in python
import os
_src = "C:\\Users\\sazid\\Desktop\\snake"
_ext = ".JPG"
for i,filename in enumerate(os.listdir(_src)):
if filename.endswith(_ext):
os.rename(filename, str(i)+_ext)
How to solve this error. Any better code to rename image files in sequential order?
os.listdir only returns the filenames, it doesn't include the directory name. You'll need to include that when renaming. Try something like this:
import os
_src = "C:\\Users\\sazid\\Desktop\\snake"
_ext = ".JPG"
for i,filename in enumerate(os.listdir(_src)):
if filename.endswith(_ext):
src_file = os.path.join(_src, filename)
dst_file = os.path.join(_src, str(i)+_ext)
os.rename(src_file, dst_file)
just use glob and save yourself the headache
with glob your code turns into this:
import os
from glob import glob
target_dir = './some/dir/with/data'
for i, p in enumerate(glob(f'{target_dir}/*.jpg')):
os.rename(p, f'{target_dir}/{i}.jpg')
in this code glob() gives you a list of found file paths for files that have the .jpg extension, hence the *.jpg pattern for glob, here is more on glob
I am trying to save images after converting them into grayscale from one folder to another. when I run my code, it keeps saving the file in the same folder and making duplicates of all the images. Here is my code, Please guide where my problem lies...
import glob
import cv2
import os
spath=r"C:\Users\usama\Documents\FYP-Data\FYP Project Data\hamza\*.png"
dpath=r"C:\Users\usama\Documents\FYP-Data\FYP Project Data\grayscale images\*.png"
files = os.listdir(spath)
for filename in glob.glob(r'C:\Users\usama\Documents\FYP-Data\FYP Project Data\hamza\*.png'):
print(filename)
img=cv2.imread(filename)
rl=cv2.resize(img, (40,50))
gray_image = cv2.cvtColor(rl, cv2.COLOR_BGR2GRAY)
cv2.imwrite(os.path.join(dpath,filename), gray_image)
If you pass a full pathname to glob.glob(), then it will return the full path of the result files, not just the filenames.
That means in this loop in your code:
for filename in glob.glob(r'C:\Users\usama\Documents\FYP-Data\FYP Project Data\hamza\*.png'):
filename is a full path such as C:\Users\usama\Documents\FYP-Data\FYP Project Data\hamza\myfile1.png.
Then, later in the loop when you call cv2.imwrite(os.path.join(dpath,filename), gray_image), you're trying to join together C:\Users\usama\Documents\FYP-Data\FYP Project Data\grayscale images\*.png and C:\Users\usama\Documents\FYP-Data\FYP Project Data\hamza\myfile1.png, which is the cause of your error.
glob() is convenient to get the full path of just the files you want, but then you have to separate the filename from the directory.
Try using listdir() instead of glob():
import glob
import cv2
import os
sdir=r"C:\Users\usama\Documents\FYP-Data\FYP Project Data\hamza"
ddir=r"C:\Users\usama\Documents\FYP-Data\FYP Project Data\grayscale images"
for filename in os.listdir(sdir):
if not filename.lower().endswith(".png"):
continue
print(filename)
img=cv2.imread(os.path.join(sdir, filename))
rl=cv2.resize(img, (40,50))
gray_image = cv2.cvtColor(rl, cv2.COLOR_BGR2GRAY)
cv2.imwrite(os.path.join(ddir,filename), gray_image)
I attended an interview and they asked me to write a script to move from one directory to another and delete only the .html files.
Now I tried to do this at first using os.remove() . Following is the code:
def rm_files():
import os
from os import path
folder='J:\\Test\\'
for files in os.listdir(folder):
file_path=path.join(folder,files)
os.remove(file_path)
The problem I am facing here is that I cannot figure out how to delete only .html files in my directory
Then I tried using glob. Following is the code:
def rm_files1():
import os
import glob
files=glob.glob('J:\\Test\\*.html')
for f in files:
os.remove(f)
Using glob I can delete the .html files but still I cannot figure out how to implement the logic of moving from one directory to another.
And along with that can someone please help me figure out how to delete a specific file type using os.remove() ?
Thank you.
Either of these methods should work. For the first way, you could just string.endswith(suffix) like so:
def rm_files():
import os
from os import path
folder='J:\\Test\\'
for files in os.listdir(folder):
file_path=path.join(folder,files)
if file_path.endswith(".html"):
os.remove(file_path)
Or if you prefer glob, moving directories is fairly straightforward: os.chdir(path) like this:
def rm_files1():
import os
os.chdir('J:\\Test')
import glob
files=glob.glob('J:\\Test\\*.html')
for f in files:
os.remove(f)
Though it seems unnecessary since glob is taking an absolute path anyway.
Your problem can be described in the following steps.
move to specific directory. This can be done using os.chdir()
grab list of all *.html files. Use glob.glob('*.html')
remove the files. use os.remove()
Putting it all together:
import os
import glob
import sys
def remove_html_files(path_name):
# move to desired path, if it exists
if os.path.exists(path_name):
os.chdir(path_name)
else:
print('invalid path')
sys.exit(1)
# grab list of all html files in current directory
file_list = glob.glob('*.html')
#delete files
for f in file_list:
os.remove(f)
#output messaage
print('deleted '+ str(len(file_list))+' files in folder' + path_name)
# call the function
remove_html_files(path_name)
To remove all html files in a directory with os.remove() you can do like this using endswith() function
import sys
import os
from os import listdir
directory = "J:\\Test\\"
test = os.listdir( directory )
for item in test:
if item.endswith(".html"):
os.remove( os.path.join( directory, item ) )
I am doing OCR on image.
from PIL import Image
import pytesseract
from pytesseract import image_to_string
img1=Image.open('my.png')
print(image_to_string(img1))
How can save the extracted information into text file called "Output.txt"
I found the easy way to save OCR output into text file
def ocr(file_to_ocr):
im = Image.open(file_to_ocr)
txt=pytesseract.image_to_string(im)
return txt
directory = os.path.join("Your_path")
for root,dirs,files in os.walk(directory):
for file in files:
if file.endswith(".jpg"):
pre_fix=file[:-4]
txt=ocr(file)
with open(directory+"\\"+pre_fix+".txt",'w') as f: f.write(str(txt))
You could create a unique folder name using uuid, and then write the output.txt to it like so?:
from uuid import uuid4
import os
folder_name = str(uuid4())
os.makedirs(folder_name)
with open('./{fn}/output.txt'.format(fn=folder_name),'wb') as f:
f.write(image_to_string(img1))