如何在Python中将8bpp位图转换为1bpp位图
问题描述
现有代码可生成灰度(8bpp)图像,但需要为打印机生成1bpp(二进制)图像。已安装Pillow,习惯从PIL导入Image,编写了conv_to_1bpp方法将值为255的像素设为1,但生成的图像仍为8bpp格式。
相关代码
from PIL import Image import numpy as np import os import sys import struct class BmpMaker: def __init__(self): self.ffp = None self.img_width = None self.img_height = None self.ws_activation = None self.bpp = None def make_image(self, img_width, img_height, ws_activation): """Creates a bitmap.""" self.ffp = None self.img_width = img_width self.img_height = img_height self.ws_activation = ws_activation # Create new black image - L mode for b/w img = Image.new('L', (img_width, img_height)) # Convert to Numpy array for easy processing na = np.array(img) # set the amount of white space activation white_rows = ws_activation # make white lines na[0:white_rows, 0:999] = 255 # Revert to PIL Image from Numpy array and save self.ffp = self.make_img_title(img_width, img_height, ws_activation) Image.fromarray(na).save(self.ffp) self.bpp = self.get_bitdepth() return self.ffp def make_img_title(self, img_width, img_height, ws_activation): if ws_activation == 0: mystr = f"{img_height}x{img_width}" elif ws_activation is not None: mystr = f"{ws_activation}_{img_height}x{img_width}" self.ffp = os.path.join(os.getcwd(), mystr + ".bmp") print("img_ffp : ", self.ffp) return self.ffp def get_bitdepth(self): # Read first 100 bytes with open(self.ffp, 'rb') as f: BMP = f.read(100) if BMP[0:2] != b'BM': sys.exit('ERROR: Incorrect BMP signature') # Get BITMAPINFOHEADER size BITMAPINFOHEADERSIZE = struct.unpack('<i', BMP[14:18])[0] okSizes = [40, 52, 56, 108, 124] if BITMAPINFOHEADERSIZE not in okSizes: sys.exit(f'ERROR: BITMAPINFOHEADER size was {BITMAPINFOHEADERSIZE},' f' expected one of {okSizes}') # Get bits per pixel self.bpp = struct.unpack('<H', BMP[28:30])[0] print(f'bbp: {self.bpp}') return self.bpp def get_img_bitdepth(self, img): # Read first 100 bytes with open(img, 'rb') as f: BMP = f.read(100) if BMP[0:2] != b'BM': sys.exit('ERROR: Incorrect BMP signature') # Get BITMAPINFOHEADER size BITMAPINFOHEADERSIZE = struct.unpack('<i', BMP[14:18])[0] okSizes = [40, 52, 56, 108, 124] if BITMAPINFOHEADERSIZE not in okSizes: sys.exit(f'ERROR: BITMAPINFOHEADER size was {BITMAPINFOHEADERSIZE},' f' expected one of {okSizes}') # Get bits per pixel img_bpp = struct.unpack('<H', BMP[28:30])[0] print(f'bbp: {img_bpp}') return img_bpp def conv_to_1bpp(self): img = Image.open(self.ffp) arr = np.array(img) # Set all values at indices where the array equals 255 to 1. arr[arr==255] = 1 print('\n'*3, arr) Image.fromarray(arr).save("my_img.bmp") print(self.get_img_bitdepth("my_img.bmp")) if __name__ == "__main__": import PIL print('Pillow Version:', PIL.__version__) bm = BmpMaker() base_image = bm.make_image(1000, 8, 5) print("bitmap ffp: ", bm.ffp) print("bitmap bitdepth : ", bm.bpp) bm.conv_to_1bpp()
内容的提问来源于stack exchange,提问作者Windy71
相关产品推荐
相关产品推荐

