将图像转换为索引的2位灰度BMP 您所在的位置:网站首页 转换为灰度图像的函数 将图像转换为索引的2位灰度BMP

将图像转换为索引的2位灰度BMP

2023-03-25 05:39| 来源: 网络整理| 查看: 265

首先,我的问题与我如何将图像转换为每个像素2位不同?不幸的是,它的解决方案不适用于我的情况...

我需要将图像转换为每像素2位灰度BMP格式。样本图像具有以下属性:

Color Model: RGB Depth: 4 Is Indexed: 1 Dimension: 800x600 Size: 240,070 bytes (4 bits per pixel but only last 2 bits are used to identify the gray scales as 0/1/2/3 in decimal or 0000/0001/0010/0011 in binary, plus 70 bytes BMP metadata or whatever)

样本BMP图像开始部分的十六进制值:

3代表图像开始处的白色像素。再往下是代表黑色,深灰色和浅灰色的0、1和2:

使用以下命令,

convert pic.png -colorspace gray +matte -depth 2 out.bmp

我可以获得视觉上正确的4级灰度图像,但是每个像素的深度或大小错误:

Color Model: RGB Depth: 8 (expect 4) Dimension: 800x504 Size: 1,209,738 bytes (something like 3 bytes per pixel, plus metadata) (no mention of indexed colour space)

请帮忙...

1> yy502..:

好的,我按照Mark的提示编写了Python脚本(请参阅原始问题下的注释),以手动创建具有4bpp的4级灰度BMP。这种特定的BMP格式构造适用于WaveShare制造的4.3英寸电子纸显示模块。规格可以在这里找到:http : //www.waveshare.com/wiki/4.3inch_e-Paper

这是将原始图片传递到我的代码并保存结果的方法。

convert in.png -colorspace gray +matte -colors 4 -depth 2 -resize '800x600>' pgm:- | ./4_level_gray_4bpp_BMP_converter.py > out.bmp

内容4_level_gray_4bpp_BMP_converter.py:

#!/usr/bin/env python """ ### Sample BMP header structure, total = 70 bytes ### !!! little-endian !!! Bitmap file header 14 bytes 42 4D "BM" C6 A9 03 00 FileSize = 240,070 '] # where item 1 is always P5, item 2 is width heigh, item 3 is always 255, items 4 is pixels/colours data = sys.stdin.readlines() width = int(data[DIMENTIONS].strip().split(' ')[0]) height = int(data[DIMENTIONS].strip().split(' ')[1]) if not width*height == len(data[PIXELS]): print "Error: pixel data (%s bytes) and image size (%dx%d pixels) do not match" % (len(data[PIXELS]),width,height) sys.exit() colours = [] # enumerate 4 gray levels for p in data[PIXELS]: if not p in colours: colours.append(p) if len(colours) == 4: break # it's possible for the converted pixels to have less than 4 gray levels colours = sorted(colours) # sort from low to high # map each colour to e-paper gray indexes # creates hex string of pixels # e.g. "0033322222110200....", which is 4 level gray with 4bpp if len(colours) == 1: # unlikely, but let's have this case here pixels = data[PIXELS].replace(colours[0],BLACK) elif len(colours) == 2: # black & white pixels = data[PIXELS].replace(colours[0],BLACK)\ .replace(colours[1],WHITE) elif len(colours) == 3: pixels = data[PIXELS].replace(colours[0],DARK_GRAY)\ .replace(colours[1],GRAY)\ .replace(colours[2],WHITE) else: # 4 grays as expected pixels = data[PIXELS].replace(colours[0],BLACK)\ .replace(colours[1],DARK_GRAY)\ .replace(colours[2],GRAY)\ .replace(colours[3],WHITE) # BMP pixel array starts from last row to first row # and must be aligned to 4 bytes or 8 pixels padding = "F" * ((BYTE/BPP) * ALIGNMENT - width % ((BYTE/BPP) * ALIGNMENT)) aligned_pixels = ''.join([pixels[i:i+width]+padding for i in range(0, len(pixels), width)][::-1]) # convert hex string to represented byte values def Hex2Bytes(hexStr): hexStr = ''.join(hexStr.split(" ")) bytes = [] for i in range(0, len(hexStr), 2): byte = int(hexStr[i:i+2],16) bytes.append(chr(byte)) return ''.join(bytes) # convert integer to 4-byte little endian hex string # e.g. 800 => 0x320 => 00000320 (big-endian) =>20030000 (little-endian) def i2LeHexStr(i): be_hex = ('0000000'+hex(i)[2:])[-8:] n = 2 # split every 2 letters return ''.join([be_hex[i:i+n] for i in range(0, len(be_hex), n)][::-1]) BMP_HEADER = BMP_HEADER % (i2LeHexStr(len(aligned_pixels)/(BYTE/BPP)+BMP_HEADER_SIZE),i2LeHexStr(width),i2LeHexStr(height)) sys.stdout.write(Hex2Bytes(BMP_HEADER+aligned_pixels))

编辑:有关此电子纸显示器的所有内容以及我要在其上显示内容的代码都可以在这里找到:https://github.com/yy502/ePaperDisplay



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有