Exemple #1
0
 def post(self):
      img = Image(self.request.get('file'))
               
      img.vertical_flip()
      vertical = img.execute_transforms()
      
      self.render(image=vertical)
Exemple #2
0
    def get(self,
            flip_horizontal,
            width,
            flip_vertical,
            height,
            halign,
            valign,
            url):
        if not url:
            self._error(400, 'The url argument is mandatory!')
            return

        if not width and not height:
            self._error(400, 'Either width or height are mandatory!')

        url = join('http://', url)

        if not self._verify_allowed_domains():
            self._error(404, 'Your domain is not allowed!')
            return

        if not self._verify_allowed_sources(url):
            self._error(404, 'Your image source is not allowed!')
            return

        width = width and int(width) or 0
        height = height and int(height) or 0

        if width > MAX_WIDTH:
            width = MAX_WIDTH
        if height > MAX_HEIGHT:
            height = MAX_HEIGHT

        if not halign:
            halign = "center"
        if not valign:
            valign = "middle"

        key = "%d_%d_%s_%s_%s" % (
                width,
                height,
                halign,
                valign,
                url
        )

        extension = splitext(url)[-1]
        image_format = extension in ('.jpg', '.jpeg') and JPEG or PNG

        data = memcache.get(key)

        self.response.headers['Cache-Key'] = key
        if data is not None:
            results = data
            self.response.headers['Cache-Hit'] = 'True'
        else:
            query = "SELECT * FROM Picture WHERE url = :1 LIMIT 1"
            pictures = db.GqlQuery(query, url).fetch(1)
            
            try:
                if len(pictures) > 0: 
                    picture = pictures[0]
                    if picture.is_expired():
                        img = picture.fetch_image()
                        picture.put()
                    else:
                        img = Image(picture.picture)
                else:
                    picture = Picture()
                    picture.url = url
                    img = picture.fetch_image()
                    picture.put()
            except ImageNotFoundError:
                self._error(404, 'Your image source is not found!')
                return

            if float(width) / float(img.width) > float(height) / float(img.height):
                img.resize(width=width)
                image_width = width
                image_height = float(width) / float(img.width) * float(img.height)
            else:
                img.resize(height=height)
                image_width = float(height) / float(img.height) * float(img.width)
                image_height = height

            rect = BoundingRect(height=image_height, width=image_width)
            rect.set_size(height=height, width=width, halign=halign, valign=valign)

            if not width:
                width = rect.target_width
            if not height:
                height = rect.target_height

            img.crop(left_x=rect.left,
                     top_y=rect.top,
                     right_x=rect.right,
                     bottom_y=rect.bottom)

            if flip_horizontal:
                img.horizontal_flip()
            if flip_vertical:
                img.vertical_flip()

            results = img.execute_transforms(output_encoding=image_format, quality=QUALITY)

            memcache.set(key=key,
                     value=results,
                     time=EXPIRATION) # ONE MONTH


            self.response.headers['Cache-Hit'] = 'False'

        self.response.headers['Content-Type'] = image_format == JPEG and 'image/jpeg' or 'image/png'
        self.response.out.write(results)
Exemple #3
0
def fetchAndProcessImage(imgurl):
    logging.info('Image URL: %s', imgurl)
    imgurl = imgurl.replace('dynamic', 'archive').replace('e.jpg', 'a.jpg')
    logging.info('Image URL enhanced: %s', imgurl)
    imgdata = urllib2.urlopen(imgurl).read()
    im = Image(imgdata)
    dimx = im.width
    dimy = im.height
    logging.info('Width: %s, Height: %s', dimx, dimy)

    im.vertical_flip()
    im.vertical_flip()
    temporary = im.execute_transforms(output_encoding=PNG, quality=100)
    test = png.Reader(file=StringIO.StringIO(temporary))
    img = list(test.asRGBA()[2])

    length = 15

    # Confirm the right and bottom boundaries
    rightmost = 0
    for x in range(0, dimx):
        clr = getpixel(img, x, 5)
        clr = quantizecolor(clr)
        if clr != 255:
            rightmost = x

    logging.info('Rightmost non-white pixel %s', rightmost)
    dimx = rightmost

    bottommost = 0
    for y in range(0, dimx):
        clr = getpixel(img, 5, y)
        clr = quantizecolor(clr)
        if clr != 255:
            bottommost = y

    logging.info('Bottommost non-white pixel %s', bottommost)
    dimy = bottommost

    logging.info('---------------')

    boxx = dimx*1.0 / length
    boxy = dimy*1.0 / length

    arr = {}
    across = {}
    down = {}
    startlist = {}
    for i in range(0, length):
        arr[i] = {}
        across[i] = {}
        down[i] = {}

    for i in range(0, length):
        for j in range(0, length):
            x = boxx * (i + 0.5) + 0.5
            y = boxy * (j + 0.5) + 0.5
            r = getpixel(img, x, y)
            clr = quantizecolor(r)
            if (clr == 255):
                arr[j][i] = 1
            else:
                arr[j][i] = 0

    matrix = []
    for i in range(0, length):
        matrix.append([])
        for j in range(0, length):
            down[i][j] = across[i][j] = 0
            if arr[i][j] == 1:
                down[i][j] = across[i][j] = 1
                matrix[i].append(1)
                if i > 0:
                    down[i][j] = down[i-1][j] + 1
                    if down[i][j] == 2:
                        if startlist.get((i-1, j)):
                            startlist[(i-1, j)] += 1
                        else:
                            startlist[(i-1, j)] = 1
                if j > 0:
                    across[i][j] = across[i][j-1] + 1
                    if across[i][j] == 2:
                        if startlist.get((i, j-1)):
                            startlist[(i, j-1)] += 2
                        else:
                            startlist[(i, j-1)] = 2
            else:
                matrix[i].append(0)

    starts = startlist.keys()
    starts.sort()
    startpos = []

    for startposn in starts:
        startpos.append([startposn[0], startposn[1], startlist[startposn]])

    return matrix, startpos
Exemple #4
0
def fetchAndProcessImage(imgurl):
    logging.info('Image URL: %s', imgurl)
    imgurl = imgurl.replace('dynamic', 'archive').replace('e.jpg', 'a.jpg')
    logging.info('Image URL enhanced: %s', imgurl)
    imgdata = urllib2.urlopen(imgurl).read()
    im = Image(imgdata)
    dimx = im.width
    dimy = im.height
    logging.info('Width: %s, Height: %s', dimx, dimy)

    im.vertical_flip()
    im.vertical_flip()
    temporary = im.execute_transforms(output_encoding=PNG, quality=100)
    test = png.Reader(file=StringIO.StringIO(temporary))
    img = list(test.asRGBA()[2])

    length = 15

    # Confirm the right and bottom boundaries
    rightmost = 0
    for x in range(0, dimx):
        clr = getpixel(img, x, 5)
        clr = quantizecolor(clr)
        if clr != 255:
            rightmost = x

    logging.info('Rightmost non-white pixel %s', rightmost)
    dimx = rightmost

    bottommost = 0
    for y in range(0, dimx):
        clr = getpixel(img, 5, y)
        clr = quantizecolor(clr)
        if clr != 255:
            bottommost = y

    logging.info('Bottommost non-white pixel %s', bottommost)
    dimy = bottommost

    logging.info('---------------')

    boxx = dimx * 1.0 / length
    boxy = dimy * 1.0 / length

    arr = {}
    across = {}
    down = {}
    startlist = {}
    for i in range(0, length):
        arr[i] = {}
        across[i] = {}
        down[i] = {}

    for i in range(0, length):
        for j in range(0, length):
            x = boxx * (i + 0.5) + 0.5
            y = boxy * (j + 0.5) + 0.5
            r = getpixel(img, x, y)
            clr = quantizecolor(r)
            if (clr == 255):
                arr[j][i] = 1
            else:
                arr[j][i] = 0

    matrix = []
    for i in range(0, length):
        matrix.append([])
        for j in range(0, length):
            down[i][j] = across[i][j] = 0
            if arr[i][j] == 1:
                down[i][j] = across[i][j] = 1
                matrix[i].append(1)
                if i > 0:
                    down[i][j] = down[i - 1][j] + 1
                    if down[i][j] == 2:
                        if startlist.get((i - 1, j)):
                            startlist[(i - 1, j)] += 1
                        else:
                            startlist[(i - 1, j)] = 1
                if j > 0:
                    across[i][j] = across[i][j - 1] + 1
                    if across[i][j] == 2:
                        if startlist.get((i, j - 1)):
                            startlist[(i, j - 1)] += 2
                        else:
                            startlist[(i, j - 1)] = 2
            else:
                matrix[i].append(0)

    starts = startlist.keys()
    starts.sort()
    startpos = []

    for startposn in starts:
        startpos.append([startposn[0], startposn[1], startlist[startposn]])

    return matrix, startpos