def debug(start, end): end += 1 for l in range(start, end): for h in range(start, end): x = BMP(str(l) + str(h), l, h, 24) x.fill((randint(0, 255), randint(0,255), randint(0,255))) x.save()
def __init__(self, file_path, file_name_no_ext, build_folder_path, info): bmp = BMP(file_path) self.__file_path = file_path self.__file_name_no_ext = file_name_no_ext self.__build_folder_path = build_folder_path self.__colors_count = bmp.colors_count width = bmp.width height = bmp.height if width == 256 and height == 256: self.__sbb = False elif (width == 256 and height == 512) or (width == 512 and height == 256) or (width == 512 and height == 512): self.__sbb = True else: raise ValueError('Invalid regular BG size: (' + str(width) + 'x' + str(height) + ')' + RegularBgItem.valid_sizes_message()) self.__width = int(width / 8) self.__height = int(height / 8) self.__bpp_8 = False try: self.__repeated_tiles_reduction = bool( info['repeated_tiles_reduction']) except KeyError: self.__repeated_tiles_reduction = True try: self.__flipped_tiles_reduction = bool( info['flipped_tiles_reduction']) except KeyError: self.__flipped_tiles_reduction = True if self.__colors_count > 16: try: bpp_mode = str(info['bpp_mode']) except KeyError: bpp_mode = 'bpp_8' if bpp_mode == 'bpp_8': self.__bpp_8 = True elif bpp_mode == 'bpp_4_auto': self.__file_path = self.__build_folder_path + '/' + file_name_no_ext + '.bn_quantized.bmp' print(' Generating bpp4 image in ' + self.__file_path + '...') start = time.time() self.__colors_count = bmp.quantize(self.__file_path) end = time.time() print(' bpp4 image with ' + str(self.__colors_count) + ' colors generated in ' + str(int((end - start) * 1000)) + ' milliseconds') elif bpp_mode != 'bpp_4_manual': raise ValueError('Invalid BPP mode: ' + bpp_mode)
def console(): try: bmp = BMP(args.image) if args.out_file: with open(args.out_file, "w") as f: f.write(bmp.get_bitmap_file_header().get_all_info()) f.write(bmp.get_bitmap_info().get_all_info()) else: print(bmp.get_bitmap_file_header().get_all_info()) print(bmp.get_bitmap_info().get_all_info()) except Exception as e: sys.exit("Error: {0}".format(e))
def __init__(self, file_path, file_name_no_ext, build_folder_path): bmp = BMP(file_path) self.__file_path = file_path self.__file_name_no_ext = file_name_no_ext self.__build_folder_path = build_folder_path self.__colors_count = bmp.colors_count self.__bpp_8 = self.__colors_count > 16
def __init__(self, file_path, file_name_no_ext, build_folder_path, info): bmp = BMP(file_path) self.__file_path = file_path self.__file_name_no_ext = file_name_no_ext self.__build_folder_path = build_folder_path self.__colors_count = bmp.colors_count width = bmp.width height = bmp.height if width != 128 and width % 256 != 0: raise ValueError( 'Affine BGs width must be 128 or divisible by 256: ' + str(width)) if height != 128 and height % 256 != 0: raise ValueError( 'Affine BGs height must be 128 or divisible by 256: ' + str(height)) self.__width = int(width / 8) self.__height = int(height / 8) try: self.__repeated_tiles_reduction = bool( info['repeated_tiles_reduction']) except KeyError: self.__repeated_tiles_reduction = True
def fill_info(self): self.bmp = BMP(self.image_path) bitmap_file_header = self.bmp.get_bitmap_file_header() bitmap_info = self.bmp.get_bitmap_info() all_list_info =\ bitmap_file_header.get_list_info() + bitmap_info.get_list_info() tableInfo = TableInfo(all_list_info) self.grid.addWidget(tableInfo.get_table_info(), 0, 0, 2, 1)
def select_image_2(self): file, file_type = QFileDialog.getOpenFileName(parent=None, caption="选择图像", directory=os.getcwd(), filter="bmp文件(*.bmp)") self.textEdit_2.setText(file) img = BMP(file) image = img.bmp_data #image = image.astype(np.int8) self.image_1 = img showImage = QtGui.QImage(image.astype(np.int8), image.shape[1], image.shape[0], QtGui.QImage.Format_RGB888) pix = QtGui.QPixmap.fromImage(showImage) item = QGraphicsPixmapItem(pix) # 创建像素图元 scene = QGraphicsScene() # 创建场景 scene.addItem(item) self.graphicsView_1.setScene(scene) # 将场景添加至视图
def dataReceived(self, data): """ Data has been received. """ self.buf += data while len(self.buf) > self.message.length: # self._logger.debug("Iterating; buf_len: %d msg_len: %d" % (len(self.buf), self.message.length)) tmp = self.buf[0:self.message.length] self.buf = self.buf[self.message.length:] if self.message.consume(tmp): # message completely parsed # fetch message source and save data self.message.source = self.transport.getPeer() self.factory.store.store(self.message) # create new message self.message = BMP.BMPMessage()
### ### XORs the bitmap data of two images together... why? Uh, I'm not quite sure... ### import sys, struct from bmp import BMP from cStringIO import StringIO if len(sys.argv) != 3: print 'I need two arguments, and the images provided need to have the same dimensions and color depth!' exit(1) else: first_filename = sys.argv[1] second_filename = sys.argv[2] first = BMP(first_filename) second = BMP(second_filename) if first.width != second.width or first.height != second.height or first.bpp != second.bpp: print 'The dimensions and/or bpp differs between the input images!' exit(1) print 'Images are {0}x{1} @ {2} bpp'.format(first.width, first.height, first.bpp) print 'Working...', sys.stdout.flush() out_bitmap_data = "" ff = StringIO(first.bitmap_data) sf = StringIO(second.bitmap_data)
def process(input_file_path, output_file_path): bmp = BMP(input_file_path) bmp.quantize(output_file_path)
def __init__(self, factory): self.factory = factory self._logger = logging.getLogger(self.__class__.__name__) self.message = BMP.BMPMessage()
#初始化LCD spi = machine.SPI(1, baudrate=8000000, polarity=0, phase=0) d = st7735.ST7735(spi, rst=13, ce=9, dc=12) d.reset() d.begin() #白色背景 d._bground = 0xffff d.fill_screen(d._bground) #画点,(0,0,0)表示黑色 d.pixel(5, 5, d.rgb_to_565(0, 0, 0)) #画线,(0,0,0)表示黑色 lcd_gfx.drawLine(5, 10, 80, 10, d, d.rgb_to_565(0, 0, 0)) #画矩形,(0,0,0)表示黑色 lcd_gfx.drawRect(5, 20, 80, 40, d, d.rgb_to_565(0, 0, 0)) #画圆,(0,0,0)表示黑色 lcd_gfx.drawCircle(40, 90, 20, d, d.rgb_to_565(0, 0, 0)) #写字符 d.p_string(10, 130, 'Hello 01Studio!', d.rgb_to_565(255, 0, 0)) time.sleep_ms(2000) #延时2秒 #显示图片 BMP('flower128x160.bmp', d, 0, 0, 1)
def test(): lever = BMP.test() if not lever: print("Test is passed") else: print("Test fails")
def __init__(self, file_path, file_name_no_ext, build_folder_path, info): bmp = BMP(file_path) self.__file_path = file_path self.__file_name_no_ext = file_name_no_ext self.__build_folder_path = build_folder_path self.__colors_count = bmp.colors_count self.__bpp_8 = self.__colors_count > 16 try: height = int(info['height']) if bmp.height % height: raise ValueError('File height is not divisible by item height: ' + str(bmp.height) + ' - ' + str(height)) except KeyError: height = bmp.height self.__graphics = int(bmp.height / height) width = bmp.width if width == 8: if height == 8: self.__shape = 'SQUARE' self.__size = 'SMALL' elif height == 16: self.__shape = 'TALL' self.__size = 'SMALL' elif height == 32: self.__shape = 'TALL' self.__size = 'NORMAL' elif height == 64: raise ValueError('Invalid sprite size: (' + str(width) + 'x' + str(height) + ')' + SpriteItem.valid_sizes_message()) else: raise ValueError('Invalid sprite height: ' + str(height) + SpriteItem.valid_sizes_message()) elif width == 16: if height == 8: self.__shape = 'WIDE' self.__size = 'SMALL' elif height == 16: self.__shape = 'SQUARE' self.__size = 'NORMAL' elif height == 32: self.__shape = 'TALL' self.__size = 'BIG' elif height == 64: raise ValueError('Invalid sprite size: (: ' + str(width) + 'x' + str(height) + ')' + SpriteItem.valid_sizes_message()) else: raise ValueError('Invalid sprite height: ' + str(height) + SpriteItem.valid_sizes_message()) elif width == 32: if height == 8: self.__shape = 'WIDE' self.__size = 'NORMAL' elif height == 16: self.__shape = 'WIDE' self.__size = 'BIG' elif height == 32: self.__shape = 'SQUARE' self.__size = 'BIG' elif height == 64: self.__shape = 'TALL' self.__size = 'HUGE' else: raise ValueError('Invalid sprite height: ' + str(height) + SpriteItem.valid_sizes_message()) elif width == 64: if height == 8: raise ValueError('Invalid sprite size: (' + str(width) + 'x' + str(height) + ')' + SpriteItem.valid_sizes_message()) elif height == 16: raise ValueError('Invalid sprite size: (' + str(width) + 'x' + str(height) + ')' + SpriteItem.valid_sizes_message()) elif height == 32: self.__shape = 'WIDE' self.__size = 'HUGE' elif height == 64: self.__shape = 'SQUARE' self.__size = 'HUGE' else: raise ValueError('Invalid sprite height: ' + str(height) + SpriteItem.valid_sizes_message()) else: raise ValueError('Invalid sprite width: ' + str(width) + SpriteItem.valid_sizes_message())
### ### "Interlace" a picture - divide every second row's brightness by two, creating a striped appearance ### import sys from bmp import BMP from cStringIO import StringIO if len(sys.argv) == 1: filename = "test.bmp" else: filename = sys.argv[1] b = BMP(filename) print 'Image is {0}x{1} @ {2} bpp'.format(b.width, b.height, b.bpp) print 'Working...', sys.stdout.flush() mod_bitmap = "" # Let's pretend it's a file to make things easy f = StringIO(b.bitmap_data) for row_num in xrange(0, b.height): if row_num & 1 == 0: # Simply copy the data from even rows... mod_bitmap += f.read((b.width * 3) + b.padding_size) else: # ... and divide all pixel values by two on odd rows data = f.read((b.width * 3) + b.padding_size)
class InformationWidget(QtGui.QWidget): """docstring for InformationWidget""" def __init__(self, widget, image_path): super(InformationWidget, self).__init__() self.image_path = image_path self.grid = QtGui.QGridLayout(widget) self.grid.setColumnStretch(0, 1) self.grid.setColumnStretch(1, 1) def fill_info(self): self.bmp = BMP(self.image_path) bitmap_file_header = self.bmp.get_bitmap_file_header() bitmap_info = self.bmp.get_bitmap_info() all_list_info =\ bitmap_file_header.get_list_info() + bitmap_info.get_list_info() tableInfo = TableInfo(all_list_info) self.grid.addWidget(tableInfo.get_table_info(), 0, 0, 2, 1) def fill_picture_auto(self): pix_map = QtGui.QPixmap(self.image_path) pix_map = pix_map.scaled(600, 600, 1) pic = QtGui.QLabel() pic.setPixmap(pix_map) self.grid.addWidget(pic, 0, 1) def fill_picture_per_pixel(self): bitmap_info = self.bmp.get_bitmap_info() pixel_data = self.bmp.get_pixel_data() width = bitmap_info.get_width().get_unpack_value() height = bitmap_info.get_height().get_unpack_value() pixel_colors_generator = pixel_data.get_pixel_color() addition = pixel_data.get_addition() image = QtGui.QImage(width, abs(height), QtGui.QImage.Format_RGB32) if height < 0: try: for i in range(abs(height)): for j in range(width): color = next(pixel_colors_generator) image.setPixel( j, i, QtGui.qRgb(color[2], color[1], color[0])) for a in range(addition): next(pixel_colors_generator) except StopIteration: for i in range(abs(height)): for j in range(width): color = next(pixel_colors_generator) image.setPixel( j, i, QtGui.qRgb(color[2], color[1], color[0])) try: for i in range(height-1, -1, -1): for j in range(width): color = next(pixel_colors_generator) image.setPixel( j, i, QtGui.qRgb(color[2], color[1], color[0])) for a in range(addition): next(pixel_colors_generator) except StopIteration: pixel_colors_generator = pixel_data.get_pixel_color() for i in range(height-1, -1, -1): for j in range(width): color = next(pixel_colors_generator) image.setPixel( j, i, QtGui.qRgb(color[2], color[1], color[0])) pix_map = QtGui.QPixmap() pix_map.convertFromImage(image) pix_map = pix_map.scaled(600, 600, 1) pic = QtGui.QLabel() pic.setPixmap(pix_map) self.grid.addWidget(pic, 0, 1)