Example #1
0
    def test_crop_bbox(self):
        expected_square = (300, 300)
        transmog = Transmogrify(self.square_img, [("c", "100-100-400-400")])
        transmog.save()
        img = Image.open(transmog.get_processed_filename())
        self.assertEqual(expected_square, img.size)

        transmog = Transmogrify(self.vert_img, [("c", "0-100-300-400")])
        transmog.save()
        img = Image.open(transmog.get_processed_filename())
        self.assertEqual(expected_square, img.size)

        transmog = Transmogrify(self.horiz_img, [("c", "0-410-300-710")])
        transmog.save()
        img = Image.open(transmog.get_processed_filename())
        self.assertEqual(expected_square, img.size)

        # 810 is larger than the image, PIL adds black to the extra space.
        # who knows if this is desirable but at least it doesn't raise
        # an exception.
        transmog = Transmogrify(self.horiz_img, [("c", "0-510-300-810")])
        transmog.save()
        img = Image.open(transmog.get_processed_filename())
        self.assertEqual(expected_square, img.size)

        transmog = Transmogrify(self.animated, [("c", "0-410-300-710")])
        transmog.save()
        img = Image.open(transmog.get_processed_filename())
        self.assertEqual(expected_square, img.size)
Example #2
0
 def testCrop(self):
     expected_square = (300,300)
     transmog = Transmogrify(self.square_img, [('c', '300x300'),])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_square, img.size)
     transmog = Transmogrify(self.vert_img, [('c', '300x300'),])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_square, img.size)
     transmog = Transmogrify(self.horiz_img, [('c', '300x300'),])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_square, img.size)
Example #3
0
 def testResize(self):
     expected_square = (300,300)
     expected_vert = (168,300)
     expected_horiz = (300,208)
     transmog = Transmogrify(self.square_img, [('r', '300x300'),])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_square, img.size)
     transmog = Transmogrify(self.vert_img, [('r', '300x300'),])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_vert, img.size)
     transmog = Transmogrify(self.horiz_img, [('r', '300x300'),])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_horiz, img.size)
Example #4
0
 def test_crop(self):
     expected_square = (300, 300)
     transmog = Transmogrify(self.square_img, [("c", "300x300")])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_square, img.size)
     transmog = Transmogrify(self.vert_img, [("c", "300x300")])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_square, img.size)
     transmog = Transmogrify(self.horiz_img, [("c", "300x300")])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_square, img.size)
     transmog = Transmogrify(self.animated, [("c", "300x300")])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_square, img.size)
Example #5
0
 def test_thumbnail(self):
     expected_square = (300, 300)
     expected_vert = (168, 300)
     expected_horiz = (300, 208)
     expected_animated = (300, 214)
     transmog = Transmogrify(self.square_img, [("r", "300x300")])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_square, img.size)
     transmog = Transmogrify(self.vert_img, [("r", "300x300")])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_vert, img.size)
     transmog = Transmogrify(self.horiz_img, [("r", "300x300")])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_horiz, img.size)
     transmog = Transmogrify(self.animated, [("r", "300x300")])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_animated, img.size)
Example #6
0
def app(environ, start_response):
    from settings import DEBUG

    cropname = None
    server = environ['SERVER_NAME']
    quality = 80

    request_uri = get_path(environ)
    path_and_query = request_uri.lstrip('/')
    if path_and_query is "":
        return do_404(environ, start_response, "Not Found", DEBUG)

    if environ.get('REQUEST_METHOD', 'GET') == 'PURGE':
        return handle_purge(environ, start_response)

    # Acquire lockfile
    lock = '/tmp/%s' % sha1(path_and_query).hexdigest()
    if os.path.isfile(lock):
        return do_redirect(environ, start_response, request_uri)

    with lock_file(lock):
        try:
            url_parts = process_url(path_and_query, server)
            output_path, _ = os.path.split(url_parts['requested_file'])
            makedirs(output_path)
            if not os.path.exists(url_parts['original_file']):
                raise Http404
            if not os.path.isfile(url_parts['original_file']):
                raise Http404
        except Http404 as e:
            return do_404(environ, start_response, e.message, DEBUG)

        new_file = Transmogrify(
            url_parts['original_file'],
            url_parts['actions'],
            quality=quality,
            output_path=output_path
        )
        new_file.cropname = cropname
        new_file.save()

        if cropname:
            # Rewrite the request_uri to use the new file with the
            # cropname
            urlbits = list(urlparse.urlsplit(request_uri))
            output_filename = new_file.get_processed_filename()
            filename = os.path.basename(output_filename)
            requested_dir = os.path.dirname(urlbits[2])
            urlbits[2] = os.path.join(requested_dir, filename)
            request_uri = urlparse.urlunsplit(urlbits)

        return do_redirect(environ, start_response, request_uri)
Example #7
0
    def test_cropname(self):
        transmog = Transmogrify(self.horiz_img, [("c", "0-510-300-810")])
        transmog.cropname = "cropped"

        self.assertEqual(transmog.get_processed_filename(), self.cropname_img)
def app(environ, start_response):
    cropname = None
    server = environ['SERVER_NAME']
    quality = 80

    if "path=" in environ.get("QUERY_STRING", ""):
        # I should probably require a POST for this, but meh, let's not
        # rock the boat.

        # transmogrify is being used directly and not as a 404 handler
        query_dict = urlparse.parse_qs(environ['QUERY_STRING'])

        path = query_dict.get("path", [""])[0]
        key = query_dict.get("key", [""])[0]

        # validate the query params
        if not (path and key):
            # The required parameters were not given
            start_response("400 Bad Response",
                           [("Content-Type", "text/plain")])
            return ["path and key are required query parameters"]

        cropname = query_dict.get("cropname", [None])[0]
        quality = 100

        # rewrite the environ to look like a 404 handler
        environ['REQUEST_URI'] = path + "?" + key


    request_uri = environ['REQUEST_URI']
    path_and_query = request_uri.lstrip("/")
    requested_path = urlparse.urlparse(path_and_query).path

    if path_and_query is "":
        return do404(environ, start_response, "Not Found", DEBUG)

    # Acquire lockfile
    lock = '/tmp/%s' % sha_constructor(path_and_query).hexdigest()

    if os.path.isfile(lock):
        return doRedirect(environ, start_response, request_uri)

    with lock_file(lock):

        if FALLBACK_SERVERS:
            result = do_fallback(FALLBACK_SERVERS, BASE_PATH, requested_path)
            if result == (False, "bad path"):
                start_response("403 Forbidden", [])
                return []

        try:
            url_parts = process_url(path_and_query, server)
        except Http404, e:
            return do404(environ, start_response, e.message, DEBUG)

        new_file = Transmogrify(
            url_parts['original_file'],
            url_parts['actions'],
            quality=quality
        )
        new_file.cropname = cropname
        new_file.save()

        if cropname:
            # Rewrite the request_uri to use the new file with the
            # cropname
            urlbits = list(urlparse.urlsplit(request_uri))
            output_filename = new_file.get_processed_filename()
            filename = os.path.basename(output_filename)
            requested_dir = os.path.dirname(urlbits[2])
            urlbits[2] = os.path.join(requested_dir, filename)
            request_uri = urlparse.urlunsplit(urlbits)

        return doRedirect(environ, start_response, request_uri)
Example #9
0
    def testCrapname(self):
        transmog = Transmogrify(self.horiz_img, [('c', '0-510-300-810'),])
        transmog.cropname = "cropped"

        self.assertEqual(transmog.get_processed_filename(),
                         self.cropname_img)