Exemplo n.º 1
0
def compress_image(src_path, dest_path, entropy=False, size=8) -> bool:
    print("Reading file")
    bitmap = imagetools.get_bitmap_from_bmp(src_path)

    if entropy:
        print("Bitmap entropy: " + str(ent.entropy(bitmap)))

    print("Crop image")
    bitmap = crop_bitmap(bitmap)

    print("Converting to YCrCb")
    bitmap = imagetools.bgr_to_ycrcb(bitmap)

    print("Separating bitmap to Y, Cb, Cr matrices")
    y, cb, cr = split_to_three_colors(bitmap)

    print("Downsampling")
    cr = downsample(cr)
    cb = downsample(cb)

    y_shape = shape_for_contacting(y.shape, size)
    cr_shape = shape_for_contacting(cr.shape, size)
    cb_shape = shape_for_contacting(cb.shape, size)

    print("Splitting to {0}x{0} sub-matrices".format(size))
    y = split_matrix_into_sub_matrices(y, size)
    cr = split_matrix_into_sub_matrices(cr, size)
    cb = split_matrix_into_sub_matrices(cb, size)

    print("dct")
    y = [dct.dct(sub_matrix) for sub_matrix in y]
    cr = [dct.dct(sub_matrix) for sub_matrix in cr]
    cb = [dct.dct(sub_matrix) for sub_matrix in cb]

    print("Quantization")
    y = [dct.quantization(submatrix) for submatrix in y]
    cr = [dct.quantization(submatrix) for submatrix in cr]
    cb = [dct.quantization(submatrix) for submatrix in cb]

    if entropy:
        print("Compressed entropy: " + str(
            ent.entropy(np.dstack([np.dstack(
                y), np.dstack(cr), np.dstack(cb)]))))

    print("UnQuantization")
    y = [dct.un_quantization(submatrix) for submatrix in y]
    cr = [dct.un_quantization(submatrix) for submatrix in cr]
    cb = [dct.un_quantization(submatrix) for submatrix in cb]

    print("Invert dct")
    y = [dct.inverse_dct(matrix) for matrix in y]
    cr = [dct.inverse_dct(matrix) for matrix in cr]
    cb = [dct.inverse_dct(matrix) for matrix in cb]

    print("Concatenate")
    y = concatenate_sub_matrices_to_big_matrix(y, y_shape)
    cr = concatenate_sub_matrices_to_big_matrix(cr, cb_shape)
    cb = concatenate_sub_matrices_to_big_matrix(cb, cr_shape)

    print("upsample")
    cr = upsample(cr)
    cb = upsample(cb)

    print("concatenate")
    concatenate_three_colors(y, cr, cb, bitmap)

    # print("ycrcb_to_bgr")
    # bitmap = imagetools.ycrcb_to_bgr(bitmap)

    print("save_matrix")
    imagetools.save_matrix(bitmap, mode='YCrCb', dest=dest_path + '.png')
Exemplo n.º 2
0
import os
import sys

import numpy as np

from jpeg import imagetools, encode, dct

if __name__ == '__main__':
    src_dir = os.path.join(os.path.dirname(__file__), 'src')
    root_dir = os.path.dirname(src_dir)

    # Read image
    path = sys.argv[1]
    print("Read image " + path)
    img = encode.crop_bitmap(imagetools.get_bitmap_from_bmp(path))
    imagetools.save_matrix(img,
                           dest=os.path.join(src_dir, 'original_crop.png'))

    def split_and_downsample(matrix, mode):
        print("Split {} colors".format(mode))
        img_channel_a, img_channel_b, img_channel_c = encode.split_to_three_colors(
            matrix)

        channel_const = np.zeros_like(img_channel_a)
        if mode == 'YCrCb':
            channel_const = channel_const + 127.5

        print("Generate {} colors images".format(mode))

        imagetools.save_matrix(encode.concatenate_three_colors(
            img_channel_a, channel_const, channel_const),
Exemplo n.º 3
0
def compress_image(src_path, dest_path, entropy=False,
                   size=8):  # pragma: no cover
    print("Reading file")
    bitmap = imagetools.get_bitmap_from_bmp(src_path)

    if entropy:
        print("Bitmap entropy: " + str(ent.entropy(bitmap)))

    print("Crop image")
    ycrcb_crop = crop_bitmap(bitmap)

    print("Converting to YCrCb")
    ycrcb_bitmap = imagetools.bgr_to_ycrcb(ycrcb_crop)

    print("Separating bitmap to Y, Cb, Cr matrices")
    y, cb, cr = split_to_three_colors(ycrcb_bitmap)

    print("Downsampling")
    cb_downsample = downsample(cb)
    cr_downsample = downsample(cr)

    y_shape = shape_for_contacting(y.shape, size)
    cb_shape = shape_for_contacting(cb_downsample.shape, size)
    cr_shape = shape_for_contacting(cr_downsample.shape, size)

    print("Splitting to 8x8 sub-matrices")
    y_split = split_matrix_into_sub_matrices(y)
    cb_split = split_matrix_into_sub_matrices(cb_downsample)
    cr_split = split_matrix_into_sub_matrices(cr_downsample)

    print("DCT")
    y_dct = [dct.DCT(sub_matrix) for sub_matrix in y_split]
    cb_dct = [dct.DCT(sub_matrix) for sub_matrix in cb_split]
    cr_dct = [dct.DCT(sub_matrix) for sub_matrix in cr_split]

    print("Quantization")
    y_quantization = [dct.quantization(submatrix) for submatrix in y_dct]
    cb_quantization = [dct.quantization(submatrix) for submatrix in cb_dct]
    cr_quantization = [dct.quantization(submatrix) for submatrix in cr_dct]

    if entropy:
        print("Compressed entropy: " + str(
            ent.entropy(
                np.array([y_quantization, cb_quantization, cr_quantization]))))

    print("UnQuantization")
    y_un_quantization = [
        dct.un_quantization(submatrix) for submatrix in y_quantization
    ]
    cb_un_quantization = [
        dct.un_quantization(submatrix) for submatrix in cb_quantization
    ]
    cr_un_quantization = [
        dct.un_quantization(submatrix) for submatrix in cr_quantization
    ]

    print("Invert DCT")
    y_invert_dct = [dct.inverse_DCT(matrix) for matrix in y_un_quantization]
    cb_invert_dct = [dct.inverse_DCT(matrix) for matrix in cb_un_quantization]
    cr_invert_dct = [dct.inverse_DCT(matrix) for matrix in cr_un_quantization]

    print("Concatenate")
    y_big = concatenate_sub_matrices_to_big_matrix(y_invert_dct, y_shape)
    cb_big = concatenate_sub_matrices_to_big_matrix(cb_invert_dct, cb_shape)
    cr_big = concatenate_sub_matrices_to_big_matrix(cr_invert_dct, cr_shape)

    print("upsample")
    cb_upsample = upsample(cb_big)
    cr_upsample = upsample(cr_big)

    new_image = concatenate_three_colors(y_big, cb_upsample, cr_upsample)

    imagetools.save_matrix(new_image, mode='YCrCb', dest=dest_path + '.png')
Exemplo n.º 4
0
 def test_whiteImage_getAllWhite(self):
     test_matrix = generate_one_color_matrix(8**3, 8**3, [255, 255, 255])
     np.testing.assert_array_equal(
         imagetools.get_bitmap_from_bmp(os.path.join(src, "white.bmp")),
         test_matrix)
Exemplo n.º 5
0
 def test_NotProd8Size_ColoredImage(self):
     test_matrix = get_colored_matrix(100, 100)
     np.testing.assert_array_equal(
         imagetools.get_bitmap_from_bmp(
             os.path.join(src, "colored_100x100.bmp")), test_matrix)
Exemplo n.º 6
0
 def test_ColoredImage_getAllColored(self):
     test_matrix = get_colored_matrix(255, 255)
     np.testing.assert_array_equal(
         imagetools.get_bitmap_from_bmp(os.path.join(src, "colored.bmp")),
         test_matrix)
Exemplo n.º 7
0
 def test_BlackImage_getAllBlack(self):
     test_matrix = generate_one_color_matrix(8**3, 8**3, [0, 0, 0])
     np.testing.assert_array_equal(
         imagetools.get_bitmap_from_bmp(os.path.join(src, "black.bmp")),
         test_matrix)