Python os.path.join() - 无法正确组合

Python os.path.join() - won't combine properly

我想在当前目录的子目录中保存一些文件,但是由于某种原因,即使 Python "succeeds" 保存了输出文件,该目录也没有出现在任何地方。

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-cropped
print(com_path)
>>> ./

奇怪的是,在使用时(最初我在这里写 curr_path 而不是 com_path,这是一个错误):

filename = com_path + 'p_%(p_num)s, (%(orig_name)s)%(ext)s' % elem

一切正常,它会将所需的文件保存在 .py 脚本文件夹中。

根据我从其他线程和官方文档中收集到的信息,它应该可以工作——为什么不行?

编辑:根据要求:

print(os.getcwd())
>>> C:\Users\iyoossaev\Desktop\Index Cards\tester

它应该在哪里

print(filename)
>>> ./p_71, (p0089).png
>>> ./p_72, (p0090).png
>>> ./p_73, (p0091).png
>>> ./p_74, (p0092).png

文件不为空,当我使用 "com_path" 时,一切正常(加载和保存),它们按预期生成。更改保存目录会搞砸(不更改加载目录)。

编辑 2: 我决定粘贴整个代码以确保万无一失。 该程序旨在加载图像,将它们转换为黑白,使用 row/cols 和 trim 中的像素平均值查找相关文本,然后使用修改后的名称保存结果。一切正常,除非我试图将它们放入子文件夹中。 要测试的示例图像:http://imgur.com/X2mPZr0

# 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)

输入的文件名是:p0089.png, p0090.png

尝试将 com_path 设置为“.”或 os.getcwd()os.path.join 不会用 os.sep 替换正斜杠。