예제 #1
0
파일: jp2iser.py 프로젝트: tomcrane/jp2iser
def make_derivatives(jp2path, bound_sizes):
    # bound_sizes should be a list of ints (square confinement)
    # make the first one from kdu_expand, then use Pillow to resize further
    # Note that Pillow's im.thumbnail function is really fast but doesn't
    # make very good quality thumbs; the PIL.Image.ANTIALIAS option
    # gives better results.

    # This could also be multi-threaded - BUT better for this process (jp2iser)
    # to be running on multiple threads and control it that way
    # i.e., a machine is processing this off the queue in parallel.
    print 'making derivatives'
    jp2 = Jp2Info.from_jp2_file(jp2path)
    head, filename, namepart, extension = path_parts(jp2path)
    prefix = os.path.join(head, namepart)
    im = None
    for size in sorted(bound_sizes, reverse=True):
        if im is None:
            im = get_reduced_image_from_kdu(jp2, size)
        else:
            req_w, req_h = confine(jp2.width, jp2.height, size, size)
            im = im.resize((req_w, req_h), resample=Image.ANTIALIAS)

        jpg = prefix + str(size) + '.jpg'
        print 'saving', jpg
        im.save(jpg, quality=90)
예제 #2
0
def process(filepath, destination=None, bounded_sizes=list(), bounded_folder=None, optimisation="kdu_med", jpeg_info_id="ID"):
    # Convert image file into tile-optimised JP2 and optionally additional derivatives
    start = time.clock()
    result = {}

    head, filename, namepart, extension = path_parts(filepath)
    print '%s  -- [%s] - %s.%s' % (head, filename, namepart, extension)

    print 'destination: %s' % destination
    jp2path = destination or os.path.join(OUTPUT_DIR, namepart + '.jp2')
    print 'Converting: ', filename
    print 'We want to make a JP2 at: ', jp2path

    if optimisation not in CMD_COMPRESS:
        optimisation = "kdu_med"

    if is_tile_optimised_jp2(filepath, extension):
        print filename, 'is already optimised for tiles, proceeding to next stage'
        shutil.copyfile(filepath, jp2path)
    else:
        kdu_ready, image_mode = get_kdu_ready_file(filepath, extension)
        make_jp2_from_image(kdu_ready, jp2path, optimisation, image_mode)
        result["jp2"] = jp2path
        if filepath != kdu_ready:
            # TODO - do this properly
            print 'removing', kdu_ready, 'as it was a temporary file'
            os.remove(kdu_ready)

    jp2_data = Jp2Info.from_jp2_file(jp2path)
    jp2_info_template = open('jp2info.mustache').read()

    jp2_info = pystache.render(jp2_info_template, {
        "id": jpeg_info_id,
        "height": jp2_data.height,
        "width": jp2_data.width,
        "scale_factors": ",".join(map(str, get_scale_factors(jp2_data.width, jp2_data.height)))
    })

    if len(bounded_sizes) > 0:
        make_derivatives(jp2_data, result, jp2path, bounded_sizes, bounded_folder)

    elapsed = time.clock() - start
    print 'operation time', elapsed
    result["clockTime"] = int(elapsed * 1000)
    result["optimisation"] = optimisation
    result["jp2Info"] = base64.b64encode(jp2_info.encode('utf-8'))
    result["width"] = jp2_data.width
    result["height"] = jp2_data.height
    return result
예제 #3
0
def process(filepath, destination=None, bounded_sizes=list(), bounded_folder=None, optimisation="kdu_med",
            jpeg_info_id="ID", operation="ingest"):

    warnings.simplefilter('ignore', Image.DecompressionBombWarning)
    Image.MAX_IMAGE_PIXELS = None

    # Convert image file into tile-optimised JP2 and optionally additional derivatives
    start = time.clock()
    result = {}

    if operation not in ['ingest', 'derivatives-only']:
        result["status"] = "error"
        result["message"] = "Unknown operation"
        get_elapsed(start)
        return result

    head, filename, namepart, extension = path_parts(filepath)
    print '%s  -- [%s] - %s.%s' % (head, filename, namepart, extension)

    if operation == "derivatives-only" and extension != "jp2":
        result["status"] = "error"
        result["message"] = "File type must be jpeg2000 for derivatives-only operation"
        get_elapsed(start)
        return result

    if operation == "ingest":

        print 'destination: %s' % destination
        jp2path = destination or os.path.join(OUTPUT_DIR, namepart + '.jp2')
        print 'Converting: ', filename
        print 'We want to make a JP2 at: ', jp2path

        if optimisation not in CMD_COMPRESS:
            optimisation = "kdu_med"

        result["jp2"] = jp2path

        if is_tile_optimised_jp2(filepath, extension):
            print filename, 'is already optimised for tiles, proceeding to next stage'
            shutil.copyfile(filepath, jp2path)
        else:
            kdu_ready, image_mode = get_kdu_ready_file(filepath, extension)
            make_jp2_from_image(kdu_ready, jp2path, optimisation, image_mode)
            if filepath != kdu_ready:
                # TODO - do this properly
                print 'removing', kdu_ready, 'as it was a temporary file'
                os.remove(kdu_ready)

    else:  # operation == "derivatives-only":

        # output path is input path
        jp2path = filepath

    jp2_data = Jp2Info.from_jp2_file(jp2path)
    jp2_info = get_jp2_info(jp2_data, jpeg_info_id)

    if len(bounded_sizes) > 0:
        make_derivatives(jp2_data, result, jp2path, bounded_sizes, bounded_folder)

    result["status"] = "complete"
    result["clockTime"] = get_elapsed(start)
    result["optimisation"] = optimisation
    result["infoJson"] = base64.b64encode(jp2_info.encode('utf-8'))
    result["width"] = jp2_data.width
    result["height"] = jp2_data.height
    return result