2017-02-25 6 views
0

EclipseでPyDevを使用し、「AttributeError: 'Namespace」オブジェクトを受け取ったオブジェクトには' perfect4k 'という属性はありませんが、プロジェクトはプロジェクトフォルダの下に保存されます。ロードするために事前に感謝し、私は、PythonでニュービーだFYI、これは私がインターネットをオフに借りているサンプルコードでPythonでArgparseを使用して画像を読み込んでいます

import numpy as np 
from PIL import Image 
import time 
from mazes import Maze 
from factory import SolverFactory 

# Read command line arguments - the python argparse class is convenient here. 
import argparse 
sf = SolverFactory() 
parser = argparse.ArgumentParser() 
parser.add_argument("-m", "--method", nargs='?', const=sf.Default, default=sf.Default, 
         choices=sf.Choices) 
parser.add_argument("perfect4k.png", help="4kImage", default=None, nargs='?') #input_file 
parser.add_argument("SaveM.png", help="4kImageSave", default=None, nargs='?') #output_file 
args = parser.parse_args() 

method = args.method 

# Load Image 
print ("Loading Image") 
im = Image.open(args.perfect4k.png) #args.input_file 

# Create the maze (and time it) - for many mazes this is more time consuming than solving the maze 
print ("Creating Maze") 
t0 = time.time() 
maze = Maze(im) 
t1 = time.time() 
print ("Node Count:", maze.count) 
total = t1-t0 
print ("Time elapsed:", total, "\n") 

# Create and run solver 
[title, solver] = sf.createsolver(args.method) 
print ("Starting Solve:", title) 

t0 = time.time() 
[result, stats] = solver(maze) 
t1 = time.time() 

total = t1-t0 

# Print solve stats 
print ("Nodes explored: ", stats[0]) 
if (stats[2]): 
    print ("Path found, length", stats[1]) 
else: 
    print ("No Path Found") 
print ("Time elapsed: ", total, "\n") 

""" 
Create and save the output image. 
This is simple drawing code that travels between each node in turn, drawing either 
a horizontal or vertical line as required. Line colour is roughly interpolated between 
blue and red depending on how far down the path this section is. Dependency on numpy 
should be easy to remove at some point. 
""" 

print ("Saving Image") 
mazeimage = np.array(im) 
imout = np.array(mazeimage) 
imout[imout==1] = 255 
out = imout[:,:,np.newaxis] 

out = np.repeat(out, 3, axis=2) 

resultpath = [n.Position for n in result] 

length = len(resultpath) 

px = [0, 0, 0] 
for i in range(0, length - 1): 
    a = resultpath[i] 
    b = resultpath[i+1] 

    # Blue - red 
    px[0] = int((i/length) * 255) 
    px[2] = 255 - px[0] 

    if a[0] == b[0]: 
     # Ys equal - horizontal line 
     for x in range(min(a[1],b[1]), max(a[1],b[1])): 
      out[a[0],x,:] = px 
    elif a[1] == b[1]: 
     # Xs equal - vertical line 
     for y in range(min(a[0],b[0]), max(a[0],b[0]) + 1): 
      out[y,a[1],:] = px 

img = Image.fromarray(out) 
img.save(args.SaveM.png) #CHANGED 

出力:。。

pydev debugger: starting (pid: 12436) 
Traceback (most recent call last): 
    File "C:\Users\Gabriel\.p2\pool\plugins\org.python.pydev_5.5.0.201701191708\pysrc\pydevd.py", line 1537, in <module> 
Loading Image 
    globals = debugger.run(setup['file'], None, None, is_module) 
    File "C:\Users\Gabriel\.p2\pool\plugins\org.python.pydev_5.5.0.201701191708\pysrc\pydevd.py", line 976, in run 
    pydev_imports.execfile(file, globals, locals) # execute the script 
    File "C:\Users\Gabriel\.p2\pool\plugins\org.python.pydev_5.5.0.201701191708\pysrc\_pydev_imps\_pydev_execfile.py", line 25, in execfile 
    exec(compile(contents+"\n", file, 'exec'), glob, loc) 
    File "C:\Users\Gabriel\Desktop\AP Computer Science\PythonMaze\solve.py", line 21, in <module> 
    im = Image.open(args.perfect4k.png) #args.input_file 
AttributeError: 'Namespace' object has no attribute 'perfect4k' 
+0

具体的に何がエラーですか - ここでランタイム出力/スタックトレースを追加できますか? –

+0

@DannyStaple投稿を編集して出力を追加しました。これが助けて欲しいです – E36

答えて

0

Pythonの変数名を含めることはできませんドット "。"文字、これはそれのいくつかのさらなる特性を参照するために使用されるオブジェクト。

"perfect4k.png"という名前がargsの1つに使用されています。このargparseの動作は明確に定義されていません。

この代わりにアンダースコア「_」を使用することをお勧めします。ヘルプテキストにこの名前が表示されることを期待している場合は、キーワードパラメータ "metavar"を使用してみることができます。あなたはこのようにそれを追加することができ、あなたの特定のケースで

metavar - A name for the argument in usage messages.

https://docs.python.org/3/library/argparse.html#the-add-argument-method参照:):次のようにPythonのマニュアルに記載されて

parser.add_argument("input_file", metavar="perfect4k.png", help="4kImage", 
    default=None, nargs='?') #input_file 

だから、実引数は「INPUT_FILEですヘルプ出力では "perfect4k.png"と呼ばれます。

これは、コードの別の場所にinput_fileとしても参照する必要があることを意味します。出力ファイルにも同じ扱いをする必要があります。

+0

ありがとう!これで修正されました。 – E36

+0

もしそうなら、あなたはこれを答えてください。 –

関連する問題