Ejemplo n.º 1
0
def index():
    """
        index accepts GET or POST at '/'

        GET parameters:
            url: the URL for an image
        POST parameters:
            image: an uploaded image file

        returns:
            jsonified data with 3 or 5 hex colors
    """
    app.logger.info('Request received at /')
    if request.method == 'GET':
        app.logger.debug('GET request')
        url = request.args.get('url', False)
        if not url:
            raise BadRequest('Missing url parameter.')
        app.logger.info('Image URL: %s', url)
        r = requests.get(url)
        if r.status_code != 200:
            raise BadRequest('Invalid image URL: %s', url)
        im = colorific.load_image(data=r.content)
    else:
        # handles an image upload
        f = request.files['image']
        im = colorific.load_image(data=f.stream)
    # extract colors
    colors = colorific.extract_colors(im)
    return jsonify({'colors': colors})
Ejemplo n.º 2
0
def test():
    """Shows the test image palettes"""
    from test import TestPhotoColors, BASE_DIR
    images = []
    for img in TestPhotoColors.TEST_PHOTOS:
        path = os.path.join(BASE_DIR, 'static/photos/', img)
        im = colorific.load_image(path=path)
        colors = colorific.extract_colors(im)
        images.append({'url': '/static/photos/%s' % img, 'colors': colors})
    return render_template('test.html', images=images)
Ejemplo n.º 3
0
 def test_extract_colors(self):
     for p in self.TEST_PHOTOS:
         im = colorific.load_image(path=self._imgpath(p))
         colors = colorific.extract_colors(im)
         self.assertTrue(colors)
Ejemplo n.º 4
0
 def test_load_image(self):
     im = colorific.load_image(path=self._imgpath('lenna.jpg'))
     self.assertTrue(im)