簡體   English   中英

Python GUI:如何瀏覽目錄並讀取其中的所有文件?

[英]Python GUI: How to browse a directory and read ALL files in it?

這將是我的第一篇文章,關於使用Python GUI讀取文件存在嚴重問題。

因此,我完全迷失於試圖找到一種更聰明的方式來使用GUI在Python 2.7中編寫代碼,以瀏覽目錄並讀取其中的所有文件以進行進一步的操作。 就我而言,我正在讀取的文件是圖像,並且我試圖將其保存在數組中。

我正在使用這些numpy,scikits等來簡化圖像處理工作。 我正在嘗試處理3張名為“ pulse1.jpg”的圖像到“ pulse3.jpg”。

這是我的下面的工作。 我很抱歉,如果有點混亂/不清楚,尤其是因為我還不能發布圖片:

import os
import numpy as np
import cv2
from matplotlib import pyplot as plt
from skimage import color, data, restoration

# first we specify the file directory
directory = 'I:\\My Pictures\\toby arm pics'
filename = "pulse"

# initiate file reading
isfile = os.path.isfile
join = os.path.join

# first, we get the number of files (images)
# in the directory 
number_of_files = sum(1 for item in os.listdir(directory)\
                  if isfile(join(directory, item)))

X = [] # we'll store the read images here
i = 1  # initiate iteration for file reading
string = "" # initiation

# read the images from directory 
# and save it to array X
while i <= number_of_files:
   string = directory + "\\" + filename + str(i) + ".jpg"
   temp = cv2.imread(string, -1)
   X.append(temp)
i += 1

伙計們,非常感謝您的幫助!

>>> import Tkinter, tkFileDialog
>>> root = Tkinter.Tk()
>>> root.withdraw()
''
>>> dirname = tkFileDialog.askdirectory(parent=root,initialdir="/",title='Pick a directory')
>>> for filename in os.listdir(dirname):
>>>     temp = cv2.imread(filename,-1)

也許?

(我從http://code.activestate.com/recipes/438123-file-tkinter-dialogs/獲取了大部分代碼)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM