def error_image(self): '''Image used for error (readonly)''' if not self._error_image: error_png_fn = join( 'atlas://data/images/defaulttheme/image-missing') self._error_image = ImageLoader.load(filename=error_png_fn) return self._error_image
def error_image(self): '''Image used for error (readonly)''' if not self._error_image: error_png_fn = join(kivy_base_dir, 'tools/theming/defaulttheme', 'image-missing.png') self._error_image = ImageLoader.load(filename=error_png_fn) return self._error_image
def load(self, atlas_page, image_name): image = ImageLoader.load(join(self.images_path, image_name)) texture = image.texture texture.mag_filter = self.texture_filter_type[atlas_page.mag_filter] texture.min_filter = self.texture_filter_type[atlas_page.min_filter] atlas_page.renderer_object = texture atlas_page.width = texture.width atlas_page.height = texture.height
def _load_local(self, filename): '''(internal) Loading a local file''' # With recent changes to CoreImage, we must keep data otherwise, # we might be unable to recreate the texture afterwise. try: im = ImageLoader.load(filename, keep_data=True) except Exception,ex: Logger.warning('Loader: Unable to load image <%s>' % filename) im = self.error_image
def load_thumbnail(self, filename): """Load from thumbnail screenDatabase, or generate a new thumbnail of the given image filename. Argument: filename: Image filename. Returns: A Kivy image""" root_widget = True if root_widget or self.loadanyway: app = App.get_running_app() full_filename = filename photo = self.photo file_found = isfile2(full_filename) # if file_found: # modified_date = int(os.path.getmtime(full_filename)) # if modified_date > photo.modify_date: #if not self.temporary: # app.Photo.update(photo) # app.update_photoinfo(folders=[photo[1]]) #app.Photo.thumbnail_update(photo[Photo.FULLPATH], photo[Photo.DATABASEFOLDER], modified_date, photo[Photo.ORIENTATION], temporary=self.temporary) thumbnail_image = photo.thumbnail if thumbnail_image: imagedata = bytes(thumbnail_image) data = BytesIO() data.write(imagedata) data.seek(0) image = CoreImage(data, ext='jpg') else: # if file_found: # updated = app.Photo.thumbnail_update(photo[Photo.FULLPATH], photo[Photo.DATABASEFOLDER], modified_date, photo[Photo.ORIENTATION], temporary=self.temporary) # if updated: # thumbnail_image = app.Photo.thumbnail(photo[Photo.FULLPATH], temporary=self.temporary) # data = BytesIO(thumbnail_image[2]) # image = CoreImage(data, ext='jpg') # else: # image = ImageLoader.load(full_filename) # else: image = ImageLoader.load('data/null.jpg') return image else: return ImageLoader.load('data/null.jpg')
def get_thumbnail(source, orientation, **kwargs): orig_dir = os.path.dirname(source) orig_name, orig_ext = os.path.splitext(os.path.basename(source).lower()) thumb_dir = os.path.join(orig_dir, 'thumb') thumb_name = 't_' + orig_name + '.jpg' thumb_path = os.path.join(thumb_dir, thumb_name) if os.path.exists(thumb_path): return ImageLoader.load(thumb_path, keep_data=True, **kwargs) else: try: os.makedirs(thumb_dir, exist_ok=True) if orig_ext in flickrUploader.image_ext: save_image_thumbnail(source, thumb_path, orientation) elif orig_ext in flickrUploader.video_ext: save_video_thumbnail(source, thumb_path) return ImageLoader.load(thumb_path, keep_data=True, **kwargs) except Exception as e: print("no thumbnail for file: %s" % source) return None
def _set_filename(self, value): #Logger.info('CoreImageOpenCV: value %s' % (value)) if value is None or value == self._filename: return self._filename = value # construct uid as a key for Cache if (self.res_width > 0) or (self.res_height > 0): uid = '%s|%d|%d|%s|%s' % (self.filename, self.res_width, self.res_height, self._mipmap, 0) else: uid = '%s|%s|%s' % (self.filename, self._mipmap, 0) # in case of Image have been asked with keep_data # check the kv.image cache instead of texture. image = Cache.get('kv.image', uid) if image: # we found an image, yeah ! but reset the texture now. self.image = image # if image.__class__ is core image then it's a texture # from atlas or other sources and has no data so skip if (image.__class__ != self.__class__ and not image.keep_data and self._keep_data): self.remove_from_cache() self._filename = '' self._set_filename(value) else: self._texture = None self._img_iterate() return else: # if we already got a texture, it will be automatically reloaded. _texture = Cache.get('kv.texture', uid) if _texture: self._texture = _texture return # if image not already in cache then load tmpfilename = self._filename #Logger.info('CoreImageOpenCV: set_filename %s' % (tmpfilename)) #Logger.info('CoreImageOpenCV: %d %d' % (self.res_width, self.res_height)) image = ImageLoader.load( self._filename, keep_data=self._keep_data, mipmap=self._mipmap, nocache=self._nocache, res_width=self.res_width, res_height=self.res_height, load_exif=self.load_exif) self._filename = tmpfilename # put the image into the cache if needed if isinstance(image, Texture): self._texture = image self._size = image.size else: self.image = image if not self._nocache: Cache.append('kv.image', uid, self.image)
def load_data(self): global movies self.video_data = {} for folder in glob('content/movies/*'): movie_id = folder.split('/')[-1] #print movie_id movie_data = json.loads(open(folder + '/data.json').read()) movies[movie_id] = Movie(movie_data) movies[movie_id].set_trailer(folder + '/trailer.avi') #self.video_data[movie_id] = VideoBuffer(filename=movies[movie_id].trailer) #preload data, so it wont hang on loading self.ad_images = {} for fname in glob('content/offers/*.png'): self.ad_images[fname] = ImageLoader.load(fname) self.offers = self.ad_images.keys() self.suggestions = movies.keys()[:3]
def load_data(self): global movies self.video_data = {} for folder in glob('content/movies/*'): movie_id = folder.split('/')[-1] #print movie_id movie_data = json.loads(open(folder+'/data.json').read()) movies[movie_id] = Movie(movie_data) movies[movie_id].set_trailer(folder+'/trailer.avi'); #self.video_data[movie_id] = VideoBuffer(filename=movies[movie_id].trailer) #preload data, so it wont hang on loading self.ad_images = {} for fname in glob('content/offers/*.png'): self.ad_images[fname] = ImageLoader.load(fname) self.offers = self.ad_images.keys() self.suggestions = movies.keys()[:3]
def _set_error_image(self, image): if isinstance(image, basestring): self._error_image = ImageLoader.load(filename=image) else: self._error_image = image
def _get_loading_image(self): if not self._loading_image: loading_png_fn = join(kivy_data_dir, 'images', 'image-loading.gif') self._loading_image = ImageLoader.load(filename=loading_png_fn) return self._loading_image
def _set_error_image(self, image): if isinstance(image, string_types): self._error_image = ImageLoader.load(filename=image) else: self._error_image = image
def error_image(self): '''Image used for error (readonly)''' if not self._error_image: error_png_fn = join(kivy_data_dir, 'images', 'image-missing.png') self._error_image = ImageLoader.load(filename=error_png_fn) return self._error_image
# Twisted initialization from kivy.support import install_twisted_reactor try: if platform in ['win', 'windows']: import sys if 'twisted.internet.reactor' in sys.modules: del sys.modules['twisted.internet.reactor'] install_twisted_reactor() except Exception as e: print('TWISTED: error installing -- %s' % str(e)) from kivy.app import App from kivy.logger import Logger # set refresh icon ALoaderOpenCV.loading_image = ImageLoader.load('data/placeholder.png', allow_stretch=False) ImageLoader.loading_image = ImageLoader.load('data/placeholder.png', allow_stretch=False) # start login process in parallel to App initialization from authentication import authenticate def on_login(token): print ('on_start: logged in') from api.streams.posts import Manager as PostsManager PostsManager.logged_in = True from modules.core.android_utils import LogTestFairy LogTestFairy('Successful login ') authenticate(on_login=on_login) from kivy.factory import Factory Factory.register('MainScreenManager', module='widgets.mainscreenmanager')
return output def get_bits(flags, reverse=False, bits=8): '''return a list with $bits items, one for each enabled bit''' mybits = (1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048)[:bits] rev_num=1 if reverse: rev_num = -1 ret = array('B') ret_append = ret.append for bit in mybits[::rev_num]: ret_append(flags & bit != 0) return ret def pack_bits(bits): '''convert a bit (bool or int) tuple into a int''' packed = 0 level = 0 for bit in bits: if bit: packed += 2 ** level level += 1 return packed # register ImageLoader.register(ImageLoaderGIF)
info = json.loads(header) data = fd.read() if len(data) != info['datalen']: raise Exception('Truncated tex data') except: Logger.warning('Image: Image <%s> is corrupted' % filename) raise width, height = info['image_size'] tw, th = info['texture_size'] images = [data] im = ImageData(width, height, str(info['format']), images[0], source=filename) ''' if len(dds.images) > 1: images = dds.images images_size = dds.images_size for index in range(1, len(dds.images)): w, h = images_size[index] data = images[index] im.add_mipmap(index, w, h, data) ''' return [im] # register ImageLoader.register(ImageLoaderTex)
# Twisted initialization from kivy.support import install_twisted_reactor try: if platform in ['win', 'windows']: import sys if 'twisted.internet.reactor' in sys.modules: del sys.modules['twisted.internet.reactor'] install_twisted_reactor() except Exception as e: print('TWISTED: error installing -- %s' % str(e)) from kivy.app import App from kivy.logger import Logger # set refresh icon ALoaderOpenCV.loading_image = ImageLoader.load('data/placeholder.png', allow_stretch=False) ImageLoader.loading_image = ImageLoader.load('data/placeholder.png', allow_stretch=False) # start login process in parallel to App initialization from authentication import authenticate def on_login(token): print('on_start: logged in') from api.streams.posts import Manager as PostsManager PostsManager.logged_in = True from modules.core.android_utils import LogTestFairy LogTestFairy('Successful login ')
def error_image(self): """Image used for error (readonly)""" if not self._error_image: error_png_fn = join(kivy_data_dir, "error.png") self._error_image = ImageLoader.load(filename=error_png_fn) return self._error_image
def _load_local(self, filename): """(internal) Loading a local file""" return ImageLoader.load(filename)
def extensions(): return ('dds', ) def load(self, filename): try: dds = DDSFile(filename=filename) except: Logger.warning('Image: Unable to load image <%s>' % filename) raise self.filename = filename width, height = dds.size im = ImageData(width, height, dds.dxt, dds.images[0], source=filename, flip_vertical=False) if len(dds.images) > 1: images = dds.images images_size = dds.images_size for index in range(1, len(dds.images)): w, h = images_size[index] data = images[index] im.add_mipmap(index, w, h, data) return [im] # register ImageLoader.register(ImageLoaderDDS)
def loading_image(self): '''Image used for loading (readonly)''' if not self._loading_image: loading_png_fn = join(kivy_data_dir, 'images', 'image-loading.gif') self._loading_image = ImageLoader.load(filename=loading_png_fn) return self._loading_image
# transparency properly img_ol.paste(img_tmp, (0, 0), img_tmp) img_tmp = img_ol img_ol = img_tmp yield ImageData(img_tmp.size[0], img_tmp.size[1], img_tmp.mode.lower(), img_tmp.tostring()) im.seek(im.tell() + 1) except EOFError: pass def load(self, filename): try: im = PILImage.open(filename) except: Logger.warning('Image: Unable to load image <%s>' % filename) raise # update internals self.filename = filename # returns an array of type ImageData len 1 if not a sequence image return list(self._img_read(im)) @staticmethod def save(filename, width, height, fmt, pixels): image = PILImage.fromstring(fmt.upper(), (width, height), pixels) image.save(filename) return True # register ImageLoader.register(ImageLoaderPIL, default=True)
def _load_local(self, filename): '''(internal) Loading a local file''' return ImageLoader.load(filename)
def _set_loading_image(self, image): if isinstance(image, string_types): self._loading_image = ImageLoader.load(filename=image) else: self._loading_image = image
imc = im.convert(32) fmt = 'rgba' except: try: imc = im.convert_alpha() fmt = 'rgba' except: Logger.warning( 'Image: Unable to convert image %r to rgba (was %r)' % (filename, im.fmt)) raise im = imc # update internals if not self._inline: self.filename = filename data = pygame.image.tostring(im, fmt.upper()) return [ImageData(im.get_width(), im.get_height(), fmt, data, source=filename)] @staticmethod def save(filename, width, height, fmt, pixels, flipped): surface = pygame.image.fromstring( pixels, (width, height), fmt.upper(), flipped) pygame.image.save(surface, filename) return True # register ImageLoader.register(ImageLoaderPygame)
def _set_loading_image(self, image): if isinstance(image, basestring): self._loading_image = ImageLoader.load(filename=image) else: self._loading_image = image
# update internals self.filename = filename images = [] while True: frame, t = loader.next_frame() if frame is None: break images.append(frame) if not len(images): raise Exception('No image found in {}'.format(filename)) w, h = images[0].get_size() ifmt = images[0].get_pixel_format() if ifmt != 'rgba' and ifmt != 'rgb24': fmt = 'rgba' sws = SWScale(w, h, ifmt, ofmt=fmt) for i, image in enumerate(images): images[i] = sws.scale(image) else: fmt = ifmt if ifmt == 'rgba' else 'rgb' return [ ImageData(w, h, fmt, img.to_memoryview()[0], source_image=img) for img in images ] # register ImageLoader.register(ImageLoaderFFPy)
raise Exception('Truncated tex header') info = json.loads(header) data = fd.read() if len(data) != info['datalen']: raise Exception('Truncated tex data') except: Logger.warning('Image: Image <%s> is corrupted' % filename) raise width, height = info['image_size'] tw, th = info['texture_size'] images = [data] im = ImageData(width, height, str(info['format']), images[0], source=filename) ''' if len(dds.images) > 1: images = dds.images images_size = dds.images_size for index in xrange(1, len(dds.images)): w, h = images_size[index] data = images[index] im.add_mipmap(index, w, h, data) ''' return [im] # register ImageLoader.register(ImageLoaderTex)
def get_bits(flags, reverse=False, bits=8): '''return a list with $bits items, one for each enabled bit''' mybits = (1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048)[:bits] rev_num = 1 if reverse: rev_num = -1 ret = array('B') ret_append = ret.append for bit in mybits[::rev_num]: ret_append(flags & bit != 0) return ret def pack_bits(bits): '''convert a bit (bool or int) tuple into a int''' packed = 0 level = 0 for bit in bits: if bit: packed += 2**level level += 1 return packed # register ImageLoader.register(ImageLoaderGIF)
def _get_error_image(self): if not self._error_image: error_png_fn = join( 'atlas://data/images/defaulttheme/image-missing') self._error_image = ImageLoader.load(filename=error_png_fn) return self._error_image
raise # update internals self.filename = filename images = [] while True: frame, t = loader.next_frame() if frame is None: break images.append(frame) if not len(images): raise Exception('No image found in {}'.format(filename)) w, h = images[0].get_size() ifmt = images[0].get_pixel_format() if ifmt != 'rgba' and ifmt != 'rgb24': fmt = 'rgba' sws = SWScale(w, h, ifmt, ofmt=fmt) for i, image in enumerate(images): images[i] = sws.scale(image) else: fmt = ifmt if ifmt == 'rgba' else 'rgb' return [ImageData(w, h, fmt, img.to_memoryview()[0], source_image=img) for img in images] # register ImageLoader.register(ImageLoaderFFPy)
def _load_local(self, filename, kwargs): '''(internal) Loading a local file''' # With recent changes to CoreImage, we must keep data otherwise, # we might be unable to recreate the texture afterwise. return ImageLoader.load(filename, keep_data=True, **kwargs)
def _img_read(self, im): """Read images from an animated file. """ im.seek(0) # Read all images inside try: while True: img_tmp = im img_tmp = self._img_correct(img_tmp) yield ImageData(img_tmp.size[0], img_tmp.size[1], img_tmp.mode.lower(), img_tmp.tostring()) im.seek(im.tell() + 1) except EOFError: pass def load(self, filename): try: im = PILImage.open(filename) except: Logger.warning("Image: Unable to load image <%s>" % filename) raise # update internals self.filename = filename # returns an array of type ImageData len 1 if not a sequence image return list(self._img_read(im)) # register ImageLoader.register(ImageLoaderPIL)
from kivy.core.image import ImageLoaderBase, ImageData, ImageLoader class ImageLoaderDDS(ImageLoaderBase): @staticmethod def extensions(): return ('dds', ) def load(self, filename): try: dds = DDSFile(filename=filename) except: Logger.warning('Image: Unable to load image <%s>' % filename) raise self.filename = filename width, height = dds.size im = ImageData(width, height, dds.dxt, dds.images[0], source=filename) if len(dds.images) > 1: images = dds.images images_size = dds.images_size for index in xrange(1, len(dds.images)): w, h = images_size[index] data = images[index] im.add_mipmap(index, w, h, data) return [im] # register ImageLoader.register(ImageLoaderDDS)
def can_load_memory(): return True def load(self, filename): if self._inline: data = filename.read() info = _img_sdl2.load_from_memory(data) else: info = _img_sdl2.load_from_filename(filename) if not info: Logger.warning('Image: Unable to load image <%s>' % filename) raise Exception('SDL2: Unable to load image') w, h, fmt, pixels, rowlength = info # update internals if not self._inline: self.filename = filename return [ImageData( w, h, fmt, pixels, source=filename, rowlength=rowlength)] @staticmethod def save(filename, width, height, fmt, pixels, flipped): _img_sdl2.save(filename, width, height, fmt, pixels, flipped) return True # register ImageLoader.register(ImageLoaderSDL2)
# transparency properly img_ol.paste(img_tmp, (0, 0), img_tmp) img_tmp = img_ol img_ol = img_tmp yield ImageData(img_tmp.size[0], img_tmp.size[1], img_tmp.mode.lower(), img_tmp.tostring()) im.seek(im.tell() + 1) except EOFError: pass def load(self, filename): try: im = PILImage.open(filename) except: Logger.warning('Image: Unable to load image <%s>' % filename) raise # update internals self.filename = filename # returns an array of type ImageData len 1 if not a sequence image return list(self._img_read(im)) @staticmethod def save(filename, width, height, fmt, pixels): image = PILImage.fromstring(fmt.upper(), (width, height), pixels) image.save(filename) return True # register ImageLoader.register(ImageLoaderPIL)
def load_resources(): global images_textures, sound_fx images_textures['img_brick_red_1_0'] = ImageLoader.load('res/brick_red_1.png').texture images_textures['img_brick_red_2_0'] = ImageLoader.load('res/brick_red_2.png').texture images_textures['img_brick_red_3_0'] = ImageLoader.load('res/brick_red_3.png').texture images_textures['img_brick_red_4_0'] = ImageLoader.load('res/brick_red_4.png').texture images_textures['img_brick_blu_1_0'] = ImageLoader.load('res/brick_blu_1.png').texture images_textures['img_brick_blu_2_0'] = ImageLoader.load('res/brick_blu_2.png').texture images_textures['img_brick_blu_3_0'] = ImageLoader.load('res/brick_blu_3.png').texture images_textures['img_brick_blu_4_0'] = ImageLoader.load('res/brick_blu_4.png').texture images_textures['img_brick_green_1_0'] = ImageLoader.load('res/brick_green_1.png').texture images_textures['img_brick_green_2_0'] = ImageLoader.load('res/brick_green_2.png').texture images_textures['img_brick_green_3_0'] = ImageLoader.load('res/brick_green_3.png').texture images_textures['img_brick_green_4_0'] = ImageLoader.load('res/brick_green_4.png').texture images_textures['img_brick_pink_1_0'] = ImageLoader.load('res/brick_pink_1.png').texture images_textures['img_brick_pink_2_0'] = ImageLoader.load('res/brick_pink_2.png').texture images_textures['img_brick_pink_3_0'] = ImageLoader.load('res/brick_pink_3.png').texture images_textures['img_brick_pink_4_0'] = ImageLoader.load('res/brick_pink_4.png').texture images_textures['img_brick_silv_1_0'] = ImageLoader.load('res/brick_silv_1.png').texture images_textures['img_brick_silv_2_0'] = ImageLoader.load('res/brick_silv_2.png').texture images_textures['img_brick_silv_3_0'] = ImageLoader.load('res/brick_silv_3.png').texture images_textures['img_brick_silv_4_0'] = ImageLoader.load('res/brick_silv_4.png').texture images_textures['img_brick_gold_1_0'] = ImageLoader.load('res/brick_gold_1.png').texture images_textures['img_brick_gold_2_0'] = ImageLoader.load('res/brick_gold_2.png').texture images_textures['img_brick_gold_3_0'] = ImageLoader.load('res/brick_gold_3.png').texture images_textures['img_brick_gold_4_0'] = ImageLoader.load('res/brick_gold_4.png').texture images_textures['img_brick_concrete_1_0'] = ImageLoader.load('res/brick_concrete_1.png').texture images_textures['pad_plain'] = ImageLoader.load('res/pad_plain.png').texture images_textures['line_blu'] = ImageLoader.load('res/line_blu.png').texture images_textures['touch_area'] = ImageLoader.load('res/bkg_touch.png').texture images_textures['ball_plain'] = ImageLoader.load('res/ball_200x200.png').texture images_textures['pad_yellow_2_0'] = ImageLoader.load('res/pad_yel_2_0.png').texture images_textures['pad_yellow_2_1'] = ImageLoader.load('res/pad_yel_2_1.png').texture sound_fx['game_over'] = SoundLoader.load('res/audio/gameover.ogg') sound_fx['game_start'] = SoundLoader.load('res/audio/gamestart.ogg') sound_fx['brick_hit'] = SoundLoader.load('res/audio/brickhit.ogg') sound_fx['pad_hit'] = SoundLoader.load('res/audio/padhit.ogg')
im = imc # update internals if not self._inline: self.filename = filename data = pygame.image.tostring(im, fmt.upper()) return [ ImageData(im.get_width(), im.get_height(), fmt, data, source=filename) ] @staticmethod def save(filename, width, height, pixelfmt, pixels, flipped, imagefmt=None): surface = pygame.image.fromstring(pixels, (width, height), pixelfmt.upper(), flipped) pygame.image.save(surface, filename) return True # register ImageLoader.register(ImageLoaderPygame)
def loading_image(self): """Image used for loading (readonly)""" if not self._loading_image: loading_png_fn = join(kivy_data_dir, "images", "image-loading.gif") self._loading_image = ImageLoader.load(filename=loading_png_fn) return self._loading_image
def __init__(self, **kwargs): self.texture = ImageLoader.load('snake.png').texture super(SnakeLogo, self).__init__(**kwargs)
def can_load_memory(): return True def load(self, filename): if self._inline: data = filename.read() info = _img_sdl2.load_from_memory(data) else: info = _img_sdl2.load_from_filename(filename) if not info: Logger.warning('Image: Unable to load image <%s>' % filename) raise Exception('SDL2: Unable to load image') w, h, fmt, pixels, rowlength = info # update internals if not self._inline: self.filename = filename return [ ImageData(w, h, fmt, pixels, source=filename, rowlength=rowlength) ] @staticmethod def save(filename, width, height, fmt, pixels, flipped): _img_sdl2.save(filename, width, height, fmt, pixels, flipped) return True # register ImageLoader.register(ImageLoaderSDL2)