【问题标题】:How do I export a 16 bit-per-pixel image bitmap in the Python Image Library?如何在 Python 图像库中导出每像素 16 位的图像位图?
【发布时间】:2018-04-19 04:27:43
【问题描述】:

我正在尝试将一组每像素 24 位的位图文件转换为每像素 16 位的图像。

环顾四周,Python 的图像库 (PIL) 似乎可以做到这一点,但到目前为止,我只能进行 8 位或 24 位图片导出。我需要每种颜色 5 位,每个通道 5 位和 1 个 alpha 位。我应该使用什么转换方法?这是我的代码:

import os
from PIL import Image

file_list = []

location = "C:/bad_images"
os.chdir(location)

for file in os.listdir(location):
    if file.endswith(".bmp"):
        BMP_file = os.path.basename(file)
        print(os.path.join("/mydir", file))
        ##im = Image.open(BMP_file).convert("RGB", colors = 256) ##Does not 
  work  Nor does L
        im = Image.open(BMP_file).convert("P", colors = 256)
        im.save('converted/' + BMP_file)

此脚本输出的图像是 8 位颜色。我找不到任何 16 位格式的示例。谢谢!

【问题讨论】:

    标签: image python-3.x bitmap


    【解决方案1】:

    对于您的代码:

    im = Image.open(BMP_file).convert("P", colors = 256)
            im.save('converted/' + BMP_file)
    

    尝试使用:

    im = Image.open(BMP_file).convert("PA", colors = 256)
            im.save('converted/' + BMP_file)
    

    或者

    im = Image.open(BMP_file).convert("PA;L", colors = 256)
            im.save('converted/' + BMP_file)
    

    “P”是 8 位,但“PA”应该是 16。 该文件底部附近有一个关于要使用的转换字母的列表。 http://svn.effbot.org/public/pil/libImaging/Unpack.c

    另外,您要的是我不太熟悉的 5x5x5x1 位格式,我见过 5x6x5 或 4x4x4x4 格式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 2018-04-01
      • 1970-01-01
      • 2014-10-18
      • 2011-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多