|
import binascii |
|
import math |
|
|
|
# Colours |
|
DR = chr(0x2591) # Dark |
|
MD = chr(0x2592) # More dark |
|
LD = chr(0x2593) # Light dark |
|
LT = chr(0x2588) # Light |
|
|
|
# Box characters |
|
BOXCLT = chr(0x250C) # Top, left |
|
BOXCRT = chr(0x2510) # Top, right |
|
BOXCLB = chr(0x2514) # Bottom, left |
|
BOXCRB = chr(0x2518) # Bottom, right |
|
BOXLH = " " # chr(0x2500) # Horizontal line |
|
BOXLV = chr(0x2502) # Vertical line |
|
|
|
# Cell size |
|
CEL = 2 |
|
|
|
# Returns the number of bytes needed for a 1-bit image with the |
|
# size provided. |
|
def bytes_needed(width, height): |
|
area = width * height |
|
bytes_needed = math.ceil(area / 8) |
|
bytes_needed = bytes_needed + 2 # for width and height, |
|
# and these are put at the beginning. |
|
return bytes_needed |
|
|
|
def create(width, height): |
|
img = bytearray(bytes_needed(width, height)) |
|
img[0] = width |
|
img[1] = height |
|
return img |
|
|
|
def place(img, data): |
|
gendata = unhexlify(data) |
|
|
|
ib_len = len(img) - 2 |
|
gd_len = len(gendata) |
|
|
|
if gd_len > ib_len: |
|
limit = ib_len |
|
else: |
|
limit = gd_len |
|
|
|
for x in range(limit): |
|
img[x+2] = gendata[x] |
|
return img |
|
|
|
def unhexlify(hex): |
|
img = [] |
|
odd = len(hex) % 2 |
|
length = len(hex) - odd |
|
|
|
for x in range(0, length, 2): |
|
img.append( ( int(hex[x], 16) & 0x0f ) << 4 | int(hex[x+1], 16)) |
|
if odd: |
|
img.append(int(hex[-1], 16) << 4) |
|
|
|
return bytes(img) |
|
|
|
def hexlify(img): |
|
return str(binascii.hexlify(img), "latin-1") |
|
|
|
def blit(img): |
|
bitstream = img[2:] |
|
stop = img[0] * img[1] |
|
|
|
for i in range(stop): |
|
if bitstream[i // 8] & (0x80 >> (i % 8)): |
|
print(LT * CEL, end="") |
|
else: |
|
print(DR * CEL, end="") |
|
if (i + 1) % img[0] == 0: |
|
print() |
|
|
|
def flip(img, x, y): |
|
if x < 0 or x >= img[0]: |
|
raise BaseException("Outside the range of width.") |
|
if y < 0 or y >= img[1]: |
|
raise BaseException("Outside the range of height.") |
|
|
|
bits_total = (y * img[0]) + x |
|
index = bits_total // 8 |
|
bit_mask = 0x80 >> (bits_total % 8) |
|
img[2+index] = img[2+index] ^ bit_mask |
|
|
|
#print("Bit-Access: %d" % bits_total) |
|
#print("Index: %d" % index) |
|
#print("Bit-Mask: %d" % bit_mask) |
|
|
|
def get(img, x, y): |
|
bits_total = (y * img[0]) + x |
|
index = bits_total // 8 |
|
bit_mask = 0x80 >> (bits_total % 8) |
|
return img[2+index] & bit_mask |
|
|
|
def rect(img, x1, y1, x2, y2): |
|
y = y1 |
|
while (y <= y2): |
|
x = x1 |
|
while (x <= x2): |
|
flip(img, x, y) |
|
x = x + 1 |
|
y = y + 1 |
|
try: |
|
import os |
|
from PIL import Image |
|
from PIL import ImageDraw |
|
|
|
def bitmap(img, outputname): |
|
border = 5 |
|
cell = 32 |
|
|
|
size = ((img[0]) * (cell + 1) + (border * 2) - 1, |
|
(img[1]) * (cell + 1) + (border * 2) - 1) |
|
image = Image.new("1", size, 1) |
|
drawing = ImageDraw.Draw(image) |
|
|
|
for i in range(img[0] * img[1]): |
|
bit_set = img[2 + (i // 8)] & (0x80 >> (i % 8)) |
|
x = i % img[0] |
|
y = i // img[0] |
|
|
|
ox = border + (x * (cell + 1)) |
|
oy = border + (y * (cell + 1)) |
|
rect = (ox, oy, ox + cell - 1, oy + cell - 1) |
|
|
|
if bit_set: |
|
drawing.rectangle(rect, 0, 0, 0) |
|
#else: |
|
# drawing.rectangle(rect, 1, 0) |
|
try: |
|
image.save(outputname) |
|
except FileNotFoundError: |
|
os.makedirs(os.path.dirname(outputname)) |
|
image.save(outputname) |
|
|
|
except ModuleNotFoundError: |
|
print("Init: imaging lib not present.") |
|
|
|
|
|
if __name__ == "__main__": |
|
import sys |
|
if len(sys.argv)-1 == 1: |
|
img = open(sys.argv[1], "rb").read() |
|
blit(img) |