Exemplo n.º 1
0
import numpy as np
import bpcs as bp

parser = ArgumentParser()
parser.add_argument("-i", "--image", type=str, help="encoded image path")
parser.add_argument("-s", "--secret", type=str, help="secret text file path")
parser.add_argument("-o", "--output", type=str, help="output text file path")
parser.add_argument("--verbose", action="store_true", help="verbose")
args = parser.parse_args()

blocksize = (8, 8)
ath = 0.45  # complexity threshold

if args.image:
    arr = bp.read_image_as_numpy(args.image)
    arr = bp.to_binary(arr)

    COLOR = 1
    if len(arr.shape) == 4:  # RGB
        COLOR = 3
    elif len(arr.shape) == 3:  # gray-scale
        arr = arr.reshape(arr.shape[:2] + (
            1,
            arr.shape[2],
        ))
        COLOR = 1
    else:
        print("Unsupported shape of image")
        exit(1)
    n_valid_blocks = 0
    for color in range(COLOR):
    print("    BIT  : 0~7 (0:MSB, 7:LSB)")
    exit(1)

PATH = sys.argv[1]
COLOR = int(sys.argv[2])
BIT = int(sys.argv[3])


def merge_bitplane_to_image(bitplane, arr, color):
    arr = bp.to_image(arr)
    img = np.zeros(arr.shape)
    img[:, :, color] = bitplane
    return img


arr = bp.read_image_as_numpy(PATH)
if len(arr.shape) < 2 or len(arr.shape) > 3:
    print("Unsupported shape of image")
    exit(1)
arr = bp.to_binary(
    arr)  # arr.shape = (h, w, 3(color), 8(byte)) or (h, w, 8(byte))
# arr = bp.to_image(arr)  # arr.shape = (h, w, 3) or (h, w)
bitplane = bp.extract_bitplane(arr, COLOR, BIT)
bitplane[bitplane > 0] = 255
if COLOR != -1 and len(arr.shape) == 4:
    arr = merge_bitplane_to_image(bitplane, arr, COLOR)
else:
    arr = bitplane
Image.fromarray(np.uint8(arr)).show()  # show image
# Image.fromarray(np.uint8(arr)).save("test.png") # save image