def show_24bit_bitmap(filename): """ Display small picture on terminal by reducing in to CharPixel """ f = open(filename, "rb") bmp = BmpReader(f) for line in range(bmp.height): bmp.seek_pix((0, line)) print("".join([charpix(*bmp.read_pix()) for col in range(bmp.width)])) f.close()
def test_header(filename): f = open(filename, "rb") bmp = BmpReader( f, check=False) # Disable check compatibility. So can display header info print('Image size (WxH) : %i x %i' % (bmp.width, bmp.height)) print('Startbit @ : %i' % bmp.startbit) print('Bit Per Pixels : %i' % bmp.bpp) print('Compression : %i' % bmp.compression) print('Colors used : %i (for indexed colors)' % bmp.colors_used) f.close()
def open_image(filename): """ Helper that detect image type based on file extension. Open the appropriate image reader (eg: BmpReader) and then the ClipReader over the reader. a ClipReader on the target image """ reader = None if '.bmp' in filename: _f = open(filename, "rb") from bmp import BmpReader reader = BmpReader(_f) if not (reader): raise Exception('No image reader support available!') # Encapsulate the reader into the clipper clip = ClipReader(reader) return clip
def test_24bit_bitmap(filename): f = open(filename, "rb") bmp = BmpReader(f) print('Image size (WxH) : %i x %i' % (bmp.width, bmp.height)) # Bitmap may have padding bytes in the file, so seek_pix is required before reading each line print('--- Line 1 ---') for i in range(bmp.width): print("%i : %s" % (i, bmp.read_pix())) print('--- Line 2 ---') bmp.seek_pix((0, 1)) # x=0, y=1 --> 2th line for i in range(bmp.width): print("%i : %s" % (i, bmp.read_pix())) print('--- Line 6 ---') # Move file cursor to a given pixel bmp.seek_pix((0, 5)) # x=0, y=5 --> 6th line for i in range(bmp.width): print("%i : %s" % (i, bmp.read_pix())) f.close()
def test_clip_reading(filename, area1, area2): """ Perform the reading of a part of the image """ f = open(filename, "rb") bmp = BmpReader(f) clip = ClipReader(bmp) for x, y, w, h in [area1, area2]: # area are tuple of (x,y,w,h) print("=" * 30) print(" Area x,y -> w,h = %i,%i -> %i,%i" % (x, y, w, h)) print("=" * 30) clip.clip(x, y, w, h) # 31,0,6,2 for x,y,width,height for i in range(clip.height): print("--- Clipped line %i ---" % i) for j in range(clip.width): print("%3i : %s" % (j, clip.read_pix())) print("") print("--- show clipping area ---") # Now integrated to clip.show() #clip.clip( x,y,w,h ) # 31,0,6,2 for x,y,width,height #for line in range( h ): # print( "".join([ charpix( *clip.read_pix() ) for col in range(w) ]) ) clip.show(reseek=True) # Display aread on the console f.close()
disp.set_origin( 0, 0) # force axis origin @ top-left corner (instead of screen center) # Convert RGB888 to RGB565 def rgb24_to_rgb16(r, g, b): """ Convert a RGB888 value to RGB565 """ return (((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3)) # Open the image file filename = 'mpy.bmp' print("Open %s" % filename) f = open(filename, "rb") # Use the reader to extract the pixel data bmp = BmpReader(f) start = time.ticks_ms() # Transfert the data to memory buffer for line in range(bmp.height): bmp.seek_pix((0, line)) for col in range(bmp.width): disp.pixel((col, line), rgb24_to_rgb16(*bmp.read_pix())) f.close() #--- Fill Screen --- print("Drawing tooks %i ms" % (time.ticks_ms() - start)) print("That's all folks")