python光栅图像HEX代码

时间:2014-08-08 15:11:05

标签: python image python-2.7 raster

我目前使用ESCPOS命令集使用热敏打印机。

我得到了这张图片:

logo

他们给了我正确的十六进制代码我必须发送到打印机打印上面的图像。上图中的代码:

10 10 00 00 00 00 00 00 1F FF FF FE 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF C0 00 00 00 00 00 00 00 00 00 00 0F FF FF FF FF F8 00 00 00 00 00 00 00 00 00 00 3F FF FF FF FF FE 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF FF C0 00 00 00 00 00 00 00 00 07 FF FF FF FF FF FF F0 00 00 00 00 00 00 00 00 0F FF FF FF FF FF FF FC 00 00 00 00 0C 00 00 00 3F FF FF B0 00 7F FF FF 00 00 00 00 0C 00 00 00 7F FF F0 10 00 07 FF FF 80 00 00 00 00 00 00 01 FF FF 80 08 00 01 FF FF C0 00 00 00 00 00 00 03 FF FC F9 E8 00 00 3F FF E0 00 00 00 00 00 00 0F FF E0 00 18 00 00 0F FF F0 00 00 00 00 00 00 1F FF C0 00 00 00 00 03 FF FC 00 00 00 00 00 00 3F FF 00 00 00 00 00 00 FF FE 00 00 00 00 00 00 7F FC 00 00 00 00 08 00 7F FF 00 00 00 00 00 00 FF F8 00 00 00 00 00 00 1F FF 80 00 00 00 00 01 FF E0 00 00 00 00 00 00 0F FF C0 00 00 00 00 03 FF C0 00 00 00 00 00 00 03 FF E0 00 00 00 00 07 FF 00 00 00 00 00 0E 00 01 FF F0 00 00 00 00 0F FE 00 00 00 00 00 0C 00 00 FF F8 00 00 00 00 0F F8 00 00 00 00 00 1C 00 00 7F FC 00 00 00 00 1F F0 00 00 00 00 00 18 27 00 3F FC 00 00 00 00 3F E0 00 00 00 00 1F FF FF 40 1F FE 00 00 00 00 7F F0 00 00 00 01 FF FF FF F0 0F FF 00 00 00 00 FF A0 00 00 00 1F FF FF FF FF 07 FF 80 00 00 00 FF 20 00 00 00 7F FF FF FF FF C3 FF 80 00 00 01 FF 40 00 00 00 FF FF FF FF FF F1 FF C0 00 00 03 FF 40 00 00 07 FF FF FF FF FF F8 FF C0 00 00 03 FE 40 00 00 1F FF FF FF FF FF FC FF E0 00 00 07 FE 80 00 00 7F FF FF FF FF FF FE 7F F0 00 00 07 FC 80 00 01 FF FF FF FF FF FF FF 3F F8 00 00 0F F3 00 00 07 FF FF FF FF FF FF FF BF F8 00 00 0F F6 00 00 1F FF FF FF FF FF FF FF DF FC 00 00 1F EC 00 00 3F FF FF FF FF FF FF FF CF FC 00 00 1F E8 00 00 FF FF FF FF FF FF FF FF EF FE 00 00 3F C8 00 03 FF FF FF FF FF FF FF FF F7 FE 00 00 3F 88 00 07 FF FF FF FF FF FF FF FF F7 FE 00 00 3F 88 00 1F FF FF FF FF FF FF FF FF FB FF 00 00 7F 08 00 3F FF FF FF FF FF FF FF FF FB FF 00 00 7F 30 00 7F FF FF FF FF FF FF FF FF FD FF 00 00 FF 40 01 FF FF FF FF FF FF FF FF FF FD FF 80 00 FE 40 03 FF FF FF FF FF FF FF FF FF FE FF 80 00 FE 40 07 FF FF FF FF FF F3 FF FF FF FE FF 80 01 FC 40 0F FF FF FF FF FE 03 FF FF FF FE FF C0 01 FC 80 1F FF FF FF FF C0 03 FF FF FF FE 7F C0 03 FC 80 3F FF FF FF FE 00 03 FF DF FF FE 7F C0 0F FD 00 7F FF FF FF F8 00 03 FF C7 FF FE 7F E0 0B F8 00 FF FF FF FF E0 00 03 FF C7 FF FF 3F E0 13 FA 00 FF FF FF FF C0 00 03 FF C7 FF FE 3F E0 17 FE 01 FF FF FF FF FC 00 03 FF C7 FF FF 3F E0 17 F8 03 FF FF FF FF FF 00 03 FF C3 FF FE 3F E0 17 F0 03 FF FF FF FF FF C0 03 FF C3 FF FE 1F F0 17 F0 07 FF FF FF FF FF E0 03 FF C3 FF FE 1F F0 17 F0 07 FF FF FF FF FF E0 03 FF C3 FF FE 1F F0 17 F0 0F FF FF FF FF FF F0 03 FF C3 FF FE 1F F0 0F F0 0F FF FF FB FF FF F8 03 FF C3 FF FE 1F F0 0F F0 1F FF FF E7 FF FF F8 03 FF C3 FF FE 1F F0 0F F0 1F FF FF C7 FF FF FC 03 FF C3 FF FE 1F F8 0F F0 1F FF FF 8F FF FF FC 03 FF C3 FF FE 0F F8 0F F0 1F FF FF 1F FF FF FE 03 FF C3 FF FE 0F F8 0F F0 1F FF FE 3F FF FF FE 03 FF C3 FF FE 0F F8 0F F0 3F FF FE 1F FF FF FE 03 FF C3 FF FE 0F F8 0F F0 1F FF FC 3F FF BF FE 03 FF C1 FF FE 0F F8 0F F0 1F FF F8 3F FE 1F FE 03 FF C1 FF FE 0F F8 0F F0 1F FF F8 3F FC 1F FE 03 FF C1 FF FE 0F F8 0F F0 1F FF F0 3F FD DF FE 03 FF C1 FF FE 0F F8 07 F0 3F FF F0 3F F9 5F FE 07 FF C1 FF FE 0F F8 07 F0 1F FF F0 3F F9 5F FE 07 FF C1 FF FE 0F F8 07 F0 1F FF E0 7F F9 5F FF 0F FF C1 FF FE 0F F8 07 F0 1F FF E0 BF F9 5F FF FF FF 81 FF FE 0F F8 07 F0 1F FF E0 BF F9 5F FF FF FF 81 FF FE 0F F8 07 F0 1F FF E0 BF F9 DF FF FF FF 81 FF FE 1F F8 07 F0 3F FF E0 BF F8 DF FF FF FF 81 FF FE 1F F8 07 F0 3F FF E0 BF F8 1F FF FF FF 81 FF F8 1F F0 07 F0 3F FF E0 BF F8 0F FF FF FF 01 FF C0 1F F0 07 F0 3F FF E0 BF F9 CF FF FF FE 01 FE 00 3F F0 07 F0 3F FF E0 BF F9 6F FF FF FC 00 F0 00 3F F0 03 F0 3F FF E0 BF F9 37 FF FF FC 00 00 00 3F F0 03 F0 3F FF E0 BF F9 17 FF FF F8 00 00 00 3F F0 03 F8 3F FF E0 BF F9 13 FF FF F0 00 00 00 3F F0 03 F8 3F FF E0 BF F9 09 FF FF E0 00 00 00 3F E0 03 F8 3F FF E0 FF F9 0C 7F FF E0 01 00 00 7F E0 01 F8 3F FF E0 3F F9 06 3F FF 10 03 80 00 7F E0 01 FC 3F FF E0 3F F9 03 0F FC 60 01 C0 00 7F E0 01 FC 3F FF E0 3F F9 00 E0 01 80 01 C0 00 7F C0 01 FE 3F FF E0 3F F9 00 37 FE 00 08 80 00 FF C0 00 FE 3F FF E0 3F F9 00 18 00 00 00 00 01 FF C0 00 FE 3F FF E0 3F F9 00 00 00 00 00 00 01 FF 80 00 FE 3F FF E0 3F F9 00 00 00 00 00 00 01 FF 80 00 FF 3F FF E0 3F F9 00 00 00 00 00 00 03 FF 80 00 7F 3F FF E0 3F F9 00 00 00 00 00 00 03 FF 00 00 7F FF FF E0 3F F9 00 00 00 00 00 00 03 FF 00 00 3F FF FF E0 1F E1 00 00 00 00 00 00 07 FF 00 00 3F FF FF E0 1F C9 00 00 00 00 00 00 07 FE 00 00 3F FF FF E0 10 7E 00 00 00 00 00 00 0F FE 00 00 1F FF FF C0 08 7E 00 00 00 00 00 00 0F FC 00 00 1F FF FF 80 07 FE 00 00 00 00 00 00 1F FC 00 00 0F FF FE 01 81 F2 00 00 00 00 00 00 1F F8 00 00 0F FF E0 00 01 F0 00 00 00 00 00 00 3F F8 00 00 07 FF 80 00 00 00 00 00 00 00 00 00 3F F0 00 00 07 FE 80 00 00 00 00 00 00 00 00 00 7F F0 00 00 03 FF 00 00 00 00 00 00 00 00 00 00 FF E0 00 00 03 FE 00 00 00 00 00 00 00 00 00 01 FF E0 00 00 01 FF 00 00 00 00 00 00 00 00 00 03 FF C0 00 00 00 FF 80 00 00 00 00 00 00 00 00 03 FF 80 00 00 00 FF C0 00 00 00 00 00 00 00 00 07 FF 80 00 00 00 7F C0 00 00 00 00 00 00 00 00 0F FF 00 00 00 00 3F F0 00 00 00 00 00 00 00 00 1F FE 00 00 00 00 1F F8 00 00 00 00 00 00 00 00 3F FC 00 00 00 00 0F FC 00 00 00 00 00 00 00 00 7F FC 00 00 00 00 0F FE 00 00 00 00 00 00 00 00 FF F8 00 00 00 00 07 FF 00 00 00 00 00 00 00 01 FF F0 00 00 00 00 03 FF C0 00 00 00 00 00 00 03 FF E0 00 00 00 00 01 FF E0 00 00 00 00 00 00 07 FF C0 00 00 00 00 00 FF F0 00 00 00 00 00 00 1F FF 80 00 00 00 00 00 7F FC 00 00 00 00 00 00 3F FF 00 00 00 00 00 00 3F FE 00 00 00 00 00 00 FF FE 00 00 00 00 00 00 1F FF 80 00 00 00 00 03 FF FC 00 00 00 00 00 00 07 FF E0 C0 00 00 00 1F FF F8 00 00 00 00 00 00 03 FF F9 3E 00 00 00 7F FF E0 00 00 00 00 00 00 01 FF FF 83 FE 40 1F FF FF C0 00 00 00 00 00 00 00 7F FF FF E0 CF FF FF FF 00 00 00 00 00 00 00 00 3F FF FF FF FF FF FF FE 00 00 00 00 00 00 00 00 0F FF FF FF FF FF FF FC 00 00 00 00 00 00 00 00 07 FF FF FF FF FF FF E0 00 00 00 00 00 00 00 00 03 FF FF FF FF FF FF 80 00 00 00 00 00 00 00 00 02 1F FF FF FF FF FE 08 00 00 00 00 00 00 00 00 03 FF FF FF FF FF FF F0 00 00 00 00 

他们告诉我使用光栅图像'生成代码。我找不到任何解决方案,为我返回上面图片的正确代码。

在进入上面显示的图片时,知道如何让函数返回上面的代码吗?

1 个答案:

答案 0 :(得分:4)

您获得的栅格数据是128行,每行16个字节,前导2个字节设置栅格大小(128 x 128)。每个位是0或1,定义不打印(白色)或打印(例如黑色),例如,一种简单的黑白格式。

看起来这些字节是从上到下编码像素而不是从左到右编码,因为你生成的字节会生成翻转和旋转的图像:

raster image, inverted

但如果您将数据视为最重要的位是左上方并且跟随位向下而不是向右,则得到:

raster image, decoded

将图像转换为黑白后,您必须自己从图像中生成相同的数据。根据PIL图像'1'模式图像,逐行读取从上到下的黑白图像数据。如果您转置图像(翻转和旋转),您可以将列放入行中并使用Image.tobytes()在一个步骤中获取所需的字节,并使用1;I模式为原始编码器生成反转数据:

from PIL import Image, ImageOps
img = Image.open("original.png")
bwimg = img.convert('1')
transposed = bwimg.transpose(Image.ROTATE_270).transpose(Image.FLIP_LEFT_RIGHT)
data = bytearray(transposed.size)
data.extend(transposed.tobytes('raw', '1;I'))

一旦你有一个正确方向的黑白图像(模式1),你所要做的就是将大小作为前2个字节,tobytes()方法产生原始图像数据(每像素1位)。

原始编码器的'1;I'模式生成0表示白色,1表示黑色而不是默认0表示黑色,1表示白色模式,请参阅raw modes table

对于输入图像(与您给出的栅格数据略有不同),产生:

80 80 00 00 00 00 00 00 01 ff ff c0 00 00 00 00 00 00 00 00 00 00 00 00 0f ff ff fc 00 00 00 00 00 00 00 00 00 00 00 00 ff ff ff ff 80 00 00 00 00 00 00 00 00 00 00 07 ff ff ff ff f0 00 00 00 00 00 00 00 00 00 00 1f ff ff ff ff fc 00 00 00 00 00 00 00 00 00 00 ff ff fb 7f ff ff 80 00 00 00 00 00 00 00 00 01 ff fd 00 00 3f ff e0 00 00 00 00 04 00 00 00 07 ff a0 00 00 03 ff fc 00 00 00 00 0c 00 00 00 1f fc 00 00 00 00 7f fe 00 00 00 00 00 00 00 00 3f f0 00 00 00 00 1f ff 00 00 00 00 00 00 00 00 ff c0 00 00 00 00 07 ff 80 00 00 00 00 00 00 01 ff 00 00 00 00 00 01 ff c0 00 00 00 00 00 00 03 fc 00 00 00 00 00 00 ff e0 00 00 00 00 00 00 0f f8 00 00 00 00 00 00 1f f8 00 00 00 00 00 00 1f e0 00 00 00 00 00 00 1f f8 00 00 00 00 00 00 3f c0 00 00 00 00 00 00 07 fe 00 00 00 00 00 00 7f 00 00 00 00 00 00 00 01 ff 00 00 00 00 00 00 fe 00 00 00 00 00 00 00 01 ff 80 00 00 00 00 01 f8 00 00 00 00 00 00 00 00 7f 80 00 00 00 00 03 f0 00 00 00 00 00 00 00 00 3f e0 00 00 00 00 07 e0 00 00 00 00 00 00 00 00 1f e0 00 00 00 00 0f c0 00 00 00 00 00 00 00 00 0f f0 00 00 00 00 0f 00 00 00 00 00 00 00 00 00 07 fc 00 00 00 00 1f 00 00 00 00 00 09 bf fc 00 03 fc 00 00 00 00 3e 00 00 00 00 00 76 40 13 e0 01 fe 00 00 00 00 7c 00 00 00 00 0b 89 bf fd 1c 00 fe 00 00 00 00 7a 00 00 00 00 74 77 ff ff f3 80 ff 00 00 00 00 f8 00 00 00 00 8b df ff ff fe 60 7f 80 00 00 01 f0 00 00 00 03 6d ff ff ff ff b0 3f 80 00 00 01 f0 00 00 00 1c bf ff ff ff ff c8 3f e0 00 00 03 e0 00 00 00 23 7f ff ff ff ff f8 1f e0 00 00 03 c0 00 00 01 dd ff ff ff ff ff fc 0f f0 00 00 07 e0 00 00 02 2f ff ff ff ff ff ff 0f f0 00 00 07 80 00 00 0d 7f ff ff ff ff ff fd 07 f8 00 00 0f 80 00 00 25 ff ff ff ff ff ff ff 47 f8 00 00 0f 00 00 00 57 ff ff ff ff ff ff ff 83 f8 00 00 1f 00 00 01 9f ff ff ff ff ff ff ff c3 fc 00 00 1e 00 00 02 7f ff ff ff ff ff ff ff a1 fc 00 00 3c 00 00 0d ff ff ff ff eb 6f ff ff f0 fc 00 00 3e 00 00 12 ff ff ff fe bc b1 ff ff c0 fe 00 00 3c 00 00 67 ff ff ff db 67 de 3f ff f8 fe 00 00 78 00 00 1f ff ff ff 66 91 63 cf ff e8 7f 00 00 7c 00 01 af ff ff f9 b8 00 9d 73 ff f0 7e 00 00 78 00 02 7f ff ff ae 80 01 7b 9d ff fc 3f 80 00 f8 00 04 ff ff fe d9 00 01 3e 06 ff e8 3f 00 00 f0 00 0b ff ff f7 60 00 00 fb 82 ff f8 3f 80 00 f0 00 11 ff ff fa 80 00 01 3e 03 7f ec 3f 80 01 f0 00 2f ff ff cc 00 00 01 7b 01 7f f8 1f 80 01 e0 00 47 ff ff 70 00 00 01 7d 03 3f ec 1f c0 01 e0 00 3f ff fe 80 00 00 01 3a 01 ff f0 1f c0 03 e0 00 9f ff f9 43 e8 00 01 ff 81 3f fc 1f c0 01 e0 01 7f ff ee 0c 14 00 00 3a 01 bf e8 0f c0 03 e0 00 3f ff f0 13 ff 80 01 bd 01 5f f4 0f e0 03 c0 03 ff ff a8 4f fe 40 01 7b 01 bf f8 0f c0 03 e0 00 7f ff 60 bf ff 80 00 be 00 bf ec 0f e0 07 c0 07 ff fe 81 2f ff 60 01 3b 01 5f f4 0f e0 03 e0 01 ff fd 80 ff ff d0 01 fe 80 bf f8 0f e0 07 c0 0d ff fa 02 7f ff a0 00 3b 00 bf ec 07 e0 03 e0 03 ff f4 01 ff ff f8 01 bd 01 5f f4 0f e0 07 c0 0b ff fa 05 fe df c0 01 7b 00 bf f8 07 f0 03 e0 0d ff d4 0b f5 27 f8 00 be 00 bf f4 07 e0 07 c0 03 ff e8 09 fe bb d8 01 3b 00 bf ec 07 e0 03 e0 0b ff d0 07 d0 05 e8 01 fe 81 5f f8 07 f0 07 c0 0b ff 60 1b e8 0b d4 00 3b 00 bf f4 07 e0 03 e0 07 ff 80 03 b0 0b f8 01 be 00 bf f8 07 f0 07 c0 0b ff e0 1f e0 09 cc 01 7b 00 bf ec 07 e0 03 e0 0b fe 80 03 b0 0f f0 01 3d 80 bf f4 07 f0 07 c0 05 ff 40 1b d0 01 dc 02 fe 00 bf f8 07 e0 03 c0 0b ff 80 17 a0 1b e8 01 3b 00 bf ec 07 e0 07 e0 0b fe c0 13 f0 0b f4 06 fd 00 bf f4 07 f0 03 c0 07 ff 00 17 90 09 ee 99 fe 00 bf b8 07 e0 07 e0 0b ff c0 13 e0 0f fb e6 f5 00 b6 d4 0f e0 03 c0 0b fe 80 1f b0 01 fc 1b fa 00 bf 68 07 e0 03 e0 15 ff 40 03 50 0b ff ef ec 00 a9 00 0f c0 03 c0 0b ff 80 1f e0 05 ff bf f4 00 de 00 0f e0 03 e0 0b fe c0 03 b0 02 ff ff d8 00 20 00 0f e0 03 c0 17 ff 00 1f d0 03 7f ff f0 00 00 00 0f c0 01 e0 09 ff c0 03 60 00 ff ff 50 00 00 00 0f e0 03 e0 17 fe 80 1f d0 03 3f ff a0 00 00 00 0f c0 01 e0 0b ff 40 03 b0 00 df fa c0 00 00 00 1f c0 01 f0 15 ff 80 1f d0 00 2f ed 00 00 00 00 1f c0 01 f0 0b fe c0 03 60 00 12 b4 00 00 00 00 3f 80 00 f0 17 ff 40 1f d0 00 0a a8 00 00 00 00 1f c0 00 f0 09 ff 80 03 b0 00 00 00 00 00 00 00 3f 80 00 f8 17 ff 40 1f e0 00 00 00 00 00 00 00 3f 80 00 f8 0b fe 80 03 b0 00 00 00 00 00 00 00 7f 00 00 7c 15 ff c0 1f 50 00 00 00 00 00 00 00 3f 00 00 7c 0b ff 00 03 e0 00 00 00 00 00 00 00 ff 00 00 7c 17 ff c0 1f d0 00 00 00 00 00 00 00 fe 00 00 3c 09 fe 80 03 30 00 00 00 00 00 00 00 fe 00 00 3e 17 ff 40 1d e0 00 00 00 00 00 00 01 fe 00 00 3e 0b ff c0 03 00 00 00 00 00 00 00 01 fc 00 00 1e 15 fe 80 0c 80 00 00 00 00 00 00 01 fc 00 00 1f 0b ff 40 00 00 00 00 00 00 00 00 03 f8 00 00 0f 97 fa 80 00 00 00 00 00 00 00 00 03 f8 00 00 0f 89 a5 00 00 00 00 00 00 00 00 00 07 f0 00 00 07 96 fa 00 00 00 00 00 00 00 00 00 07 f0 00 00 07 eb 00 00 00 00 00 00 00 00 00 00 0f e0 00 00 03 e4 80 00 00 00 00 00 00 00 00 00 0f e0 00 00 03 f2 00 00 00 00 00 00 00 00 00 00 1f c0 00 00 01 f0 00 00 00 00 00 00 00 00 00 00 3f 80 00 00 01 f8 00 00 00 00 00 00 00 00 00 00 3f c0 00 00 00 f8 00 00 00 00 00 00 00 00 00 00 7f 00 00 00 00 7c 00 00 00 00 00 00 00 00 00 00 ff 00 00 00 00 7e 00 00 00 00 00 00 00 00 00 01 fe 00 00 00 00 3f 00 00 00 00 00 00 00 00 00 01 fc 00 00 00 00 1f 80 00 00 00 00 00 00 00 00 03 fa 00 00 00 00 0f 80 00 00 00 00 00 00 00 00 07 f8 00 00 00 00 07 e0 00 00 00 00 00 00 00 00 0f f0 00 00 00 00 07 e0 00 00 00 00 00 00 00 00 1f e0 00 00 00 00 03 f8 00 00 00 00 00 00 00 00 3f d0 00 00 00 00 01 fc 00 00 00 00 00 00 00 00 7f c0 00 00 00 00 00 fc 00 00 00 00 00 00 00 00 ff 00 00 00 00 00 00 7f 80 00 00 00 00 00 00 01 ff 00 00 00 00 00 00 1f 80 00 00 00 00 00 00 07 fc 00 00 00 00 00 00 1f e0 00 00 00 00 00 00 0f fc 00 00 00 00 00 00 07 f8 00 00 00 00 00 00 3f f0 00 00 00 00 00 00 07 fc 00 00 00 00 00 00 7f e0 00 00 00 00 00 00 01 ff 00 00 00 00 00 03 ff c0 00 00 00 00 00 00 00 7f c0 00 00 00 00 0f ff 80 00 00 00 00 00 00 00 3f f0 00 00 00 00 5f fe 00 00 00 00 00 00 00 00 1f fe 80 00 00 0b ff f8 00 00 00 00 00 00 00 00 07 ff fa 20 8a ff ff f0 00 00 00 00 00 00 00 00 01 ff ff ff ff ff ff c0 00 00 00 00 00 00 00 00 00 ff ff ff ff ff ff 00 00 00 00 00 00 00 00 00 00 07 ff ff ff ff f8 00 00 00 00 00 00 00 00 00 00 02 ff ff ff fe a0 00 00 00 00 00 00 00 00 00 00 00 12 92 aa 80 00 00 00 00 00 00

或图片:

final result

您可能需要调整如何将颜色或灰度图像转换为黑白图像以获得更好的效果。我的忍者头像变成了:

Ninja

这个过程。这些图像都已旋转/翻转以表示将要打印的图像数据。