简体   繁体   English

Python-如何使用Open-CV或PIL将24位PNG图像转换为32位

[英]Python - how to convert a 24-bit PNG image to 32-bit using Open-cv or PIL

I want to convert a 24-bit PNG image to 32-bit so that it can be displayed on the LED matrix. 我想将24位PNG图像转换为32位,以便可以将其显示在LED矩阵上。 Here is the code which I have used, but it converted 24-bit to 48-bit 这是我使用过的代码,但是它将24位转换为48位

import cv2
import numpy as np
i = cv2.imread("bbb.png")
img = np.array(i, dtype = np.uint16)
img *= 256
cv2.imwrite('test.png', img)

I think you have confused the color bit-depth with the size of the input image/array. 我认为您已经将颜色位深度与输入图像/阵列的大小混淆了。 From the links posted in the comments, there is no mention of 32 as a bit depth. 在评论中发布的链接中,没有提到32深度。 The script at that tutorial link uses an image with 3-channel, 8-bit color (red, green, and blue code values each represented as numbers from 0-255). 该教程链接处的脚本使用的图像具有3通道,8位颜色(红色,绿色和蓝色代码值,每个值表示为0-255之间的数字)。 The input image must have the same height as the array, but can be a different width to allow scrolling. 输入图像必须与数组具有相同的高度,但可以具有不同的宽度以允许滚动。

For more on bit-depth: https://en.wikipedia.org/wiki/Color_depth 有关位深度的更多信息: https : //en.wikipedia.org/wiki/Color_depth

I looked at the christmas.png image in the code you linked to, and it appears to be a 624x8 pixel image with a palette and an 8-bit alpha channel. 我查看了链接到的代码中的christmas.png图像,它看起来像是一个624x8像素的图像,带有一个调色板和一个8位的alpha通道。

Assuming the sample image works, you can make one with the same characteristics by taking a PNG image and adding a fully opaque alpha channel like this: 假设样本图像有效,则可以通过拍摄PNG图像并添加完全不透明的alpha通道来制作具有相同特征的图像,如下所示:

#!/usr/local/bin/python3
from PIL import Image

# Load the image and convert to 32-bit RGBA
im = Image.open("image.png").convert('RGBA')

# Save result
im.save("result.png")

I generated a gradient image and applied that processing and got this, so maybe you can try that: 我生成了一个渐变图像并应用了该处理并得到了它,所以也许您可以尝试:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM