Exemplo n.º 1
0
def extract(params):
    key = Key.open(params['key'])
    stego_image = Image.open(params['stego'])

    with benchmark("extracted in"):
        secret_image = QtarStego.extract(stego_image, key)

    if params['wm_path']:
        si_path = params['wm_path']
    else:
        stego_image_file_name = extract_filename(params['stego'])
        si_path = 'extracted_from_%s.png' % stego_image_file_name

    save_file(secret_image, si_path)
Exemplo n.º 2
0
def save_de_results(file, def_params, new_params, def_metrics, new_metrics):
    try:
        workbook = openpyxl.load_workbook(file, guess_types=True)
    except:
        workbook = openpyxl.Workbook()
        workbook.guess_types = True

    issue = def_params["issue"]

    sheet_name = "issue " + str(issue) + " " +\
                 ('pm' if def_params['pm_mode'] else '') +\
                 ('cf' if def_params['cf_mode'] else '') +\
                 ('wmdct' if def_params['wmdct_mode'] else '')

    def_params = prepare_params(def_params)
    new_params = prepare_params(new_params)
    def_metrics = prepare_params(def_metrics)
    new_metrics = prepare_params(new_metrics)

    try:
        sheet = workbook.get_sheet_by_name(sheet_name)
    except:
        sheet = workbook.create_sheet(sheet_name)
        headers = (*def_params["headers"], *def_metrics["headers"],
                   *new_params["headers"], *new_metrics["headers"])
        past_list_in_row(sheet, first_col, first_row, headers)

    row = first_row + 1
    container = def_params["values"][0]
    watermark = def_params["values"][1]
    while True:
        container_cell_val = sheet[cell(first_col, row)].value
        watermark_cell_val = sheet[cell(first_col + 1, row)].value
        if container_cell_val is None:
            break
        if container_cell_val == container and watermark_cell_val == watermark:
            break
        row += 1

    values = (*def_params["values"], *def_metrics["values"],
              *new_params["values"], *new_metrics["values"])
    past_list_in_row(sheet, first_col, row, values)

    save_file(workbook, file)
Exemplo n.º 3
0
def embed(params):
    container = Image.open(params['container'])
    if params['container_size']:
        container = container.resize(params['container_size'], Image.BILINEAR)
    watermark = Image.open(params['watermark'])
    if params['watermark_size']:
        watermark = watermark.resize(params['watermark_size'], Image.BILINEAR)

    qtar = QtarStego(**params)

    try:
        with benchmark("Embedded in "):
            embed_result = qtar.embed(container, watermark, stages=True)
    except NoSpaceError as e:
        print(e)
        return

    stego = embed_result.img_stego
    key = embed_result.key

    if params['stego'] is None:
        stego_path = 'stego_%s.png' % extract_filename(params['container'])
    else:
        stego_path = params['stego']

    save_file(stego, stego_path)
    save_file(key, params['key'])

    bpp_ = embed_result.bpp
    psnr_container = psnr(container, stego)
    ssim_container = ssim(container, stego)

    metrics_info = METRICS_INFO_TEMPLATE.format(psnr_container=psnr_container,
                                                ssim_container=ssim_container,
                                                bpp=bpp_,
                                                key=key)

    print(metrics_info)
Exemplo n.º 4
0
def test(params):
    container = Image.open(params['container'])
    if params['container_size']:
        container = container.resize(params['container_size'], Image.BILINEAR)
    watermark = Image.open(params['watermark'])
    if params['watermark_size']:
        watermark = watermark.resize(params['watermark_size'], Image.BILINEAR)

    embedding_info = EMBEDDING_INFO_TEMPLATE.format(**params)
    print(embedding_info)

    qtar = QtarStego(**params)

    try:
        with benchmark("embedded in "):
            embed_result = qtar.embed(container, watermark, stages=True)
    except NoSpaceError as e:
        print(e)
        return {
            "container psnr": 0,
            "container ssim": 0,
            "watermark psnr": 0,
            "watermark ssim": 0,
            "watermark bcr": 0,
            "container bpp": 0,
            "key size": 0
        }

    container = embed_result.img_container
    stego = embed_result.img_stego
    wm = embed_result.img_watermark
    key = embed_result.key

    bpp_ = embed_result.bpp
    psnr_container = psnr(container, stego)
    ssim_container = ssim(container, stego)

    if 'ss' in params and params['ss']:
        save_stages(
            embed_result.stages_imgs,
            STAMP_TEMPLATE.format(psnr=psnr_container,
                                  ssim=ssim_container,
                                  bpp=bpp_) if params['st'] else None)

    if 'key' in params and params['key']:
        save_file(key, params['key'])
        key = Key.open(params['key'])

    key_info = KEY_INFO_TEMPLATE.format(key=key)
    print(key_info)

    with benchmark("extracted in"):
        extract_stages_imgs = qtar.extract(stego, key, stages=True)
    extracted_wm = extract_stages_imgs['9-extracted_watermark']

    if 'ss' in params and params['ss']:
        save_stages(extract_stages_imgs)

    bcr_wm = bcr(wm, extracted_wm)
    psnr_wm = psnr(wm, extracted_wm)
    ssim_wm = ssim(wm, extracted_wm)

    metrics_info = METRICS_INFO_TEMPLATE.format(psnr_container=psnr_container,
                                                ssim_container=ssim_container,
                                                psnr_wm=psnr_wm,
                                                ssim_wm=ssim_wm,
                                                bpp=bpp_,
                                                bcr=bcr_wm,
                                                width=wm.size[0],
                                                height=wm.size[1])
    print(metrics_info)
    print("_" * 40 + '\n')
    return {
        "container psnr": psnr_container,
        "container ssim": ssim_container,
        "watermark psnr": psnr_wm,
        "watermark ssim": ssim_wm,
        "watermark bcr": bcr_wm,
        "container bpp": bpp_,
        "key size": key.size
    }
Exemplo n.º 5
0
def save_stages(stages, stamp_txt=None):
    for name, img in stages.items():
        if stamp_txt is not None:
            img = stamp_image(img, stamp_txt)
        save_file(img, STAGES_DIR + name + '.bmp')