ฉันต้องการบันทึกไฟล์บางไฟล์ในไดเร็กทอรีย่อยของไดเร็กทอรีปัจจุบัน แต่ด้วยเหตุผลใดก็ตาม แม้ว่า 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: ฉันตัดสินใจวางโค้ดทั้งหมดเพื่อความแน่ใจ โปรแกรมนี้มีไว้สำหรับโหลดรูปภาพ แปลงเป็นขาวดำ ค้นหาข้อความที่เกี่ยวข้องโดยใช้พิกเซลเฉลี่ยในแถว/คอลัมน์ และตัดแต่ง จากนั้นบันทึกผลลัพธ์ด้วยชื่อที่แก้ไข ทุกอย่างทำงานได้ยกเว้นตอนที่ฉันพยายามใส่มันลงในโฟลเดอร์ย่อย ภาพตัวอย่างที่จะทดสอบ: https://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
เป็นต้น
images
อาจเป็นรายการว่างใช่ไหม ใส่print(filename)
ไว้ในวงของคุณ หรือบางทีไดเร็กทอรีการทำงานปัจจุบันของคุณอาจไม่ใช่สิ่งที่คุณคาดหวัง วางprint(os.getcwd())
ไว้ที่ไหนสักแห่ง หรือโปรดแก้ไขคำถามของคุณเพื่อรวมโปรแกรมสั้นๆ สมบูรณ์ที่แสดงให้เห็นถึงปัญหา ดูตัวอย่างการทำซ้ำขั้นต่ำสำหรับข้อมูลเพิ่มเติม - person Robᵩ   schedule 15.02.2017filename = os.path.join(curr_path, 'p_%(p_num)s, ...'%elem)
ไม่ใช่เหรอ? - person Aran-Fey   schedule 15.02.2017com_path = './'
ด้วยcom_path = 'os.getcwd()'
แล้วหรือยัง? ไม่อย่างนั้น คุณคงอยากแน่ใจว่า './' แก้ไขอะไร! - person Son of a Beach   schedule 15.02.2017