def _windows_image_loader(fname): try: import imload width, height = imload.QueryImage(fname) im = ImageRGBA(width, height) addr, pitch = im.address_info() imload.GetImage(fname, addr, width, height) return im except ImportError: return None return None
def test_img_rect(self): img = ImageRGBA(40, 40) x, y = 5, 5 width, height = 10, 10 r, g, b = 125, 175, 210 draw_rect(img, x, y, width, height, r, g, b) for j in range(y, y + height): for i in range(x, x + width): r1, g1, b1, a1 = img.get_pixel(i, j) self.assertEqual(r, r1) self.assertEqual(g, g1) self.assertEqual(b, b1)
def _load_image_frimgldr(fname): import freeimgldr width, height, bpp = freeimgldr.QueryImage(fname) if bpp > 32: im = ImagePRGBA(width, height) else: im = ImageRGBA(width, height) addr, pitch = im.address_info() freeimgldr.GetImage(fname, addr, width, height, bpp) return im
def test_get_rgba(self): code = """ v1 = get_rgba(image1, 10, 10) """ image = ImageRGBA(200, 200) image.set_pixel(10, 10, 25, 77, 142, 185) p1 = StructArg('image1', image) p2 = Vec4Arg('v1', Vector4(0.0, 0.0, 0.0, 0.0)) shader = Shader(code=code, args=[p1, p2]) shader.compile() shader.prepare([Runtime()]) shader.execute() val = shader.get_value('v1') self.assertAlmostEqual(val.x, 25 * 0.0039, places=6) self.assertAlmostEqual(val.y, 77 * 0.0039) self.assertAlmostEqual(val.z, 142 * 0.0039) self.assertAlmostEqual(val.w, 185 * 0.0039)
def load_ppm(fname): f = open(fname, 'rb') identifier = None rx = ry = None max_value = None while True: #reading header information line = _read_bin_to_asci_line(f) line.strip() if line == '' or line == '#': continue if identifier is None: if line != 'P6': raise ValueError("Just P6 format is supported.") identifier = line continue if rx is None: words = line.split() rx = int(words[0]) ry = int(words[1]) if len(words) > 2: max_value = int(words[3]) break #whole header is read continue if max_value is None: max_value = int(line) break # whole header is read #read raw bytes(pixels) img = ImageRGBA(rx, ry) for y in range(ry): for x in range(rx): c = f.read(3) yy = y yy = ry - y - 1 # filp image?? img.set_pixel(x, yy, c[0], c[1], c[2]) f.close() return img
def load_tga(fname): if not os.path.isfile(fname): return None #file doesn't exists f = open(fname, 'rb') # READ HEADER of IMAGE id_length = f.read(1)[0] color_map_type = f.read(1)[ 0] # 0 - no color map in image, 1 - color map is included # 0 - no image data, 1 - Uncompressed(color-map) image, 2 - Uncompressed(true color) image # 3 - Uncompressed Black and White image, 9 - RLE color mapped image, 10 - RLE true color, 11 - RLE black and white image_type = f.read( 1 )[0] # 0 - no image data, 1 - Uncompressed(color-map) image, 2 - Uncompressed(true color) image color_map_origin = unpack('h', f.read(2))[0] color_map_length = unpack('h', f.read(2))[0] color_map_entry_size = f.read(1)[0] x_origin = unpack('h', f.read(2))[0] y_origin = unpack('h', f.read(2))[0] width = unpack('h', f.read(2))[0] height = unpack('h', f.read(2))[0] pixel_depth = f.read(1)[0] image_descriptor = f.read(1)[0] # CHECK IF LOADER SUPPORT IMAGE if image_type != 2 and image_type != 10: return None # only true color is supported if pixel_depth != 16 and pixel_depth != 24 and pixel_depth != 32: return None if color_map_type != 0 and color_map_type != 1: return None # SKIP OVER UNNECESSARY BYTES skip = id_length + color_map_type * color_map_length f.seek(skip, SEEK_CUR) # TODO -- image descriptor --- ordering of pixel, bottom left, top left etc... #READ IMAGE BYTES image = ImageRGBA(width, height) if image_type == 2: ret = _read_uncompressed(f, width, height, pixel_depth // 8, image) elif image_type == 10: ret = _read_compressed(f, width, height, pixel_depth // 8, image) f.close() return ret
def create_image(objects, args): pix_format, width, height = args.split(',') if pix_format == 'RGBA': img = ImageRGBA(int(width), int(height)) elif pix_format == 'BGRA': img = ImageBGRA(int(width), int(height)) elif pix_format == 'PRGBA': img = ImagePRGBA(int(width), int(height)) else: raise ValueError("Unknown pixel format", pix_format) objects[str(id(img))] = img return str(id(img))
def save_image(self, objects, args): fname, id_img = args.split(',') image = objects[id_img] if isinstance(image, ImagePRGBA): width, height = image.size() new_img = ImageRGBA(width, height) blt_prgba_to_rgba(image, new_img) save_image(fname, new_img) elif isinstance(image, ImageRGBA): save_image(fname, image) elif isinstance(image, ImageBGRA): new_img = image.to_rgba() save_image(fname, new_img) return ""
def test_blt(self): source = ImageRGBA(300, 200) dest = ImageRGBA(300, 200) sa, spitch = source.address_info() da, dpitch = dest.address_info() draw_rect(source, 120, 130, 10, 8, 130, 120, 150, 180) blt_rect(sa, 120, 130, 10, 8, spitch, da, 180, 190, dpitch, fliped=False) self._check_values(dest, 180, 190, 10, 8, 130, 120, 150, 180) blt_rect(sa, 120, 130, 10, 8, spitch, da, 50, 60, dpitch, fliped=False) self._check_values(dest, 50, 60, 10, 8, 130, 120, 150, 180)
def test_blt_image(self): source = ImageRGBA(50, 20) dest = ImageRGBA(50, 20) draw_rect(source, 0, 0, 50, 20, 130, 120, 150, 180) blt_image(source, dest) self._check_values(dest, 0, 0, 50, 20, 130, 120, 150, 180)