Esempio n. 1
0
    def __getitem__(self, item):
        if not self._loaded_textures[item]:
            # first, check if a texture with the same name already exist in the
            # cache
            # pylint: disable-msg=redefined-builtin
            chr = type(self._filename)
            uid = chr(u'%s|%d|%d') % (self._filename, self._mipmap, item)
            texture = Cache.get('kv.texture', uid)

            # if not create it and append to the cache
            if texture is None:
                zfilename = self._index_list[item]
                # read file and store it in mem with fileIO struct around it
                tmpfile = BytesIO(self._zip_file.read(zfilename))
                ext = zfilename.split('.')[-1].lower()
                image = None
                for loader in ImageLoader.loaders:
                    if (ext not in loader.extensions()
                            or not loader.can_load_memory()):
                        continue
                    Logger.debug('Image%s: Load <%s> from <%s>',
                                 loader.__name__[11:], zfilename,
                                 self._filename)
                    try:
                        image = loader(zfilename,
                                       ext=ext,
                                       rawdata=tmpfile,
                                       inline=True)
                    except:  # pylint: disable-msg=bare-except   # noqa
                        # Loader failed, continue trying.
                        continue
                    break
                if image is None:
                    raise AssertionError("Could not load image {} (index {}) "
                                         "from zip {}".format(
                                             zfilename, item, self._filename))

                self.width = image.width
                self.height = image.height

                imagedata = image._data[
                    0]  # pylint: disable-msg=protected-access

                source = '{}{}|'.format(
                    'zip|' if self._filename.endswith('.zip') else '',
                    self._no_cache)
                imagedata.source = chr(source) + uid
                texture = Texture.create_from_data(imagedata,
                                                   mipmap=self._mipmap)
                if not self._no_cache:
                    Cache.append('kv.texture', uid, texture)
                if imagedata.flip_vertical:
                    texture.flip_vertical()

            self._loaded_textures[item] = texture

        return self._loaded_textures[item]
Esempio n. 2
0
 def get_texture(self, data):
     bt = data['image']
     full: PillowImage = PillowImage.open(io.BytesIO(bt))
     exif_data = full._getexif()
     angle = 0
     vFlip = True
     hFlip = False
     # is there a rotation?
     rotation = 1
     if exif_data is not None and 274 in exif_data:
         rotation = exif_data[274]
     if rotation == 1:
         full = full.transpose(PillowImage.FLIP_LEFT_RIGHT)
     if rotation == 2:
         full = full.transpose(PillowImage.FLIP_LEFT_RIGHT)
     elif rotation == 3:
         full = full.transpose(PillowImage.ROTATE_180)
     elif rotation == 4:
         pass
     elif rotation == 5:
         full = full.transpose(PillowImage.FLIP_LEFT_RIGHT).transpose(
             PillowImage.ROTATE_270)  #swap 90 and 270
     elif rotation == 6:
         full = full.transpose(PillowImage.FLIP_LEFT_RIGHT).transpose(
             PillowImage.ROTATE_90)  #here too
     elif rotation == 7:
         full = full.transpose(PillowImage.ROTATE_90)
     elif rotation == 8:
         full = full.transpose(PillowImage.FLIP_LEFT_RIGHT).transpose(
             PillowImage.ROTATE_270)
     coreImage = CoreImageData(full.size[0], full.size[1],
                               full.mode.lower(), full.tobytes())
     texture = Texture.create_from_data(coreImage)
     data['angle'] = angle
     data['vflip'] = vFlip
     data['hflip'] = not hFlip
     return texture