PhotoMosaic.py
def create_mosaic(
    main_image_path:str,
    size:int,
    resolution:int,
    samples_path:str = ".\\",
    name:str = "Mosaic.jpg",
    recursive:bool = False,
    color_mode:str = "SIMPLIFIED",
    resize_mode:str = "BICUBIC",
    formats:list = []
) -> bool:
    """
    ### Creates a images mosaic combining many sample images to \
    create a bigger image that resembles the original main image
    
    #### Arguments:
    - main_image_path {str} -- Path to the main image, source to the mosaic image
    - size {int} -- Size of each tile in the main image (different from tile resolution)
    - resolution {int} -- Resolution (size) of each tile in the final image
    
    #### Keyword Arguments:
    - samples_path {str} -- The path to the samples images (default: {".\"})
    - recursive {bool} -- Whether or not to recursively search all the files in the sample images folder (default: {False})
    - color_mode {str} -- Specifies how to calculate the distance between two colors (default: {"SIMPLIFIED"})
    - resize_mode {str} -- Specifies a resampling filter for resizing images (default: {"BICUBIC"})
    - formats {list} -- Aditional file formats to be accepted (dot required) (default: [])
    
    #### Returns:
    {bool} -- Whether or not the function exited successfully
    """

    # Loading main image
    try:
        main_image:PIL.Image.Image = PIL.Image.open(main_image_path)
    except FileNotFoundError:
        print(f"The main image could not be found. Please try again. (Input: {main_image_path})")
        return False

    main_image_info = ImageInfo(main_image_path, main_image, average_color(main_image, resize_mode))

    # Valid extensions
    valid_extensions = [".jpg", ".jpeg", ".png"]
    valid_extensions.extend(formats)
    valid_extensions = tuple(valid_extensions)

    # The list with all image names
    path_name_list = []

    # Adding the images
    for ext in valid_extensions:
        if recursive:
            path_name_list.extend(glob(f"{samples_path}/**/*{ext}", recursive=True))
        else:
            path_name_list.extend(glob(f"{samples_path}/*{ext}", recursive=False))

    # Removing all non valid image
    path_name_list = list(filter(
        lambda x: 
            x.endswith(valid_extensions),
    path_name_list))

    # Stores the information of all the sample images
    sample_image_info = []

    # Creating progress bar
    pbar = tqdm(path_name_list, "Loading sample images", unit=" images", leave=False)

    # Looping through all the images
    for path_name in pbar:

        # Updating current image
        pbar.set_postfix({"Current": path_name[-25:]}, True)

        # Opening the files
        with PIL.Image.open(path_name) as image:
            image = image.convert("RGBA")

            # Getting info
            sq_image = square_crop(image, resolution, resize_mode)
            av_color = average_color(image, resize_mode)

            # Saving info
            sample_image_info.append(ImageInfo(path_name,sq_image,av_color))

    # Creating the mosaic
    mosaic_image = create_mosaic_image(main_image, sample_image_info, size, resolution, color_mode, resize_mode)

    # Checking if the mosaic is valid
    if mosaic_image == None:
        return False

    # Creating progress bar
    pbar = tqdm(desc="Saving the mosaic... (This might take a while)", bar_format="{desc}", leave=False)

    # Saving the image
    mosaic_image.save(name)

    # Closing the progress bar
    pbar.close()

    # Success
    return True

    ### Creates a images mosaic combining many sample images to \
    create a bigger image that resembles the original main image
    
    #### Arguments:
    - main_image_path {str} -- Path to the main image, source to the mosaic image
    - size {int} -- Size of each tile in the main image (different from tile resolution)
    - resolution {int} -- Resolution (size) of each tile in the final image
    
    #### Keyword Arguments:
    - samples_path {str} -- The path to the samples images (default: {".\"})
    - recursive {bool} -- Whether or not to recursively search all the files in the sample images folder (default: {False})
    - color_mode {str} -- Specifies how to calculate the distance between two colors (default: {"SIMPLIFIED"})
    - resize_mode {str} -- Specifies a resampling filter for resizing images (default: {"BICUBIC"})
    - formats {list} -- Aditional file formats to be accepted (dot required) (default: [])
    
    #### Returns:
    {bool} -- Whether or not the function exited successfully
    
Creates a images mosaic combining many sample images to \
create a bigger image that resembles the original main image





{bool} -- Whether or not the function exited successfully
Creates a images mosaic combining many sample images to \ create a bigger image that resembles the original main image { bool } -- Whether or not the function exited successfully
def create_mosaic(
    main_image_path:str,
    size:int,
    resolution:int,
    samples_path:str = ".\\",
    name:str = "Mosaic.jpg",
    recursive:bool = False,
    color_mode:str = "SIMPLIFIED",
    resize_mode:str = "BICUBIC",
    formats:list = []
) -> bool:
    try:
        main_image:PIL.Image.Image = PIL.Image.open(main_image_path)
    except FileNotFoundError:
        print(f"The main image could not be found. Please try again. (Input: {main_image_path})")
        return False
    main_image_info = ImageInfo(main_image_path, main_image, average_color(main_image, resize_mode))
    valid_extensions = [".jpg", ".jpeg", ".png"]
    valid_extensions.extend(formats)
    valid_extensions = tuple(valid_extensions)
    path_name_list = []
    for ext in valid_extensions:
        if recursive:
            path_name_list.extend(glob(f"{samples_path}/**/*{ext}", recursive=True))
        else:
            path_name_list.extend(glob(f"{samples_path}/*{ext}", recursive=False))
    path_name_list = list(filter(
        lambda x: 
            x.endswith(valid_extensions),
    path_name_list))
    sample_image_info = []
    pbar = tqdm(path_name_list, "Loading sample images", unit=" images", leave=False)
    for path_name in pbar:
        pbar.set_postfix({"Current": path_name[-25:]}, True)
        with PIL.Image.open(path_name) as image:
            image = image.convert("RGBA")
            sq_image = square_crop(image, resolution, resize_mode)
            av_color = average_color(image, resize_mode)
            sample_image_info.append(ImageInfo(path_name,sq_image,av_color))
    mosaic_image = create_mosaic_image(main_image, sample_image_info, size, resolution, color_mode, resize_mode)
    if mosaic_image == None:
        return False
    pbar = tqdm(desc="Saving the mosaic... (This might take a while)", bar_format="{desc}", leave=False)
    mosaic_image.save(name)
    pbar.close()
    return True
def create_mosaic ( main_image_path : str , size : int , resolution : int , samples_path : str = ".\\" , name : str = "Mosaic.jpg" , recursive : bool = False , color_mode : str = "SIMPLIFIED" , resize_mode : str = "BICUBIC" , formats : list = [ ] ) -> bool : try : main_image : PIL . Image . Image = PIL . Image . open ( main_image_path ) except FileNotFoundError : print ( f"The main image could not be found. Please try again. (Input: {main_image_path})" ) return False main_image_info = ImageInfo ( main_image_path , main_image , average_color ( main_image , resize_mode ) ) valid_extensions = [ ".jpg" , ".jpeg" , ".png" ] valid_extensions . extend ( formats ) valid_extensions = tuple ( valid_extensions ) path_name_list = [ ] for ext in valid_extensions : if recursive : path_name_list . extend ( glob ( f"{samples_path}/**/*{ext}" , recursive = True ) ) else : path_name_list . extend ( glob ( f"{samples_path}/*{ext}" , recursive = False ) ) path_name_list = list ( filter ( lambda x : x . endswith ( valid_extensions ) , path_name_list ) ) sample_image_info = [ ] pbar = tqdm ( path_name_list , "Loading sample images" , unit = " images" , leave = False ) for path_name in pbar : pbar . set_postfix ( { "Current" : path_name [ - 25 : ] } , True ) with PIL . Image . open ( path_name ) as image : image = image . convert ( "RGBA" ) sq_image = square_crop ( image , resolution , resize_mode ) av_color = average_color ( image , resize_mode ) sample_image_info . append ( ImageInfo ( path_name , sq_image , av_color ) ) mosaic_image = create_mosaic_image ( main_image , sample_image_info , size , resolution , color_mode , resize_mode ) if mosaic_image == None : return False pbar = tqdm ( desc = "Saving the mosaic... (This might take a while)" , bar_format = "{desc}" , leave = False ) mosaic_image . save ( name ) pbar . close ( ) return True
Creates a images mosaic combining many sample images to \
create a bigger image that resembles the original main image
Creates a images mosaic combining many sample images to \ create a bigger image that resembles the original main image
"""
    ### Creates a images mosaic combining many sample images to \
    create a bigger image that resembles the original main image
    
    #### Arguments:
    - main_image_path {str} -- Path to the main image, source to the mosaic image
    - size {int} -- Size of each tile in the main image (different from tile resolution)
    - resolution {int} -- Resolution (size) of each tile in the final image
    
    #### Keyword Arguments:
    - samples_path {str} -- The path to the samples images (default: {".\"})
    - recursive {bool} -- Whether or not to recursively search all the files in the sample images folder (default: {False})
    - color_mode {str} -- Specifies how to calculate the distance between two colors (default: {"SIMPLIFIED"})
    - resize_mode {str} -- Specifies a resampling filter for resizing images (default: {"BICUBIC"})
    - formats {list} -- Aditional file formats to be accepted (dot required) (default: [])
    
    #### Returns:
    {bool} -- Whether or not the function exited successfully
    """ # Loading main image # Valid extensions # The list with all image names # Adding the images # Removing all non valid image # Stores the information of all the sample images # Creating progress bar # Looping through all the images # Updating current image # Opening the files # Getting info # Saving info # Creating the mosaic # Checking if the mosaic is valid # Creating progress bar # Saving the image # Closing the progress bar # Success
main_image_path
str
size
int
resolution
int
samples_path
str
name
str
recursive
bool
color_mode
str
resize_mode
str
formats
list