matplotlib를 터미널 환경에서 사용하기

집에 있는 서버에 tensorflow가 설치되어 있는데, ssh로 접속하여 실행한다. 결과물이 텍스트같이 터미널로 표시되면 볼수 있는데 그림으로 표시되면 터미널에 표시가 안된다.

Traceback (most recent call last):
  File "imagetran.py", line 13, in <module>
    plt.imshow(image)
  File "/home/now0930/anaconda2/envs/tensorflow/lib/python2.7/site-packages/matplotlib/pyplot.py", line 3010, in imshow
    ax = gca()
  File "/home/now0930/anaconda2/envs/tensorflow/lib/python2.7/site-packages/matplotlib/pyplot.py", line 928, in gca
    return gcf().gca(**kwargs)
  File "/home/now0930/anaconda2/envs/tensorflow/lib/python2.7/site-packages/matplotlib/pyplot.py", line 578, in gcf
    return figure()
  File "/home/now0930/anaconda2/envs/tensorflow/lib/python2.7/site-packages/matplotlib/pyplot.py", line 527, in figure
    **kwargs)
  File "/home/now0930/anaconda2/envs/tensorflow/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py", line 46, in new_figure_manager
    return new_figure_manager_given_figure(num, thisFig)
  File "/home/now0930/anaconda2/envs/tensorflow/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py", line 53, in new_figure_manager_given_figure
    canvas = FigureCanvasQTAgg(figure)
  File "/home/now0930/anaconda2/envs/tensorflow/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py", line 76, in __init__
    FigureCanvasQT.__init__(self, figure)
  File "/home/now0930/anaconda2/envs/tensorflow/lib/python2.7/site-packages/matplotlib/backends/backend_qt4.py", line 68, in __init__
    _create_qApp()
  File "/home/now0930/anaconda2/envs/tensorflow/lib/python2.7/site-packages/matplotlib/backends/backend_qt5.py", line 138, in _create_qApp
    raise RuntimeError('Invalid DISPLAY variable')
RuntimeError: Invalid DISPLAY variable

마지막에 Invalid DISPLAY variable이라고 뜨고 죽는데 아래와 같이 변경하면 된다.

import matplotlib
matplotlib.use('Agg')

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

# First, load the image
filename = "./MarshOrchid.jpg"
image = mpimg.imread(filename)

# Print out its shape
#print(image.shape)


plt.imshow(image)
plt.savefig('test.jpg')


# Create a TensorFlow Variable
x = tf.Variable(image, name='x')
model = tf.initialize_all_variables()
with tf.Session() as session:
	x = tf.transpose(x, perm=[1, 0, 2])
	session.run(model)
	result = session.run(x)
print(result.shape)
mpimg.imsave('test.jpg',image)
mpimg.imsave('test.jpg',result)

디스플레이가 있는 PC에서 다운로드해 보면 된다..이게 웹으로 보내면 좋은데, 아직 그런게는 없나보다. stack overflow의 여기에서 답을 얻었다.

질문은..
Generating a PNG with matplotlib when DISPLAY is undefined

시스템에 따라 설정파일을 바꾸면 귀찮게 안써도 되는데, 나의 경우 어디에 설정파일이 있는지 몰라 그냥 써야겠다.

코멘트

댓글 남기기

이 사이트는 Akismet을 사용하여 스팸을 줄입니다. 댓글 데이터가 어떻게 처리되는지 알아보세요.