示例#1
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('input', help="input BMP file name")
    parser.add_argument("output", help ="output BMP file name")
    parser.add_argument("--crop", help="crop to specified are: eg. X1, Y1, X2, Y2")
    parser.add_argument('--select-color', help="select one color: red, green or blue")
    parser.add_argument('--black-and-white', action='store_true')
    parser.add_argument('--rotate', type=int, help="rotate by given number of degree")
    parser.add_argument('--blur', action="store_true")

    args= parser.parse_args()

    data=bmp.read_bmp(args.input)

    if args.blur:
        data = scipy.ndimage.guassian_filter(data,(3,3,0)) #(0,0,1.5)

    if args.select_color:
        color = args.select_color.lower([0]) #r,g or b
        data=data*np.array(COLOR_VECTORS[color])

    if args.crop:
        x1, y1, x2, y2 = args.crop.split(',')
        x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
        data = data[y1:y2+1, x1:x2+1]

    if args.rotate:
        data = scipy.ndimage.rotate(data, args.rotate, prefilter=False)

    bmp.write_bmp(args.output, data)

    data = bmp.read_bmp(args.input)
    bmp.write_bmp(args.output, data)
示例#2
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('input', help='input BMP file name')
    parser.add_argument('output', help='output BMP file name')
    parser.add_argument('--crop', help='crop the specifide area: x1,y1,x2,y2')
    parser.add_argument('--select_color',
                        help='select one color: red,green,blue')
    parser.add_argument('--rotate')

    args = parser.parse_args()

    data = bmp.read_bmp(args.input)

    if args.rotate:
        data = scipy.ndimage.rotate(data, args.rotate, prefilter=False)

    if args.select_color:
        color = args.select_color.lower()[0]  # r, g or b
        data = data * np.array(COLOR_VECTORS[color])

    if args.crop:
        x1, y1, x2, y2 = args.crop.split(',')
        x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
        data = data[y1:y2 + 1, x1:x2 + 1]

    bmp.write_bmp(args.output, data)
示例#3
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('input', help="input BMP file name")
    parser.add_argument('output', help="output BMP file name")
    parser.add_argument('--crop',
                        help="crop to specified area, eg. X1,Y1,X2,X2")
    parser.add_argument('--select-color',
                        help="select one color: red, green, blue")
    parser.add_argument('--black-and-white', action="store_true")
    parser.add_argument('--blur', action="store_true")
    parser.add_argument('--rotate', type=int, help="rotate by given degrees")
    args = parser.parse_args()

    data = bmp.read_bmp(args.input)

    if args.blur:
        data = scipy.ndimage.gaussian_filter(data, (2, 2, 0))

    if args.rotate:
        data = scipy.ndimage.rotate(data, args.rotate, prefilter=False)

    if args.select_color:
        color = args.select_color.lower()[0]
        data = data * np.array(COLOR_VECTORS[color])

    if args.black_and_white:
        data = data.sum(axis=2, keepdims=True) / 3
        data = np.ones(3) * data

    if args.crop:
        x1, y1, x2, y2 = args.crop.split(',')
        x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
        data = data[y1:y2 + 1, x1:x2 + 1]

    bmp.write_bmp(args.output, data)
示例#4
0
def main():
    args = parse_args()
    data = bmp.read_bmp(args.input)

    if args.rotate:
        data = ndimage.rotate(data, angle=args.rotate)
    if args.blur:
        data = ndimage.gaussian_filter(data, sigma=3)
    if args.black_white:
        sum_array = data.sum(axis=2, keepdims=True) / 3
        data = np.ones(3) * sum_array
    if args.color:
        data = data * np.array([args.color == 'BLUE', args.color == 'GREEN', args.color == 'RED'])

    bmp.write_bmp(args.output, data)
示例#5
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('input', help='input BMP file name')
    parser.add_argument('output', help='output BMP file name')
    parser.add_argument('--crop', help='crop to specified area')
    parser.add_argument('--select-color',
                        help='select one color: red, green, blue')
    parser.add_argument('--black-and-white', action='store_true')
    parser.add_argument('--blur', action='store_true')
    parser.add_argument('--rotate',
                        type=int,
                        help='rotate by given number of degrees')
    args = parser.parse_args()

    data = bmp.read_bmp(args.input)

    if args.blur:
        data = scipy.ndimage.gaussian_filter(data, (6, 1, 1.5))

    if args.rotate:
        data = scipy.ndimage.rotate(data, args.rotate, prefilter=False)

    if args.select_color:
        color = args.select_color.lower()[0]  # r, g or b
        data = data * np.array(COLOR_VECTORS[color])

    if args.black_and_white:
        data = data.sum(axis=2, keepdims=True) / 3
        data = data * np.array([1, 1, 1])

    if args.crop:
        x1, y1, x2, y2 = args.crop.split(',')
        x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
        data = data[y1:y2 + 1, x1:x2 + 1]

    bmp.write_bmp(args.output, data)
示例#6
0
        pack(SIZE_4, data, 50, dec_bmp['important_colors'])

        bmp_palette = bmp.get_palette()
        for i in range(len(bmp_palette)):
            pack(SIZE_1, data, 54 + i, bmp_palette[i])

        offset = dec_bmp['offset']
        pack(SIZE_2, data, offset, len(codes))

        for i in range(len(codes)):
            tmp_offset = offset + 2 + i
            pack(U_SIZE_1, data, tmp_offset, codes[i])
        f.write(data)


def encode(bmp_data):
    codes = []
    for i in range(0, IMG_SIZE, BLOCK_SIZE):
        for j in range(0, IMG_SIZE, BLOCK_SIZE):
            block_tmp = get_block(i, j, bmp_data)
            block_encoded = dct.encode(block_tmp) + [-128]
            codes += block_encoded
    return codes


if __name__ == "__main__":
    dec_bmp = bmp.read_bmp('lena.bmp')
    bmp_data = dec_bmp['bmp_data']
    codes = encode(bmp_data)
    pack_code('lena.cbmp', bmp_data, codes)
示例#7
0
def cut_out_image(pixel_matrix):
    pixels = pixel_matrix[120:354, 140:430]
    bmp.write_bmp('tiger_cut_out.bmp', pixels)


def single_color(pixel_matrix, color_index):
    mask = np.zeros(3)
    mask[color_index] = 1
    bmp.write_bmp('single_color.bmp', pixel_matrix * mask)


def rotate_image(pixel_matrix, degrees: float):
    bmp.write_bmp(
        'rotated_tiger.bmp',
        scipy.ndimage.interpolation.rotate(pixel_matrix, degrees, order=1))


def black_and_white(pixel_matrix):
    # can use also: scipy.ndimage.filters.gaussian_filter (see sigma param)
    sum_array = pixel_matrix.sum(axis=2, keepdims=True) / 3
    bmp.write_bmp('black_and_white_tiger.bmp', np.ones(3) * sum_array)


if __name__ == '__main__':
    pixels = bmp.read_bmp('tiger.bmp')

    cut_out_image(pixels)
    single_color(pixels, 1)
    rotate_image(pixels, -34.0)
    black_and_white(pixels)