9

我正在尝试显示.png使用以下内容构建的文件。

import pydot, StringIO
dot_data = StringIO.StringIO() 
tree.export_graphviz( clf, out_file = dot_data,    
feature_names =['age', 'sex', 'first_class', 'second_class', 'third_class'])
graph = pydot.graph_from_dot_data( dot_data.getvalue())
graph.write_png('titanic.png') 
from IPython.core.display import Image
Image( filename ='titanic.png')

我尝试了以下但既没有错误也没有.png显示:

from PIL import Image
image = Image.open("titanic.png")
image.show()
4

1 回答 1

19

如果你只想显示它,你可以使用matplotlib

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

img = mpimg.imread('file-name.png')
plt.imshow(img)
plt.show()
于 2013-12-15T17:00:01.000 回答