Exemple #1
0
 def test_size_order_bug(self):
     img_path = self.create_unique_image('size-order-bug.png')
     crop = Crop(Box(x1=160, y1=0, x2=800, y2=640), img_path)
     size = Size('650', w=650, min_h=250)
     new_crop = size.fit_to_crop(crop)
     self.assertGreaterEqual(new_crop.box.w, 650,
         "Calculated best fit (%d) didn't get required width (650)" % new_crop.box.w)
Exemple #2
0
 def test_off_by_one_bug(self):
     img_path = self.create_unique_image('best-fit-off-by-one-bug.png')
     crop = Crop(Box(x1=0, y1=0, x2=960, y2=915), img_path)
     size = Size('960', w=960, h=594)
     new_crop = size.fit_to_crop(crop)
     self.assertNotEqual(new_crop.box.h, 593, "Calculated best fit height is 1 pixel too short")
     self.assertEqual(new_crop.box.h, 594)
 def test_size_order_bug(self):
     img_path = os.path.join(self.TEST_IMG_DIR, 'size-order-bug.png')
     crop = Crop(Box(x1=160, y1=0, x2=800, y2=640), img_path)
     size = Size('650', w=650, min_h=250)
     new_crop = size.fit_to_crop(crop)
     self.assertGreaterEqual(new_crop.box.w, 650,
         "Calculated best fit (%d) didn't get required width (650)" % new_crop.box.w)
 def test_off_by_one_bug(self):
     img_path = os.path.join(self.TEST_IMG_DIR, 'best-fit-off-by-one-bug.png')
     crop = Crop(Box(x1=0, y1=0, x2=960, y2=915), img_path)
     size = Size('960', w=960, h=594)
     new_crop = size.fit_to_crop(crop)
     self.assertNotEqual(new_crop.box.h, 593, "Calculated best fit height is 1 pixel too short")
     self.assertEqual(new_crop.box.h, 594)
 def crop_size(self):
     from cropduster.resizing import Size
     size_json = self.get('size', {}).get('json') or None
     if size_json:
         return size_json
     size_w = self.get('size', {}).get('w') or None
     size_h = self.get('size', {}).get('h') or None
     if not size_w and not size_h:
         return None
     return Size('crop', w=size_w, h=size_h)
Exemple #6
0
def object_hook(dct):
    if dct.get('__type__') in ['Size', 'cropduster.resizing.Size']:
        return Size(name=dct.get('name'),
                    label=dct.get('label'),
                    w=dct.get('w'),
                    h=dct.get('h'),
                    min_w=dct.get('min_w'),
                    min_h=dct.get('min_h'),
                    max_w=dct.get('max_w'),
                    max_h=dct.get('max_h'),
                    retina=dct.get('retina'),
                    auto=dct.get('auto'),
                    required=dct.get('required'))
    return dct
Exemple #7
0
    def test_get_min_size(self):
        from cropduster.utils import get_min_size
        from cropduster.resizing import Size

        sizes = [
            Size('a', w=200, h=200),
            Size('b', w=100, h=300),
            Size('c', w=20, h=20)
        ]
        self.assertEqual(get_min_size(sizes), (200, 300))

        sizes = [
            Size('a', min_w=200, min_h=200, max_h=500),
            Size('b', min_w=100, min_h=300),
            Size('c', w=20, h=20)
        ]
        self.assertEqual(get_min_size(sizes), (200, 300))
def get_crop(image, crop_name, exact_size=False, **kwargs):
    """
    Get the crop of an image. Usage:

    {% get_crop article.image 'square_thumbnail' attribution=1 exact_size=1 as img %}

    will assign to `img` a dictionary that looks like:

    {
        "url": '/media/path/to/my.jpg',
        "width": 150,
        "height" 150,
        "attribution": 'Stock Photoz',
        "attribution_link": 'http://stockphotoz.com',
        "caption": 'Woman laughing alone with salad.',
    }

    For use in an image tag or style block like:

        <img src="{{ img.url }}">

    The `size` kwarg is deprecated.

    Omitting the `attribution` kwarg will omit the attribution, attribution_link,
    and caption.

    Omitting the `exact_size` kwarg will return the width and/or the height of
    the crop size that was passed in. Crop sizes do not always require both
    values so `exact_size` gives you access to the actual size of an image.
    """

    if not image:
        return

    if "size" in kwargs:
        warnings.warn("The size kwarg is deprecated.", DeprecationWarning)

    data = {}
    data['url'] = getattr(Image.get_file_for_size(image, crop_name), 'url', None)

    if not exact_size:
        sizes = Size.flatten(image.sizes)
        try:
            size = six.next(size_obj for size_obj in sizes if size_obj.name == crop_name)
        except StopIteration:
            pass
        else:
            if size.width:
                data['width'] = size.width

            if size.height:
                data['height'] = size.height
    elif image.related_object:
        thumbs = {thumb.name: thumb for thumb in image.related_object.thumbs.all()}

        try:
            thumb = thumbs[crop_name]
        except KeyError:
            if crop_name == "original":
                thumb = image.related_object
            else:
                return None

        cache_buster = base64.b32encode(str(time.mktime(thumb.date_modified.timetuple())))
        data.update({
            "url": "%s?%s" % (data["url"], cache_buster),
            "width": thumb.width,
            "height": thumb.height,
            "attribution": image.related_object.attribution,
            "attribution_link": image.related_object.attribution_link,
            "caption": image.related_object.caption,
        })

    return data