/image2array

Primary LanguageCMIT LicenseMIT

Image2Array

A simple to use python script to convert image to a C header file. Supports majority of image formats including svg

Usage

usage: Image2Array.py [-h] -i INPUT [-o OUTPUT] [--header HEADER] [--array_name ARRAY_NAME]

Convert image files to C header files

optional arguments:
  -h, --help            show this help message and exit
  -i INPUT, --input INPUT
                        Specify the path of input image
  -o OUTPUT, --output OUTPUT
                        Specify the name of output .h file (dont write .h to the end)
  --header HEADER       Path of include header for icon struct
  --array_name ARRAY_NAME
                        Specify the name of output array in header file
  • Test Images are included in test/, also includes a test.h file generated by Image2Array

Documentation

  • template.py contains template for the output header file, custom one can be added accordingly
def Image2Array(fileName, outputFileName="ImageHeader", headerPath="../../UI/Widgets/Icon.h", arrayName="ImageArray", templateString=templateString):
    '''
    Generates a C header file with image data converted into a array

            Parameters:
                    fileName (str): image file to be converted
                    outputFileName (str): filename of generated header file
                    headerPath (str): path of header to be included
                    arrayName (str): name of the image array
                    templateString (string.Template): specify the template which the generated header file should use, default one specified below
                    templateString = Template(
                    """#ifndef $MACRO_GUARD
                    #define $MACRO_GUARD

                    #include "$ICON_HEADER_PATH"
                        
                    const Icon STATIC_SECTION $ARRAY_NAME = {$ICON_WIDTH, $ICON_HEIGHT, (const uint8_t[]){$ICON_DATA}};
                        
                    #endif"""
                    ) 
                    Necessary to use all the template substituents, their order can change in custom template string
            Returns:
                    None
    '''

License

MIT License

Copyright (c) 2021 Vedant Paranjape

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.