def populate_from_image_file(self, path, f, composite_op = None): if not Image: raise RuntimeError, 'Cannot open non-native image types without Python Imaging Library: %s' % (path) src = Image.open(f) (w, h) = src.size # print ("conversion of image, sized " + str(w) + "," + str(h)) if len(self.frames) > 0 and (w != self.width or h != self.height): raise ValueError, "Image sizes must be uniform! Anim is %dx%d, image is %dx%d" % (w, h, self.width, self.height) (self.width, self.height) = (w, h) # I'm punting on animated gifs, because they're too slow. If you coalesce them # via image magic then they are fast again. if path.endswith('.gif'): import animgif self.frames += animgif.gif_frames(src, composite_op = composite_op) else: (w,h) = src.size if src.mode == "P": src.convert("RGB") src.mode = "RGB" surf = sdl2_DisplayManager.inst().make_texture_from_imagebits(bits=src.tobytes(), width=w, height=h, mode=src.mode, composite_op = composite_op) frame = Frame(w,h,surf) self.frames.append(frame)
def old_populate_from_image_file(self, path, f): if not Image: raise RuntimeError, 'Cannot open non-native image types without Python Imaging Library: %s' % (path) src = Image.open(f) (w, h) = src.size if len(self.frames) > 0 and (w != self.width or h != self.height): raise ValueError, "Image sizes must be uniform! Anim is %dx%d, image is %dx%d" % (w, h, self.width, self.height) (self.width, self.height) = (w, h) if path.endswith('.gif'): from . import animgif self.frames += animgif.gif_frames(src) else: alpha = None try: alpha = Image.fromstring('L', src.size, src.tostring('raw', 'A')) except: pass # No alpha channel available? reduced = src.convert("L") frame = Frame(w, h) # Construct a lookup table from 0-255 to 0-15: eight_to_four_map = [0] * 256 for l in range(256): eight_to_four_map[l] = int(round((l/255.0) * 15.0)) for x in range(w): for y in range(h): color = eight_to_four_map[reduced.getpixel((x,y))] if alpha: color += eight_to_four_map[alpha.getpixel((x,y))] << 4 frame.set_dot(x=x, y=y, value=color) self.frames.append(frame)
def populate_from_image_file(self, path, f, composite_op = None): if not Image: raise RuntimeError, 'Cannot open non-native image types without Python Imaging Library: %s' % (path) src = Image.open(f) (w, h) = src.size # print ("conversion of image, sized " + str(w) + "," + str(h)) if len(self.frames) > 0 and (w != self.width or h != self.height): raise ValueError, "Image sizes must be uniform! Anim is %dx%d, image is %dx%d" % (w, h, self.width, self.height) (self.width, self.height) = (w, h) # I'm punting on animated gifs, because they're too slow. If you coalesce them # via image magic then they are fast again. if path.endswith('.gif'): import animgif self.frames += animgif.gif_frames(src, composite_op = composite_op) else: #frame = Animation.convertImage(src) (w,h) = src.size if src.mode == "P": src.convert("RGB") src.mode = "RGB" # surf = HD_load_file(path) surf = sdl2_DisplayManager.inst().make_texture_from_imagebits(bits=src.tostring(), width=w, height=h, mode=src.mode, composite_op = composite_op) #print("ConvertImage made texture for this frame -- contents: " + str(surf)) frame = Frame(w,h,surf) self.frames.append(frame)