我想将一些文件保存在当前目录的子目录中,但无论出于何种原因,即使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:我决定粘贴整个代码以确保。 该程序用于加载图像,将它们转换为B& W,使用行/列中的像素的平均值来查找文本的相关性并修剪它们,然后使用修改的名称保存结果。一切都有效,除非我试图将它们放入子文件夹中。 要测试的示例图片: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
等。
答案 0 :(得分:1)
尝试将npm start
设为'。'或C:\Users\RenysRonaldoDelaCruz\Documents\Phonegap\NuevaOFertas>npm start
> nuevaofertas@1.0.0 start C:\Users\RenysRonaldoDelaCruz\Documents\Phonegap\NuevaOFertas
> node config && HOST=0.0.0.0 webpack-dev-server
module.js:471
throw err;
^
Error: Cannot find module 'replace'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\Users\RenysRonaldoDelaCruz\Documents\Phonegap\NuevaOFertas\config.js:5:17)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v6.9.5
npm ERR! npm v3.10.10
npm ERR! code ELIFECYCLE
npm ERR! nuevaofertas@1.0.0 start: `node config && HOST=0.0.0.0 webpack-dev-server`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the nuevaofertas@1.0.0 start script 'node config && HOST=0.0.0.0 webpack-dev-server'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the nuevaofertas package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node config && HOST=0.0.0.0 webpack-dev-server
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs nuevaofertas
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls nuevaofertas
npm ERR! There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
npm ERR! Please include the following file with any support request:
npm ERR! C:\Users\RenysRonaldoDelaCruz\Documents\Phonegap\NuevaOFertas\npm-debug.log
。 com_path
不会将正斜杠替换为os.getcwd()
。