def rgba2bytes(sc, img, _type): write_pixel = None if _type in (0, 1): def write_pixel(pixel): return struct.pack('4B', *pixel) if _type == 2: def write_pixel(pixel): r, g, b, a = pixel return struct.pack( '<H', a >> 4 | b >> 4 << 4 | g >> 4 << 8 | r >> 4 << 12) if _type == 3: def write_pixel(pixel): r, g, b, a = pixel return struct.pack( '<H', a >> 7 | b >> 3 << 1 | g >> 3 << 6 | r >> 3 << 11) if _type == 4: def write_pixel(pixel): r, g, b = pixel return struct.pack('<H', b >> 3 | g >> 2 << 5 | r >> 3 << 11) if _type == 6: def write_pixel(pixel): return struct.pack('2B', *pixel[::-1]) if _type == 10: def write_pixel(pixel): return struct.pack('B', pixel) if write_pixel is not None: width, height = img.size pix = img.getdata() point = -1 for y in range(height): for x in range(width): sc.write(write_pixel(pix[y * width + x])) curr = Console.percent(y, height) if curr > point: Console.progress_bar(locale.writing_pic, y, height) point = curr
def bytes2rgba(data: Reader, _type, img, pix): read_pixel = None if _type in (0, 1): def read_pixel(): return data.read_ubyte(), data.read_ubyte(), data.read_ubyte( ), data.read_ubyte() elif _type == 2: def read_pixel(): p = data.read_uint16() return (p >> 12 & 15) << 4, (p >> 8 & 15) << 4, ( p >> 4 & 15) << 4, (p >> 0 & 15) << 4 elif _type == 3: def read_pixel(): p = data.read_uint16() return (p >> 11 & 31) << 3, (p >> 6 & 31) << 3, ( p >> 1 & 31) << 3, (p & 255) << 7 elif _type == 4: def read_pixel(): p = data.read_uint16() return (p >> 11 & 31) << 3, (p >> 5 & 63) << 2, (p & 31) << 3 elif _type == 6: def read_pixel(): return (data.read_ubyte(), data.read_ubyte())[::-1] elif _type == 10: def read_pixel(): return data.read_ubyte() if read_pixel is not None: width, height = img.size point = -1 for y in range(height): for x in range(width): pix.append(read_pixel()) curr = Console.percent(y, height) if curr > point: Console.progress_bar(locale.crt_pic, y, height) point = curr print() img.putdata(pix)