Image to BitMap C-Style Stream Conversion using Python :
As I have discussed in earlier Blog that After fetching Image from DataBase we need to convert that image into C streamable format for LCD display, After analyzing the input file and output file of Image to Bitmap Converter I have created my own code which can be deployed on Server and convert Image files into C-style streamable format.
My Python code to output a table of hex values representing the size and data from a .bmp graphics file.
- First we need to convert that Image (.png) to BMP (.bmp) format.
from PIL import Image Image.open("sample1.png").save("sample1.bmp")
Right now my code is to convert that image based on Arguments passed via Command Line
Like
Sample.py [-i] [-r] [-n] [-d] [-x] [-w <bytes>] [-b <size-bytes>] <infile> <tablename>
# param raw "-r", bitmap written as raw table
# param invert "-i", to invert image pixel colors
# param tablewidth "-w <bytes>, The number of characters for each row of the output table
# param sizebytes "-b <bytes>, Bytes = 0, 1, or 2. 0 = auto. 1 = 1-byte for sizes. 2 = 2-byte sizes (big endian)
# param named "-n", use a names structure
# param double "-d", use double bytes rather than single ones
# param xbm "-x", use XBM format (bits reversed in byte)
# param version "-v", returns version number
Here is the sample code attached(not possible to paste here and manage) which I have used to convert the Image into Hex Table, In the future, we can modify code according to the needs. For the initial stage my needs Pre-Processed Image (Like Scaling, Crop, Invert, Version)
Input Image :
[choosen small image because for the larger image again displaying output here is not possible, I will attach the screenshot for larger image conversion]
Output :
PROGMEM const struct { unsigned int width; unsigned int height; unsigned int bitDepth; uint8_t *pixel_data[160]; } SOBA = { 40, 32, 1, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x01, 0x90, 0x00, 0x00, 0x00, 0x03, 0x30, 0x00, 0x00, 0x00, 0x06, 0x60, 0x00, 0x00, 0x00, 0x0c, 0xc0, 0x00, 0x00, 0x00, 0x19, 0x80, 0x00, 0x1f, 0x00, 0x33, 0x00, 0x00, 0x7b, 0xc0, 0x66, 0x00, 0x01, 0xe0, 0xf0, 0xcc, 0x00, 0x03, 0x80, 0x39, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xe0, 0x07, 0xff, 0xff, 0xff, 0xe0, 0x07, 0xff, 0xff, 0xff, 0xe0, 0x07, 0xff, 0xff, 0xff, 0xe0, 0x07, 0xff, 0xff, 0xff, 0xe0, 0x03, 0xff, 0xff, 0xff, 0xc0, 0x03, 0xff, 0xff, 0xff, 0xc0, 0x01, 0xff, 0xff, 0xff, 0x80, 0x01, 0xff, 0xff, 0xff, 0x80, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x7f, 0xff, 0xfe, 0x00, 0x00, 0x3f, 0xff, 0xfc, 0x00, 0x00, 0x1f, 0xff, 0xf8, 0x00, 0x00, 0x0f, 0xff, 0xf0, 0x00, 0x00, 0x03, 0xff, 0xc0, 0x00, 0x00, 0x01, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } };
Here is the converted file using Bitmap Converter Software(LCD Bitmap Converter for emWin v2.0).....And I got a similar result may be some pixel values differ because of the software which I have downloaded is Demo Version so there have added (watermark type "Demo" text).
#include <stdlib.h> #include "GUI.h" #ifndef GUI_CONST_STORAGE #define GUI_CONST_STORAGE const #endif extern GUI_CONST_STORAGE GUI_BITMAP bmsoba; /* Palette The following are the entries of the palette table. Every entry is a 32-bit value (of which 24 bits are actually used) xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx |no use| | Blue | | Green| | Red | */ static GUI_CONST_STORAGE GUI_COLOR _Colorssoba[2] = { 0x000000,0xFFFFFF }; static GUI_CONST_STORAGE GUI_LOGPALETTE _Palsoba = { 2, /* number of entries */ 0, /* No transparency */ &_Colorssoba[0] }; /* Bitmap data */ static GUI_CONST_STORAGE unsigned char _acsoba[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Line: 1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Line: 2 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Line: 3 */ 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, /* Line: 4 */ 0x00, 0x00, 0x00, 0x01, 0x90, 0x00, 0x00, 0x00, /* Line: 5 */ 0x00, 0x00, 0x00, 0x03, 0x30, 0x00, 0x00, 0x00, /* Line: 6 */ 0x00, 0x00, 0x00, 0x06, 0x60, 0x00, 0x00, 0x00, /* Line: 7 */ 0x00, 0x00, 0x00, 0x0C, 0xC0, 0x00, 0x00, 0x00, /* Line: 8 */ 0x00, 0x00, 0x00, 0x19, 0x80, 0x00, 0x00, 0x00, /* Line: 9 */ 0x00, 0x1F, 0x00, 0x33, 0x00, 0x00, 0x00, 0x00, /* Line: 10 */ 0x00, 0x7B, 0xC0, 0x66, 0x00, 0x00, 0x00, 0x00, /* Line: 11 */ 0x01, 0xE0, 0xF0, 0xCC, 0x00, 0x00, 0x00, 0x00, /* Line: 12 */ 0x03, 0x80, 0x39, 0x98, 0x00, 0x00, 0x00, 0x00, /* Line: 13 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Line: 14 */ 0x07, 0xFF, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x00, /* Line: 15 */ 0x07, 0xFF, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x00, /* Line: 16 */ 0x07, 0xFF, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x00, /* Line: 17 */ 0x07, 0xFF, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x00, /* Line: 18 */ 0x07, 0xFF, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x00, /* Line: 19 */ 0x03, 0xFF, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x00, /* Line: 20 */ 0x03, 0xFF, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x00, /* Line: 21 */ 0x01, 0xFF, 0xFF, 0xFF, 0x80, 0x00, 0x00, 0x00, /* Line: 22 */ 0x01, 0xFF, 0xFF, 0xFF, 0x80, 0x00, 0x00, 0x00, /* Line: 23 */ 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, /* Line: 24 */ 0x00, 0x7F, 0xFF, 0xFE, 0x00, 0x00, 0x00, 0x00, /* Line: 25 */ 0x00, 0x3F, 0xFF, 0xFC, 0x00, 0x00, 0x00, 0x00, /* Line: 26 */ 0x00, 0x1F, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, /* Line: 27 */ 0x00, 0x0F, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, /* Line: 28 */ 0x00, 0x03, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00, /* Line: 29 */ 0x00, 0x01, 0xFF, 0x80, 0x00, 0x00, 0x00, 0x00, /* Line: 30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Line: 31 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 /* Line: 32 */ }; GUI_CONST_STORAGE GUI_BITMAP bmsoba = { 40, /* width */ 32, /* height */ 8, /* bytes per line */ 1, /* bits per pixel */ _acsoba, /* Pointer to bitmap data */ &_Palsoba /* Pointer to palette */ }; /********** End of file **********/
Attached the output file for the Conversion of below image
Top Comments