2

我一直在尝试使用 matplotLib 基本上使用用户选择的图像制作像素直方图。用户使用 JCROP 选择图像的一部分,然后当他/她提交按钮时,PlotsHandlers,post 方法获取参数。然后我裁剪它并尝试使用此处描述的方法显示直方图来绘制它。我正在通过部署它进行测试,但它不起作用。有人可以指导我如何在 GAE 中使用 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)
4

1 回答 1

1

解决了这个问题。忘记在 app.yaml 中将 threadsafe 设置为 false。

于 2013-02-27T06:55:59.493 回答