Example #1
0
File: utils.py Project: eJon/tiler
def scale_and_crop(path,
                   requested_size,
                   row,
                   col,
                   zoom,
                   image,
                   cache_image_open=False):
    im = Image.open(path)
    x, y = [float(v) for v in im.size]
    xr, yr = [float(v) for v in requested_size]
    r = min(xr / x, yr / y)

    box = (256 * row, 256 * col, 256 * (row + 1), 256 * (col + 1))

    w, h = int(round(x * r)), int(round(y * r))
    _cache_key = '%s-%s-%s' % (image, zoom, w)
    pathname, extension = os.path.splitext(path)

    width = 256 * (2**zoom)
    _resized_file = path.replace(extension,
                                 '-%s-%s%s' % (zoom, width, extension))

    if not os.path.isfile(_resized_file):
        # resizer.make_resize() uses `convert` so it's much more memory
        # efficient
        print "Having to use make_resize()"
        t0 = time.time()
        _resized_file = make_resize(path, zoom)
        t1 = time.time()
        print "\ttook", round(t1 - t0, 2), "seconds"
        time.sleep(0.5)  # time to save it

    if cache_image_open:
        if _cache_key in _RESIZES:
            im = _RESIZES[_cache_key]
        else:
            im = Image.open(_resized_file)
            print "Having to use Image.open()"
            _RESIZES[_cache_key] = im
            _TIMESTAMPS[_cache_key] = time.time()

        # to avoid memory bloat of `_RESIZES` getting too large, instead use the
        # `_TIMESTAMPS` dict to keep track of which Image instances are getting old
        _now = time.time()
        for key, timestamp in _TIMESTAMPS.items():
            age = _now - timestamp
            if age > 10:
                print "CLEAR", key
                del _TIMESTAMPS[key]
                del _RESIZES[key]
    else:
        im = Image.open(_resized_file)

    # convert (width, height, x, y) into PIL crop box
    return im.crop(box)
Example #2
0
File: utils.py Project: eJon/tiler
def scale_and_crop(path, requested_size, row, col, zoom, image,
                   cache_image_open=False):
    im = Image.open(path)
    x, y = [float(v) for v in im.size]
    xr, yr = [float(v) for v in requested_size]
    r = min(xr / x, yr / y)

    box = (256 * row, 256 * col, 256 * (row + 1), 256 * (col + 1))

    w, h = int(round(x * r)), int(round(y * r))
    _cache_key = '%s-%s-%s' % (image, zoom, w)
    pathname, extension = os.path.splitext(path)

    width = 256 * (2 ** zoom)
    _resized_file = path.replace(
        extension,
        '-%s-%s%s' % (zoom, width, extension)
    )

    if not os.path.isfile(_resized_file):
        # resizer.make_resize() uses `convert` so it's much more memory
        # efficient
        print "Having to use make_resize()"
        t0 = time.time()
        _resized_file = make_resize(path, zoom)
        t1 = time.time()
        print "\ttook", round(t1 - t0, 2), "seconds"
        time.sleep(0.5)  # time to save it


    if cache_image_open:
        if _cache_key in _RESIZES:
            im = _RESIZES[_cache_key]
        else:
            im = Image.open(_resized_file)
            print "Having to use Image.open()"
            _RESIZES[_cache_key] = im
            _TIMESTAMPS[_cache_key] = time.time()

        # to avoid memory bloat of `_RESIZES` getting too large, instead use the
        # `_TIMESTAMPS` dict to keep track of which Image instances are getting old
        _now = time.time()
        for key, timestamp in _TIMESTAMPS.items():
            age = _now - timestamp
            if age > 10:
                print "CLEAR", key
                del _TIMESTAMPS[key]
                del _RESIZES[key]
    else:
        im = Image.open(_resized_file)

    # convert (width, height, x, y) into PIL crop box
    return im.crop(box)
Example #3
0
def scale_and_crop(path, requested_size, row, col, zoom, image):
    im = Image.open(path)
    x, y = [float(v) for v in im.size]
    xr, yr = [float(v) for v in requested_size]
    r = min(xr / x, yr / y)

    box = (256 * row, 256 * col, 256 * (row + 1), 256 * (col + 1))

    w, h = int(round(x * r)), int(round(y * r))
    _cache_key = '%s-%s-%s' % (image, zoom, w)
    pathname, extension = os.path.splitext(path)

    _resized_file = path.replace(
        extension,
        '-%s-%s%s' % (zoom, w, extension)
    )

    already = _RESIZES.get(_cache_key)
    if already:
        im = already
    else:
        if not os.path.isfile(_resized_file):
            # resizer.make_resize() uses `convert` so it's much more memory
            # efficient
            print "Having to use make_resize()"
            t0 = time.time()
            make_resize(path, zoom)
            t1 = time.time()
            print "\ttook", round(t1 - t0, 2), "seconds"
            time.sleep(1)  # time to save it

        if (os.path.isfile(_resized_file) and
            os.stat(_resized_file)[stat.ST_SIZE]):
            print "REUSING", _resized_file
            logging.debug('REUSING %s' % _resized_file)
            im = Image.open(_resized_file)
            #print "Assert?", (im.size, (w,h))
        else:
            print "Need to resize... FAIL!"
            t0 = time.time()

            im = im.resize((w, h),
                           resample=Image.ANTIALIAS)
            t1 = time.time()
            logging.debug("SAVE RESIZED TO %s" % _resized_file)
            im.save(_resized_file)
            print "SAVE RESIZED TO", _resized_file
            print "\t", round(t1 - t0, 2), "seconds to resize"

    _RESIZES[_cache_key] = im
    _TIMESTAMPS[_cache_key] = time.time()

    # to avoid memory bloat of `_RESIZES` getting too large, instead use the
    # `_TIMESTAMPS` dict to keep track of which Image instances are getting old
    _now = time.time()
    for key, timestamp in _TIMESTAMPS.items():
        age = _now - timestamp
        if age > 10:
            del _TIMESTAMPS[key]
            del _RESIZES[key]

    # convert (width, height, x, y) into PIL crop box
    return im.crop(box)