英文:
PicoDVI and Adafruit GFX: code to extract the color palette from an 8-bit PNG
问题
Here is the translated code portion:
我有Adafruit RP2040 Feather带有DVI。我正在浏览示例,并在按照以下说明将8位PNG图像转换为PicoDVI和Adafruit GFX使用的“RGB565”颜色格式时遇到问题。
在https://learn.adafruit.com/picodvi-arduino-library-video-out-for-rp2040-boards?view=all的底部,有一些关于如何进行转换的说明。有一段Python代码,我无法在Windows 10上成功运行。
以下是Python代码:
from PIL import Image, ImagePalette
with Image.open("dragon.png") as img:
pal = img.getpalette()
print("const uint16_t palette[] = {")
for i in range(len(pal) // 3):
r = pal[i * 3 + 0] >> 3
g = pal[i * 3 + 1] >> 2
b = pal[i * 3 + 2] >> 3
rgb = (r << 11) | (g << 5) | b;
print("0x%04X, " % (rgb), end="")
print("};\n")
print("const uint8_t sprite[] = {")
for y in range(img.size[1]):
for x in range(img.size[0]):
p = img.getpixel((x, y))
print("0x%02X, " % (p), end="")
print("};\n")
Please note that I've removed the HTML encoding ("
) for better readability. If you have any questions or need further assistance, feel free to ask.
英文:
I have Adafruit RP2040 Feather with DVI. I am going through examples and have an issue with following instructions to convert an 8 bit png image to the “RGB565” color format used by PicoDVI and Adafruit GFX.
At the bottom of https://learn.adafruit.com/picodvi-arduino-library-video-out-for-rp2040-boards?view=all there are some instructions how do the conversion. There is a python code that I cannot make it run successfully on Windows 10.
Here is the python code:
from PIL import Image, ImagePalette
with Image.open("dragon.png") as img:
pal = img.getpalette()
print("const uint16_t palette[] = {")
for i in range(len(pal) // 3):
r = pal[i * 3 + 0] >> 3
g = pal[i * 3 + 1] >> 2
b = pal[i * 3 + 2] >> 3
rgb = (r << 11) | (g << 5) | b;
print("0x%04X, " % (rgb), end="")
print("};\n")
print("const uint8_t sprite[] = {")
for y in range(img.size[1]):
for x in range(img.size[0]):
p = img.getpixel((x, y))
print("0x%02X, " % (p), end="")
print("};\n")
When I run in (I am not a python developer) python2 I get:
PS C:\Users\PC\Downloads> c:\Python27\python convert.py File "convert.py", line 12 print("0x%04X, " % (rgb), end="") ^ SyntaxError: invalid syntax
When I run it with Python3 I get:
PS C:\Users\PC\Downloads> python3 convert.py const uint16_t palette[] = { Traceback (most recent call last): File "C:\Users\PC\Downloads\convert.py", line 7, in for i in range(len(pal) // 3): TypeError: object of type 'NoneType' has no len()
I've tried with different 8 bit png images.
Is there onother way to run it successfully in python? Is there a way to produce the same PicoDVi compatible mask the bits with ImageMagic, or C? How? Thank you.
答案1
得分: 2
你只能在存在调色板的情况下获取调色板。大多数 *.png 图片是真彩色图像,类似于 JPEG 图片。这些图像将每个像素的颜色保存为 8 位 R、G、B 值,因此没有调色板。在这种情况下,Image.getpalette() 返回一个 'None' 对象。你的 'dragon.png' 似乎是这种类型。要将其转换为带有调色板的 *.PNG 图像,你需要一个能够将其转换为 256 色图像的图形工具。我使用了 IrfanView。
如果它显示 '16,7 Million Colors',那么你没有调色板,因此你可以通过 "Image" ==> "Decrease Color Depth..." 来减少颜色深度到 256 种颜色。然后将其保存为某个 PNG 文件,然后再试一次。
所以你的代码应该检查 "pal = img.getpalette()" 的返回值。只有当它不是 None 时才能继续。
英文:
You can only acquire a palette if there is one. Most *.png pictures are true color images, like JPEG pictures. These have the color of each pixel saved as 8-bit R,G,B values, so there is no palette.
In this case, Image.getpalette() returns a 'None' object.
Your 'dragon.png' seems to be of this type. To convert it to a *.PNG with a palette you need a graphics tool that can convert it to a 256 color image. I used IrfanView.
You can check your image with this tool by pressing "i".
If it states '16,7 Million Colors' you don't have a palette, so you can reduce the color depth via "Image" ==> "Decrease Color Depth..." to 256 colors. Then save it to some PNG file and try again.
So your code should check the return value of "pal = img.getpalette()". Only if it's not None you can proceed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论