Skip to content

Commit

Permalink
Added png to cpp conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderRedYT committed Dec 30, 2021
1 parent 50c8dfb commit ca3fd9f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# rgb565-converter

A simple script to help converting png files to rgb565 (used in [TFT_eSPI](https://github.com/Bodmer/TFT_eSPI))
A simple script to help converting png files to rgb565 (used in [TFT_eSPI](https://github.com/Bodmer/TFT_eSPI))

## Requirements
- ImageMagick
83 changes: 81 additions & 2 deletions rgb565_converter/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
import argparse
import os
import tempfile
from PIL import Image
import struct

from enum import Enum, auto

class Mode(Enum):
CPP = ".cpp"
PNG = ".png"

def main():
parser = argparse.ArgumentParser(
Expand All @@ -21,12 +29,83 @@ def main():
dest="output_file",
help="Output file to be converted."
)

args = parser.parse_args()

input_basename = os.path.basename(args.input_file).rsplit('.', 1)

mode = Mode.CPP if (input_basename[1] == 'png') else Mode.PNG

if args.output_file is None:
args.output_file = os.path.basename(args.input_file.rsplit('.', 1)[0]) + '.png'
args.output_file = input_basename[0] + mode.value

output_basename = os.path.basename(args.output_file).rsplit('.', 1)

if (input_basename[1] not in ['png', 'cpp']):
print("Error: Input file must be a .png or .cpp file.")
exit(1)

if (output_basename[1] not in ['png', 'cpp']):
print("Error: Output file must be a .png or .cpp file.")
print(f"Output file: {output_basename}")
exit(1)

if (input_basename[1] == output_basename[1]):
print("Error: Input and output file must be different.")
exit(1)

if (mode == Mode.PNG):
convert_rgb565_to_png(args)
else:
convert_png_to_rgb565(args)

def convert_png_to_rgb565(args):
name = os.path.basename(args.output_file).rsplit('.', 1)[0]
png = Image.open(args.input_file)
width, height = png.size

max_line_width = min(width, 64)

# iterate over the pixels
image = png.getdata()
image_content = ""
for i, pixel in enumerate(image):
r = (pixel[0] >> 3) & 0x1F
g = (pixel[1] >> 2) & 0x3F
b = (pixel[2] >> 3) & 0x1F
rgb = r << 11 | g << 5 | b
image_content += f"0x{rgb:04X}" + (",\n " if (i % max_line_width == max_line_width-1) else ",")

if image_content.endswith("\n "):
image_content = image_content[:-5]

output_h_content = f"""
#pragma once
#include "icon.h"
namespace bobbyicons {{
extern const espgui::Icon<{width}, {height}> {name};
}} // namespace bobbyicons
""".strip() + "\n"

output_cpp_content = f"""
#include "{name}.h"
namespace bobbyicons {{
const espgui::Icon<{width}, {height}> {name}{{{{
{image_content}
}}, "{name}"}}
}} // namespace bobbyicons
""".strip() + "\n"

with open(args.output_file, 'w') as output_file:
output_file.write(output_cpp_content)

with open(args.output_file.replace('.cpp', '.h'), 'w') as output_file:
output_file.write(output_h_content)


def convert_rgb565_to_png(args):
with open(args.input_file, 'r') as input_file:
with tempfile.NamedTemporaryFile(mode='wb', suffix='.rgb565') as tmp_file:
tmp = input_file.read()
Expand Down
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='rgb565-converter',
version='1.0.1',
version='1.1.0',
description='Convert a file from png to rgb565 (cpp) and vice versa.',
url='https://github.com/CommanderRedYT/rgb565-converter',
author='CommanderRedYT',
Expand All @@ -16,4 +16,8 @@
python_requires='>=3.6',
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
install_requires=[
'argparse~=1.4.0',
'Pillow~=8.1.0',
],
)

0 comments on commit ca3fd9f

Please sign in to comment.