Reputation: 655
I want to save some files in a subdirectory of the current directory, but for whatever reason, even though Python "succeeds" saving the output files, the directory doesn't appear anywhere.
com_path = './'
out_f = "output"
crop_f = "1-cropped"
(…)
curr_path = os.path.join(com_path, out_f, crop_f)
for elem in images:
img = img_trim(elem['img'])
filename = curr_path + 'p_%(p_num)s, (%(orig_name)s)%(ext)s' % elem
cv2.imwrite(filename, img)
print(curr_path)
>>> ./output\1-cropped
print(com_path)
>>> ./
Oddly enough, while using (originally I wrote here curr_path instead of com_path, which was a mistake):
filename = com_path + 'p_%(p_num)s, (%(orig_name)s)%(ext)s' % elem
Everything works just fine and it saves the desired files in the .py script folder.
From what I gather frm other threads and the official documentation, it should work—why doesn't it?
EDIT: as per request:
print(os.getcwd())
>>> C:\Users\iyoossaev\Desktop\Index Cards\tester
where it should be
print(filename)
>>> ./p_71, (p0089).png
>>> ./p_72, (p0090).png
>>> ./p_73, (p0091).png
>>> ./p_74, (p0092).png
Files are not empty, when I use "com_path" everything works (for both loading and saving) they are generated as expected. Changing the saving directory messes things up (without changing the loading directory).
EDIT 2: I decided to paste the entire code just to be sure. The program is meant to load images, convert them to B&W, find relevant of text using mean average of pixels in row/cols and trim them, then save the results with a modified name. Everything works except when I am trying to put them into a subfolder. Sample image to test: https://i.sstatic.net/1LMLf.jpg
# IndexCardSplitter
import cv2 # biblioteka do obróbki
import os # operating system-specific
import re # RegEx
import numpy # obliczenia matematyczne
p_num_off = -18
t_thresh_hor = 210.0
t_thresh_ver = 240.0
t_thresh_m = 25
com_path = os.getcwd()
# com_path = '.'
filenames = os.listdir(com_path)
filenames = [ f for f in filenames if os.path.isfile(os.path.join(com_path, f)) and re.match('[^\_]*\.png$', f) ]
out_f = "output"
crop_f = "1-cropped"
col_f = "2-columns"
def_f = "3-definitions"
def name_mod(filename):
elements = re.match("(p0{1,}([1-9][^\.]*?))(\.....?$)", filename)
orig_name = elements.group(1)
p_num = str(int(elements.group(2)) + p_num_off)
ext = elements.group(3)
return{'orig_name':orig_name, 'p_num':p_num, 'ext':ext}
def load_images(filenames):
images = []
for f in filenames:
print(f)
img_t = cv2.imread(os.path.join(com_path,f), cv2.IMREAD_GRAYSCALE)
thresh, img_t = cv2.threshold(img_t, 128, 255, cv2.THRESH_OTSU)
dictionary = name_mod(f)
dictionary['img'] = img_t
images.append(dictionary)
return images
def img_trim(image):
height, width = image.shape
height = height - 1
width = width - 1
top_trim = 0
bot_trim = height
left_trim = 0
right_trim = width
i = 0 # GÓRA
while i < height and top_trim == 0:
row = image[i, 0:-1]
if numpy.mean(row) < t_thresh_hor:
top_trim = i
i = i + 1
i = height # DÓŁ
while i > 0 and bot_trim == height:
row = image[i, 0:-1]
if numpy.mean(row) < t_thresh_hor:
bot_trim = i
i = i - 1
i = 0 # LEWO
while i < width and left_trim == 0:
col = image[0:-1, i]
if numpy.mean(col) < t_thresh_ver:
left_trim = i
i = i + 1
i = width # PRAWO
while i > 0 and right_trim == width:
col = image[0:-1, i]
# col = image[0:-1, 2700]
if numpy.mean(col) < t_thresh_ver:
right_trim = i
i = i - 1
top_trim = top_trim - t_thresh_m
bot_trim = bot_trim + t_thresh_m
left_trim = left_trim - t_thresh_m
right_trim = right_trim + t_thresh_m
image = image[top_trim:bot_trim, left_trim:right_trim]
return(image)
print('*'*40)
# curr_path = os.path.join(os.getcwd(), out_f, crop_f)
# print(curr_path)
print(com_path)
for elem in images:
img = img_trim(elem['img'])
# filename = os.path.join(os.getcwd(), out_f, crop_f, 'p_%(p_num)s, (%(orig_name)s)%(ext)s' % elem)
filename = com_path + 'p_%(p_num)s, (%(orig_name)s)%(ext)s' % elem
print(filename)
cv2.imwrite(filename, img)
The input filenames are: p0089.png, p0090.png
etc.
Upvotes: 1
Views: 3511
Reputation: 2627
Try setting com_path
to '.' or os.getcwd()
. os.path.join
doesn't replace forward slashes with os.sep
.
Upvotes: 1