Ejemplo n.º 1
0
def decompress(compressed_image_path, neural_network_path, target_image_path):
    network = neural_network.load(neural_network_path)

    # read data necessary to decompress
    file = open(compressed_image_path, 'rb')
    x = struct.unpack('>i', file.read(4))[0]
    y = struct.unpack('>i', file.read(4))[0]
    number_of_rgb_squares = struct.unpack('>i', file.read(4))[0]
    bits = struct.unpack('>b', file.read(1))[0]
    hidden_layer_length = struct.unpack('>b', file.read(1))[0]
    smoothing = struct.unpack('>b', file.read(1))[0]

    if hidden_layer_length != len(network.hidden_layers[0]):
        raise ZdpException('Loaded network and compressed image are not compatible')

    logger.info('Decompressing in progress...\033[F')

    if not smoothing:
        squares = decompress_squares(file, number_of_rgb_squares, network, bits)
        print_picture(Image.new('RGB', (x, y)), squares, target_image_path)
    else:
        squares = decompress_squares(file, number_of_rgb_squares, network, bits)
        smoothing_squares = decompress_squares(file, number_of_rgb_squares, network, bits)
        print_picture(Image.new('RGB', (x, y)), squares, target_image_path, smoothing_squares)
    logger.info('Decompressing completed     ')
    logger.info('Decompressed image saved to ' + target_image_path)
Ejemplo n.º 2
0
def compress(image_path, neural_network_path, compressed_image_path, bits, smoothing=False):
    if not 1 <= bits <= 8:
        raise ZdpException('Number of bits must be <1;8>')
    network = neural_network.load(neural_network_path)

    img = Image.open(image_path)
    rgb_squares = get_sequence_squares(img)

    # open file and write data necessary to decompress
    file = open(compressed_image_path, 'wb')
    file.write(struct.pack('>i', img.size[0]))
    file.write(struct.pack('>i', img.size[1]))
    file.write(struct.pack('>i', len(rgb_squares)))
    file.write(struct.pack('>b', bits))
    file.write(struct.pack('>b', len(network.hidden_layers[0])))
    file.write(struct.pack('>b', int(smoothing)))

    if smoothing:
        compress_squares(file, rgb_squares, network, bits, 0, 50)
        smoothing_squares = get_sequence_squares(img, 4)
        compress_squares(file, smoothing_squares, network, bits, 50, 100)
    else:
        compress_squares(file, rgb_squares, network, bits)

    file.close()
    logger.info('Compressing completed          ')
    logger.info('Compressed image saved to ' + compressed_image_path)