2013-02-26 9 views
2

저는 matplotLib를 사용하여 기본적으로 사용자가 선택한 이미지를 사용하여 픽셀 막대 그래프를 만들려고했습니다. 사용자가 JCROP를 사용하여 이미지의 일부를 선택한 다음 제출 버튼 인 The PlotsHandlers, post 메서드가 매개 변수를 가져옵니다. 그 다음 그것을 잘라 내고 method described here을 사용하여 히스토그램을 표시하려고 시도합니다. 나는 그것을 배치함으로써 테스트를하고 있지만, 단지 작동하지 않습니다. 누군가 GAE에서 matplotlib을 사용하여 히스토그램 (또는 다른 데이터)을 플롯하는 방법을 안내해 줄 수 있습니까?Google App Engine에서 Matplotlib를 사용하는 히스토그램

import os 
import webapp2 
import re 
import jinja2 
import Image 
import ImageOps 
import logging 
import numpy as np 
import matplotlib.pyplot as plt 

template_dir = os.path.join(os.path.dirname(__file__), 'templates') 
jinja_env= jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape = True) 
PAGE_RE = r'(/(?:[a-zA-Z0-9_-]+/?)*)' 


def render_str(template, **params): 
    t = jinja_env.get_template(template) 
    return t.render(params) 

#Opens the image and Grayscales it 
def openAndGrayScaleImage(imageLocationString): 
    OpenedImage = Image.open(imageLocationString) 
    return ImageOps.grayscale(OpenedImage) 

def cropImage(image, x1, y1, x2, y2): 
    box = (x1, y1, x2, y2) 
    return image.crop(box) 



class BaseHandler(webapp2.RequestHandler): 
    def render(self, template, **kw): 
     self.response.out.write(render_str(template, **kw)) 

    def write(self, *a, **kw): 
     self.response.out.write(*a, **kw) 


class MainHandler(BaseHandler): 
    def get(self): 
     self.render("mainPage.html") 

class PlotsHandler(BaseHandler): 
    def get(self): 
     self.redirect('/') 



def post(self): 
     image1 = openAndGrayScaleImage("images.jpg") 
     #Parameters for cropping. Verified that it's working 
      x1 = int(self.request.get('x1')) 
     y1 = int(self.request.get('y1')) 
     x2 = int(self.request.get('x2')) 
     y2 = int(self.request.get('y2')) 
     image2 = cropImage(image1, x1, y1, x2, y2) 
     #Get the sequence of pixels 
      values_= list(image2.getdata()) 
     n, bins, patches = plt.hist(values_, 256/2, normed=1, facecolor ='blue', alpha = .75) 
     plt.xlabel('Light Intensity') 
     plt.ylabel('Probability') 
     plt.title('Normalized distribution of light') 
     plt.axis([0, 255, 0, .3]) 
     plt.grid(True) 
     rv = StringIO.StringIO() 
     plt.savefig(rv, format = "png") 
     imgb64 = rv.getvalue().encode("base64").strip() 
     plt.clf() 
     rv.close() 
     self.response.write("""<html><body>""") 
     self.response.write("<img src='data:image/png;base64,%s'/>" % img_b64) 
     self.response.write("""</body> </html>""") 







app = webapp2.WSGIApplication([('/', MainHandler), 
           ('/plots', PlotsHandler)], 
           debug=True) 

답변

1

문제가 해결되었습니다. app.yaml에서 threadsafe를 false로 설정하는 것을 잊었습니다.

+0

threadsafe 설정은 플로팅 루틴에 어떤 영향을 줍니까? –

+1

Matplotlib은 스레드를 사용합니다 (matplotlib가 필요한 이미지 처리는 많은 스레드 작업이 필요하다는 사실을 알고 있습니다). 또한 [3 부 라이브러리]의 하단 (https://developers.google.com/appengine/docs/python/tools/libraries27)을 방문하면 threadsafe 값을 false로 설정하라는 메시지가 표시됩니다. – piyushg91

관련 문제