element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Connected Cloud Challenge
  • Challenges & Projects
  • Design Challenges
  • Connected Cloud Challenge
  • More
  • Cancel
Connected Cloud Challenge
Blog Connected Cloud Challenge - IoT based Advertisement BillBoards/Hordings - #BlogPost-10 Image to C-Style Stream Conversion
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: sami9974
  • Date Created: 25 May 2020 3:57 AM Date Created
  • Views 2129 views
  • Likes 3 likes
  • Comments 6 comments
  • connectedcloud
  • connected cloud
  • postgresql
  • tft
  • tft display
  • imagetobmp
  • psoc6
  • aws_iot
Related
Recommended

Connected Cloud Challenge - IoT based Advertisement BillBoards/Hordings - #BlogPost-10 Image to C-Style Stream Conversion

sami9974
sami9974
25 May 2020

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 :

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
}
};

 

image

 

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

image

Attachments:
python_plus_output.zip
  • Sign in to reply

Top Comments

  • cstanton
    cstanton over 5 years ago in reply to sami9974 +3
    sami9974 , this was because certain words contained in it triggered the spam filter. It's now been released.
  • sami9974
    sami9974 over 5 years ago +1
    e14phil can you check why this blog post asking for approval, and please take the necessary steps and approve it. so that i can be count in submission
Parents
  • sami9974
    sami9974 over 5 years ago

    e14phil can you check why this blog post asking for approval, and please take the necessary steps and approve it.

    so that i can be count in submission

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • cstanton
    cstanton over 5 years ago in reply to sami9974

    sami9974, this was because certain words contained in it triggered the spam filter. It's now been released.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • cstanton
    cstanton over 5 years ago in reply to sami9974

    sami9974, this was because certain words contained in it triggered the spam filter. It's now been released.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube