class GrayscaleConverter:
    """
    The GrayscaleConverter object allows you to convert
    a image to a grayscale version easily


    Attributes
    ----------

    success_messages : SuccessMessages
        Object that contains the success messages templates

    error_messages : ErrorMessages
        Object that contains the error messages templates

    """
    def __init__(self):
        self.success_messages = SuccessMessages()
        self.error_messages = ErrorMessages()

    def convert_to_grayscale(self, image):
        """
        Convert a given image to a grayscale version


        Parameters
        ----------

        image : PIL Image Object
            The image to be converted to grayscale

        
        Returns
        -------

        converted_image : PIL Image Object
            The image already converted to grayscale

        """
        image = image.copy()

        try:
            converted_image = image.convert(mode='L')
            self.success_messages.show_success_message(
                'Image converted to grayscale')

        except Exception as e:
            self.error_messages.show_error_message(
                'converting image to grayscale', e)

        return converted_image
Ejemplo n.º 2
0
class ASCIIImageSaver:
    """
    The ASCIIImageSaver object allows you to save your new ASCII images easily


    Attributes
    ----------

    success_messages : SuccessMessages
        Object that contains the success messages templates

    error_messages : ErrorMessages
        Object that contains the error messages templates

    """
    def __init__(self):
        self.success_messages = SuccessMessages()
        self.error_messages = ErrorMessages()

    def save_image(self, text, path):
        """
        Save the ASCII text into a .txt file at a given path


        Parameters
        ----------

        text : str
            The ASCII text to put on the .txt file

        
        Returns
        -------

        None

        """
        try:
            with open(path, 'w') as file:
                file.write(text)
            self.success_messages.show_success_message(
                f"Imaged saved on path: '{path}'")

        except Exception as e:
            self.error_messages.show_error_message('saving ASCII image', e)
 def __init__(self):
     self.success_messages = SuccessMessages()
     self.error_messages = ErrorMessages()
Ejemplo n.º 4
0
class ImageResizer:
    """
    The ImageResizer object allows you to resize an image easily


    Attributes
    ----------

    success_messages : SuccessMessages
        Object that contains the success messages templates

    error_messages : ErrorMessages
        Object that contains the error messages templates

    """

    def __init__(self):
        self.success_messages = SuccessMessages()
        self.error_messages = ErrorMessages()

    def get_width_and_height(self, image):
        """
        Catch the width and height of a given image


        Parameters
        ----------

        image : PIL Image Object
            The image to get the width and height

        
        Returns
        -------

        tuple
            A tuple containing the width and height of the image, respectively

        """
        width, height = image.size

        return (width, height)

    def resize_image(self, image, new_width=100):
        """
        Resize a image, without changing it,
        and following the aspect ratio
        

        Parameters
        ----------

        image : PIL Image Object
            The image to be resized

        new_width : int
            The width of the new image


        Returns
        -------

        resized_image : PIL Image Object
            The new resized image

        """
        image = image.copy()

        width, height = self.get_width_and_height(image)
        aspect_ratio = height / width

        new_height = round(new_width * aspect_ratio * 0.45)

        try:
            resized_image = image.resize((new_width, new_height))
            self.success_messages.show_success_message(f'Image resized from {width}x{height} px to {new_width}x{new_height} px')
            
        except Exception as e:
            self.error_messages.show_error_message('resizing image', e)

        return resized_image
class ASCIIConverter(GrayscaleConverter, ImageResizer):
    """
    The ASCIIConverter object allows you to 
    simple convert an image to a ASCII text


    Attributes
    ----------

    success_messages : SuccessMessages
        Object that contains the success messages templates

    error_messages : ErrorMessages
        Object that contains the error messages templates

    """
    def __init__(self):
        self.success_messages = SuccessMessages()
        self.error_messages = ErrorMessages()

    ASCII_CHARACTERS = '@#$%?*+;:,.'

    def __get_ascii_characters(self, image):
        """
        Convert each pixel from the image to a ASCII character


        Parameters
        ----------

        image : PIL Image Object
            Image used to capture the ASCII characters


        Returns
        -------
            characters : str
                A string containing all ASCII characters from the image

        """
        try:
            pixels = image.getdata()
            characters = ''.join(
                [self.ASCII_CHARACTERS[pixel // 25] for pixel in pixels])
            self.success_messages.show_success_message(
                'ASCII characters gotten')

        except Exception as e:
            self.error_messages.show_error_message('getting ASCII characters',
                                                   e)

        return characters

    def __format_ascii_characters(self, ascii_characters, new_width=200):
        """
        Format the ASCII characters to make them form a 'image'


        Parameters
        ----------

        ascii_characters : str
            The ASCII characters to be formatted

        new_width : int
            The width of the new image
            Has the default value of 200


        Returns 
        -------

        formatted : str
            A formatted string containing the text of the new ASCII image

        """
        try:
            formatted = '\n'.join(
                ascii_characters[i:(i + new_width)]
                for i in range(0, len(ascii_characters), new_width))
            self.success_messages.show_success_message(
                'ASCII characters successfully formatted')

        except Exception as e:
            self.error_messages.show_error_message(
                'formatting ASCII characters', e)

        return formatted

    def convert_to_ascii(self, image, new_width=200):
        """
        Converts a image to a ASCII version


        Parameters
        ----------

        image : PIL Image Object
            The image you want to convert to ASCII

        new_width : int
            The width of the new ASCII image

        
        Returns
        -------

        formatted_text : str
            The new ASCII image already formatted

        """
        resized_image = self.resize_image(image, new_width)
        grayscale_image = self.convert_to_grayscale(resized_image)
        ascii_characters = self.__get_ascii_characters(grayscale_image)
        formatted_text = self.__format_ascii_characters(
            ascii_characters, new_width)

        return formatted_text