gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:39:52 24 4
gpt4 key购买 nike

我想将一些文件保存在当前目录的子目录中,但无论出于何种原因,即使Python“成功”保存了输出文件,该目录也不会出现在任何地方。

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)
>>> ./

奇怪的是,在使用时(最初我在这里写的是 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:为了确定起见,我决定粘贴整个代码。该程序旨在加载图像,将其转换为黑白,使用行/列中像素的平均平均值查找相关文本并修剪它们,然后使用修改后的名称保存结果。一切正常,除非我试图将它们放入子文件夹中。要测试的示例图像:/image/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)

输入文件名是:p0089.png、p0090.png

最佳答案

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

关于Python os.path.join() - 无法正确组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42236186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com