Beispiel #1
0
def resize_local_images(target_size: int = 1024) -> None:
    """
    Resize all the local images to a desired target size.
    :param target_size: Target width and height of the square image
    :return: Images will be resized to the desired size
    """
    images_paths = os.path.join('datasets', 'earth_view', 'triple_cropped')
    save_path = os.path.join('datasets', 'earth_view', 'resized', f'{target_size}')
    cropped_images_paths = glob.glob(os.path.join(images_paths, '*.jpg'))

    if not os.path.exists(save_path):
        os.makedirs(save_path)

    for img_path in tqdm(cropped_images_paths, desc='Resizing images...', unit='images'):
        img_name = os.path.basename(img_path)
        img_resized_path = os.path.join(save_path, f'{img_name}_resized{target_size}.jpg')
        # Sanity check: skip if resized image already exists
        if cv2.haveImageReader(img_resized_path):
            continue
        img = cv2.imread(img_path)
        # Sanity check: make sure it's a square image
        h, w, c = img.shape
        if h != w:
            # Skip, but leave a trail
            print(f'"{img_path}" not a square image! Shape: ({h}, {w}, {c})')
            continue
        # Pass: yay, so we resize and save it
        img_resized = cv2.resize(img, (target_size, target_size), interpolation=cv2.INTER_LINEAR)
        cv2.imwrite(img_resized_path, img_resized)

    # Sanity check: same number of resized as original images
    resized_images_paths = glob.glob(os.path.join(save_path, '*.jpg'))
    diff = len(cropped_images_paths) - len(resized_images_paths)
    assert diff == 0, f'Something went wrong, missing {diff} images in {save_path}!'
Beispiel #2
0
def collect_img_files(data_source):
    data_source = str(data_source)
    directory = Path(
        data_source if isdir(data_source) else os.path.dirname(data_source))

    def get_images_from_file(file_name):
        image_files = []
        with open(file_name) as file:
            for line in file.readlines():
                line = line.rstrip('\n')
                if isabs(line):
                    image_files.append(line)
                else:
                    image_files.append(str(directory.joinpath(line)))
        return image_files

    if isfile(data_source):
        if not data_source.endswith('.txt'):
            raise Exception(
                'Only .txt files or directories can be set as data_source')
        return get_images_from_file(data_source)

    file_names = []
    all_files_in_dir = os.listdir(data_source)\
        if isdir(data_source) else glob(data_source)

    for name in all_files_in_dir:
        file = str(directory.joinpath(name))
        if cv.haveImageReader(file):
            file_names.append(file)

    return sorted(file_names)
Beispiel #3
0
def main():
    if cv2.haveImageReader(args["input"]) == True:
        image(args["input"], args["output"], args["resize image"])

    else:
        print("this is a video file")
        video(args["input"], args["output"], args["skip frame"])
Beispiel #4
0
def get_image(path):
    if not os.path.exists(path):
        print_error('\nError: File does not exists.\n')
    if not cv.haveImageReader(path):
        print_error('\nError: Has no image or is corrupted.\n')
    else:
        return cv.imread(path)
Beispiel #5
0
def download_by_country(imgs_by_country: list,
                        save_path: Union[str, os.PathLike]) -> None:
    """
	Auxiliary function to download all the images to the save_path. If the image has already been
	downloaded (in the './images/all/full_resolution/' directory), then we will copy it to the save_path,
	which by default is './images/all/full_resolution'

	:param imgs_by_country: list of tuples containing all the urls of the images and their respective country
	:param save_path: Path to save the images at
	:return: None, the images will be saved at the desired path
	"""
    for img_url, country in tqdm(imgs_by_country,
                                 desc='Downloading images...',
                                 unit='images'):
        country = country.replace(' ', '')
        # If the image doesn't belong to any country, rename to 'None'
        if country == '':
            country = 'None'
        # Make the country dir if it doesn't exist
        if not os.path.isdir(os.path.join(save_path, country)):
            os.makedirs(os.path.join(save_path, country))

        img_name = os.path.basename(
            img_url
        )  # 'https://www.gstatic.com/prettyearth/assets/full/1003.jpg' -> '1003.jpg'
        img_country_save_path = os.path.join(save_path, country, img_name)
        # If image exists, open it, and if there's no error, skip
        if cv2.haveImageReader(img_country_save_path):
            continue

        # On the other hand, if it's already been downloaded in the './images/all/full_resolution' directory, copy it
        all_imgs_path = os.path.join('images', 'all', 'full_resolution',
                                     img_name)
        if cv2.haveImageReader(all_imgs_path):
            res = shutil.copyfile(src=all_imgs_path, dst=img_country_save_path)
            continue

        # Get and download the image
        urllib.request.urlretrieve(img_url, img_country_save_path)
        # Check it was correctly saved and has the expected dimensions
        test_image(img_country_save_path)
Beispiel #6
0
    def __init__(self, dataset_path):
        # Use OpenCV to gather image files
        # Collect names of image files
        self._files = []
        all_files_in_dir = os.listdir(dataset_path)
        for name in all_files_in_dir:
            file = os.path.join(dataset_path, name)
            if cv.haveImageReader(file):
                self._files.append(file)

        # Define shape of the model
        self._shape = (224,224)
Beispiel #7
0
def megjelenit(kep):
    kepe = cv2.haveImageReader(
        kep)  #le ellenorizuk, hogy egy kep utvolat kaptuk-e meg
    if (not kepe):
        print(str(kep) + ':\tEz nem kep!')
    else:
        eredmeny = vizsgal(
            kep, 1
        )  # az elso parameter a kepet adja at, a masodik a képek megjeleniteset engedelyezi
        print(kep)  # eredmeny: kor, kocka, waitKey(0) erteke
        cv2.destroyAllWindows()
        if eredmeny[3] == 113:  # q -re kilepunk a kepek feldolgozasabol
            return -1
Beispiel #8
0
def verify_valid_image_path(filename, read=True):
    """
    Function verifies if the given image can be encoded/decoded by OpenCV.
    If read is true, function checks if image can be decoded (imread()) Otherwise the
    function checks if the image can be encoded (imwrite()).
    """
    if read:
        if cv2.haveImageReader(filename):
            return True
        raise InvalidImagePath("The image path provided is invalid. Please insure the path is correct or the file format is supported.")
    if cv2.haveImageWriter(filename):
        return True
    raise InvalidImagePath("The provided filename cannot be encoded by OpenCV. Please insure your desired file format is supported.")
Beispiel #9
0
def carrega_imagem():

    global imagem_original
    global filename
    global cv2image

    global op_arq_salvar
    global op_arquivo_fechar
    global op_fer_desfazer
    global op_fer_redimensionar
    global op_fer_fcor
    global op_fer_fpb
    global op_aju_fechar
    global frame_sb

    filename = fdlg.askopenfilename()  # Isto te permite selecionar um arquivo
    if not cv2.haveImageReader(
            filename
    ):  # Returns true if the specified image can be decoded by OpenCV
        trata_erro(0)
    else:
        if len(filename) > 0:
            imagem_original = cv2.imread(filename)
            try:
                cv2image = imagem_original.copy()
            except (AttributeError, cv2.error):
                trata_erro(
                    1
                )  # 'Provavelmente você está tentando abrir um arquivo que não é de imagem.'
            except:
                trata_erro(2)  # 'ERRO desconhecido'
            else:
                carrega_img_reseize_scrollbar(canvas_root, cv2image, 0)

                # habilita:
                habilita_desabilita_menu(op_arq_salvar, 1)
                habilita_desabilita_menu(op_arquivo_fechar, 1)
                habilita_desabilita_menu(op_fer_redimensionar, 1)
                habilita_desabilita_menu(op_fer_fcor, 1)
                habilita_desabilita_menu(op_fer_fpb, 1)

                # desabilita:
                habilita_desabilita_menu(op_fer_desfazer, 0)
                var_sc.set(gmin)
                var_sc_max.set(gmin)
                habilita_desabilita_frame(
                    frame_sb, 0)  # desabilia barra deslizante de contornos
Beispiel #10
0
def convert_image(input_file: Path, output_file: Path, max_width: int=None, max_height: int=None, quality:int=JPEG_QUALITY):
    # tests
    if not input_file.is_file():
        raise IOError("input_file is not a valid filepath: " + str(input_file))
    if not cv.haveImageReader(str(input_file)):
        raise IOError("Unable to convert files with extension: " + input_file.suffix)
    if not output_file.is_file():
        raise IOError("output_file is not a valid filepath: " + str(output_file))
    if output_file.suffix not in ALLOWED_OUTPUT_EXTENSIONS:
        raise IOError("Unable to save to a fileformat with extension: " + output_file.suffix)

    try:
        cv_img = _read_image(str(input_file))
        if max_height or max_width:
            cv_img = _resize_image(cv_img, max_height=max_height, max_width=max_width)
        final = _save_image(output_file, cv_img, quality)
        return output_file
    except Exception as e:
        raise Exception(e)
def create_mask(imagePath, maskPath, addInterior=True):
    """ 
    ACTION: 
        Generates a black and white mask of the input image
        The white area corresponds to green markings in the
        file including any interior points and the rest is black. 
        Note that this algorithm fails in some situations, 
        check the output visually to make sure it is correct. 
    INPUTS: 
        imagePath: path to image file
        maskPath: path to mask file to be created
        addInterior: True/False, whether the pixels inside the green line should be labeled white or not
    OUTPUT: 
        1 if mask was created, 0 if not
    """

    # Read image (if it exists) and make copy for comparison
    if cv2.haveImageReader(imagePath):
        originalImage = cv2.imread(imagePath)
    else:
        print(f"Failed to read input file at {imagePath}")
        return 0
    image = originalImage.copy()
    image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

    # Define color range and mask everything within range
    lower = np.array([50, 125, 125], dtype="uint8")
    upper = np.array([100, 255, 255], dtype="uint8")
    mask = cv2.inRange(image, lower, upper)

    if addInterior:
        # Add interior points to mask
        cnts = cv2.findContours(mask, cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_SIMPLE)
        cnts = cnts[0] if len(cnts) == 2 else cnts[1]
        mask = cv2.fillPoly(mask, cnts, (255, 255, 255))
        mask = erosion(mask)  # Perform erosion on mask

    # Save the output
    if not cv2.imwrite(maskPath, mask):
        print(f"Failed to write output file at {maskPath}")

    return 1
Beispiel #12
0
def test_image(img_save_path: Union[str, os.PathLike],
               expected_height: int = 1200,
               expected_width: int = 1800,
               expected_channels: int = 3) -> None:
    """
	Test the saved image to see if it was correctly saved (otherwise, it needs to be downloaded again).
	:param img_save_path: Path to the image (including the image file name and extension, '.jpg')
	:param expected_height: Expected height in pixels of the image; will be the same throughout: 1200 pixels
	:param expected_width: Expected width in pixels of the image; will be the same throughout: 1800 pixels
	:param expected_channels: Number of channels of the image; expected an RGB image, so 3
	:return: None, checks will be conducted
	"""
    assert cv2.haveImageReader(
        img_save_path), f'Image "{img_save_path}" was incorrectly saved!'

    img = cv2.imread(img_save_path)
    h, w, c = img.shape
    msg = f'Image "{img_save_path}" has unexpected dimensions: ({h}, {w}, {c})'
    assert (h, w, c) == (expected_height, expected_width,
                         expected_channels), msg
Beispiel #13
0
def download_all(img_urls: list, save_path: Union[str, os.PathLike]) -> None:
    """
	Auxiliary function to download all the images to the save_path.

	:param img_urls: list containing all the urls of the images
	:param save_path: Path to save the images at
	:return: None, the images will be saved at the desired path
	"""
    for img_url in tqdm(img_urls, desc='Downloading images...', unit='images'):
        img_name = os.path.basename(
            img_url
        )  # 'https://www.gstatic.com/prettyearth/assets/full/1003.jpg' -> '1003.jpg'
        img_save_path = os.path.join(save_path, img_name)
        # If image exists, open it, and if there's no error, skip
        if cv2.haveImageReader(img_save_path):
            continue

        # Get and download the image
        urllib.request.urlretrieve(img_url, img_save_path)
        # Check it was correctly saved and has the expected dimensions
        test_image(img_save_path)
Beispiel #14
0
     i = sys.argv.index("-m")
     modes = ["1", "2", "3", "4"]
     if i + 1 < len(sys.argv) and sys.argv[i + 1] in modes:
         mode = int(sys.argv[i + 1])
     else:
         print("Please enter a valid mode [1, 2, 3, 4]")
         exit(1)
 if "-i" in sys.argv:
     i = sys.argv.index("-i")
     if i + 1 < len(sys.argv) and os.path.isfile(sys.argv[i + 1]):
         filename = sys.argv[i + 1]
     else:
         print("Please enter a valid file")
         exit(1)
 #check that the file can be processed
 canopen = cv.haveImageReader(filename)
 if canopen:
     image_array = cv.imread(filename)
 else:
     print("The specified file cannot be read")
     exit(1)
 #fft mode
 if mode == 1:
     img, ft = utils.get_ft(filename)
     utils.plot_ft(img, ft)
 #Denoising mode
 elif mode == 2:
     img, ft = utils.get_ft(filename)
     zeros, fraction = utils.denoise(ft)
     print("Number of non-zero coefficients: " + str(zeros) +
           ", % of original: " + str(fraction * 100))
Beispiel #15
0
def main():
    argparser = argparse.ArgumentParser(description='CARLA Client to use CIL')
    argparser.add_argument('-v',
                           '--verbose',
                           action='store_true',
                           dest='debug',
                           help='print debug information')
    argparser.add_argument('--host',
                           metavar='H',
                           default='127.0.0.1',
                           help='IP of the host server (default: %(default)s)')
    argparser.add_argument('-p',
                           '--port',
                           metavar='P',
                           default=2000,
                           type=int,
                           help='TCP port to listen to (default: %(default)s)')
    argparser.add_argument('--res',
                           metavar='WIDTHxHEIGHT',
                           default='1280x800',
                           type=_parse_res,
                           help='Window resolution (default: %(default)s)')
    argparser.add_argument('--filter',
                           metavar='PATTERN',
                           default='vehicle.*',
                           help='actor filter (default: %(default)s)')
    argparser.add_argument('--rolename',
                           metavar='NAME',
                           default='hero',
                           help='actor role name (default: %(default)s)')
    argparser.add_argument(
        '--gamma',
        default=2.2,
        type=float,
        help='Gamma correction of the camera (default: %(default)s)')
    argparser.add_argument(
        '--output-folder-name',
        '-out',
        help='Output folder name to save results to (default: %(default)s)',
        default=None)
    argparser.add_argument(
        '--folder',
        '-f',
        type=str,
        help='Folder within `./_logs` that contains the trained model',
        required=True)
    argparser.add_argument('--exp',
                           '-e',
                           type=str,
                           help='Name of experiment within --folder',
                           required=True)
    argparser.add_argument(
        '--ckpt',
        type=int,
        help=
        'Model number; see `./_logs/{folder}/{exp}/checkpoints/` for available models',
        required=True)
    argparser.add_argument(
        '--spawn-point',
        '-sp',
        type=int,
        help='Spawn point number; choose from 0 to 255 (default: %(default)s)',
        default=0)
    argparser.add_argument(
        '--car-model',
        '-model',
        type=str,
        help='Car model to use (make sure it exists; default: %(default)s)',
        default='vehicle.lincoln.mkz2017')
    argparser.add_argument('--synchronous',
                           '-sync',
                           action='store_true',
                           help='Add flag to set the mode to synchronous mode')
    args = argparser.parse_args()

    args.width, args.height = args.res

    pygame.font.init()
    client = carla.Client(args.host, args.port)
    client.set_timeout(4.0)

    hud = HUD(args.width, args.height)
    world = World(client.get_world(), hud, args.spawn_point, args.car_model,
                  g_conf.SENSORS)
    # Get the town name (for saving the results)
    town_name = world.world.get_map().name

    # Set synchronous mode
    if args.synchronous:
        print('Setting synchrounous mode...')
        settings = world.world.get_settings()
        settings.synchronous_mode = True
        settings.fixed_delta_seconds = 1 / 20
        world.world.apply_settings(settings)

    # If named, the output will be saved in the respective folder
    if args.output_folder_name is not None:
        args.output = os.path.join(os.getcwd(), 'drive_runs', args.exp,
                                   str(args.ckpt), town_name,
                                   f'spawn_point-{args.spawn_point:02d}',
                                   f'{args.output_folder_name}')
    else:
        args.output = None

    log_level = logging.DEBUG if args.debug else logging.INFO
    logging.basicConfig(format='%(levelname)s: %(message)s', level=log_level)

    logging.info('listening to server %s:%s', args.host, args.port)

    print(__doc__)

    try:
        from drive.coil_agent import CoILAgent
        import torch
        checkpoint = torch.load(
            os.path.join('_logs', args.folder, args.exp, 'checkpoints',
                         f'{args.ckpt}.pth'))
        merge_with_yaml(
            os.path.join('configs', args.folder, f'{args.exp}.yaml'))
        agent = CoILAgent(checkpoint, town_name=town_name)
        game_loop(args, agent, world)

    except KeyboardInterrupt:
        print('\nCancelled by user. Bye!')
        # Create video
        if args.output is not None:
            print('Creating video...')
            # Remove faulty images so ffmpeg doesn't fail/final video is bad (usually when user cancels execution)
            images = glob.glob(os.path.join(args.output, '*.png'))
            faulty_images = [
                image for image in images if not cv2.haveImageReader(image)
            ]

            # Get the number for each faulty image (shared with all sensors)
            number_list = [
                re.findall(r'\d+', image)[-1] for image in faulty_images
            ]

            # Remove all of the sensors with the same number
            for number in number_list:
                for image in images:
                    if number in image:
                        os.remove(image)

            layer1 = ffmpeg.input(os.path.join(args.output, 'layer1_*.png'),
                                  pattern_type='glob',
                                  framerate=40)
            layer2 = ffmpeg.input(os.path.join(args.output, 'layer2_*.png'),
                                  pattern_type='glob',
                                  framerate=40)
            layer3 = ffmpeg.input(os.path.join(args.output, 'layer3_*.png'),
                                  pattern_type='glob',
                                  framerate=40)
            layer4 = ffmpeg.input(os.path.join(args.output, 'layer4_*.png'),
                                  pattern_type='glob',
                                  framerate=40)

            out1 = ffmpeg.filter([layer1, layer2], 'hstack')
            out2 = ffmpeg.filter([layer3, layer4], 'hstack')
            out = ffmpeg.filter([out1, out2], 'vstack').output(
                os.path.join(args.output,
                             'all_layers.mp4')).run(capture_stdout=True,
                                                    capture_stderr=True)

            out3 = ffmpeg.input(os.path.join(args.output, 'screen', '*.png'),
                                pattern_type='glob',
                                framerate=40).output(
                                    os.path.join(args.output, 'screen',
                                                 'screen.mp4')).run(
                                                     capture_stdout=True,
                                                     capture_stderr=True)
Beispiel #16
0
def multi_crop_local_images(
        target_size: int,
        fullres_path: Union[str, os.PathLike],
        img_save_path: Union[str, os.PathLike],
        make_zip: bool) -> None:
    """
    Reproduction of multi-cropping an image (used in the BreCaHAD dataset in StyleGAN2-ADA)
        https://github.com/NVlabs/stylegan2-ada/blob/1ea5f6fa58108ca9fb94140320a1cdf515c1e246/dataset_tool.py#L836

    However, they use a static overlap between the images, which doesn't translate well to all datasets (where
    individual images may have different dimensions). This code then will try to automate this overlap with the
    desired target size. Note this isn't meant for a conditional dataset!

    :param target_size: The size of the crops; if using for a vanilla StyleGAN1/2/2-ADA, make sure it's a power of 2
    :param fullres_path: Path to the full-resolution images
    :param img_save_path: Root path where we will save the images at
    :param make_zip: Make a ZIP file with all the images; to be saved at './images/zip_files'
    :return: Images will be saved at the specified path in target_sizextarget_size resolution
    """
    # Set the final save path for the images
    save_path = os.path.join(img_save_path, 'all', 'multi_cropped', f'{target_size}')

    # Get all the path images
    full_res_img_paths = []
    for root, _, files in os.walk(os.path.join(fullres_path, 'all', 'full_resolution')):
        for file in files:
            if file.endswith(accepted_filetypes):
                full_res_img_paths.append(os.path.join(root, file))

    if not os.path.exists(save_path):
        os.makedirs(save_path)

    # We go through each image, cutting it according to the dimensions and target_size
    # TODO: optimize this loop with multithreading
    for img_path in tqdm(full_res_img_paths, desc='Cropping images...', unit='images'):
        img_base_name = os.path.basename(img_path)  # 'images/all/full_resolution/1003.jpg' -> '1003.jpg'
        # We will use the image name (here, a number) and the image format (.jpg)
        img_name, ext = os.path.splitext(img_base_name)  # '1003.jpg' -> ('1003', '.jpg')

        # Open image and get dimensions
        img = PIL.Image.open(img_path).convert('RGB')
        w, h = img.size

        # Skip if target size is larger than either side
        if all(target_size > i for i in (h, w)):
            continue

        # Number of columns and rows to crop (guard against edge case where w or h == target_size)
        crop_cols = int(np.rint(w / target_size)) if w / target_size > 1 else 0
        crop_rows = int(np.rint(h / target_size)) if h / target_size > 1 else 0

        # Size of step to take when moving column and row-wise
        width_step = int((w - target_size) / crop_cols) if crop_cols != 0 else 0
        height_step = int((h - target_size) / crop_rows) if crop_rows != 0 else 0

        # Get all the crops
        for i in range(crop_cols + 1):
            for j in range(crop_rows + 1):
                # Keep the original name, but add the cropped number (easier to differentiate)
                save_name = os.path.join(save_path, f'{img_name}_cropped{2*i + j}{ext}')  # _cropped{0,1,2,...}
                # If image exists, open it, and if there's no error, skip (useful if restarting)
                if cv2.haveImageReader(save_name):
                    continue

                # Crop and save it
                new_img = img.crop((i*width_step, j*height_step,  # upper-left corner
                                    i*width_step + target_size, j*height_step + target_size))  # lower-right corner
                new_img.save(save_name)

    # Zip if desired
    if make_zip:
        print(f'Making ZIP file...')
        make_zip_file(
            parent_path_to_zip=os.path.join(img_save_path, 'all', 'multi_cropped'),
            folder_to_zip=f'{target_size}',
            zip_filename=f'all_imgs_multi-cropped{target_size}',
            path_to_save_zip=os.path.join(os.getcwd(), 'images', 'zip_files'))
Beispiel #17
0
else:
    uj = 0

f = open('Eredmeny_mert.txt', 'w')
text = ("Kep neve \t Dobas ertek\tkocka szama\n")
if uj:
    b = open('Eredmeny_valos.txt', 'w')
    b.write(text)
f.write(text)   


dirname = './kepek'     # eleresi utvonal
dirlist = os.scandir(dirname)   # fajlok, es mappak listazasa mappából
for e in dirlist:   # vegig iteralunk a neveken
    kep = dirname + '/'+ e.name # teljes eleres
    kepe = cv2.haveImageReader(kep) #le ellenorizuk, hogy megfelel-e cv2-nek, nehogy egy mappa nevet kapjunk
    if (not kepe):
        print('\tEz nem kep')
    else:
        ## -------
        megjelenjen = 0 # Ezt kell átírni, ha ellenőrizni szeretnénk a program képi kimenetét változatlan adatbázis esetén.
        ## -------
        if uj:
            megjelenjen = 1 ## Ezt semmi kép nem írható át!!
        eredmeny = vizsgal(kep,megjelenjen)   # az elso parameter a kepet adja at, a masodik a képek megjeleniteset engedelyezi
        print(kep)     # eredmeny: kor, kocka, waitKey(0) erteke
        f.write(str(eredmeny[0]) + '\t' + str(eredmeny[1]) + '\t' + str(eredmeny[2]) + '\n')
        if uj:
            print('Adja meg a korok szamat: ')
            korok = int(input())
            print('Adja meg a kockak szamat: ')
Beispiel #18
0
import numpy as np
import cv2 as cv

# Returns true if the specified image can be decoded by OpenCV.
# retval	=	cv.haveImageReader(	filename	)
retval = cv.haveImageReader('lena.png')
print(retval)

# Returns true if an image with the specified filename can be encoded by OpenCV.
# retval	=	cv.haveImageWriter(	filename	)
retval = cv.haveImageWriter('lena.png')
print(retval)

# Reads an image from a buffer in memory.
# The function imdecode reads an image from the specified buffer in the memory. 
# If the buffer is too short or contains invalid data, the function returns an empty matrix ( Mat::data==NULL ).
# In the case of color images, the decoded images will have the channels stored in B G R order.
f = open('lena.png' , 'rb+')
buffer = f.read()
f.close()

image = np.asarray(bytearray(buffer), dtype="uint8")
image = cv.imdecode(image, cv.IMREAD_COLOR)
cv.imshow('lena',image)
cv.waitKey()

# Encodes an image into a memory buffer.
# The function imencode compresses the image and stores it in the memory buffer that is resized to fit the result. 
# See cv::imwrite for the list of supported formats and flags description.
# retval, buf = cv.imencode(ext, img[, params])
# Parameters