-
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #365 from sonic74/main
adapt bmp2rgb565.py to rgb888
- Loading branch information
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Converts an image to a RGB888 array | ||
|
||
import argparse | ||
from PIL import Image | ||
|
||
def rgb_to_rgb888(r, g, b): | ||
return (r << 16) | (g << 8) | (b) | ||
|
||
def convert_bmp_to_rgb888_array(file_path): | ||
# Open image | ||
image = Image.open(file_path) | ||
|
||
# Retrieve pixel data | ||
pixels = list(image.convert("RGB").getdata()) | ||
|
||
# Initialize RGB888 array | ||
rgb888_array = [] | ||
|
||
# Iterate through each pixel and convert to RGB888 | ||
for r, g, b in pixels: | ||
rgb888_value = rgb_to_rgb888(r, g, b) | ||
rgb888_array.append(str(rgb888_value)) | ||
|
||
# Join the array elements into a string with square brackets | ||
return "[" + ", ".join(rgb888_array) + "]" | ||
|
||
# Create argument parser | ||
parser = argparse.ArgumentParser(description="Convert an image to an RGB888 array string.") | ||
parser.add_argument("file_path", help="Path to the image to be converted.") | ||
|
||
# Parse arguments | ||
args = parser.parse_args() | ||
|
||
# Convert the BMP file to an RGB888 array string | ||
rgb888_array_string = convert_bmp_to_rgb888_array(args.file_path) | ||
|
||
# Print the converted string | ||
print(rgb888_array_string) |