Mercurial > libervia-backend
comparison sat/core/sat_main.py @ 3259:f300d78f08f3
core: image convertion + SVG support:
/!\ new optional dependency: CairoSVG (with installed `[SVG]` extra)
- new `convert` method in `tools.image` to save an image in an other format, with support
for SVG (when CairoSVG is available)
- new `imageConvert` method is available for frontends
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 19 Apr 2020 16:53:44 +0200 |
parents | 6cf4bd6972c2 |
children | 4240b44842bb |
comparison
equal
deleted
inserted
replaced
3258:7aa01e262e05 | 3259:f300d78f08f3 |
---|---|
172 self.bridge.register_method("devicesInfosGet", self._getDevicesInfos) | 172 self.bridge.register_method("devicesInfosGet", self._getDevicesInfos) |
173 self.bridge.register_method("namespacesGet", self.getNamespaces) | 173 self.bridge.register_method("namespacesGet", self.getNamespaces) |
174 self.bridge.register_method("imageCheck", self._imageCheck) | 174 self.bridge.register_method("imageCheck", self._imageCheck) |
175 self.bridge.register_method("imageResize", self._imageResize) | 175 self.bridge.register_method("imageResize", self._imageResize) |
176 self.bridge.register_method("imageGeneratePreview", self._imageGeneratePreview) | 176 self.bridge.register_method("imageGeneratePreview", self._imageGeneratePreview) |
177 self.bridge.register_method("imageConvert", self._imageConvert) | |
177 | 178 |
178 self.memory.initialized.addCallback(lambda __: defer.ensureDeferred(self._postMemoryInit())) | 179 self.memory.initialized.addCallback(lambda __: defer.ensureDeferred(self._postMemoryInit())) |
179 | 180 |
180 @property | 181 @property |
181 def version(self): | 182 def version(self): |
794 dest=cache_f | 795 dest=cache_f |
795 ) | 796 ) |
796 | 797 |
797 return preview_path | 798 return preview_path |
798 | 799 |
800 def _imageConvert(self, source, dest, extra, profile_key): | |
801 client = self.getClient(profile_key) if profile_key else None | |
802 source = Path(source) | |
803 dest = None if not dest else Path(dest) | |
804 extra = data_format.deserialise(extra) | |
805 d = defer.ensureDeferred(self.imageConvert(client, source, dest, extra)) | |
806 d.addCallback(lambda dest_path: str(dest_path)) | |
807 return d | |
808 | |
809 async def imageConvert(self, client, source, dest=None, extra=None): | |
810 """Helper method to convert an image from one format to an other | |
811 | |
812 @param client(SatClient, None): client to use for caching | |
813 this parameter is only used if dest is None | |
814 if client is None, common cache will be used insted of profile cache | |
815 @param source(Path): path to the image to convert | |
816 @param dest(None, Path, file): where to save the converted file | |
817 - None: use a cache file (uid generated from hash of source) | |
818 file will be converted to PNG | |
819 - Path: path to the file to create/overwrite | |
820 - file: a file object which must be opened for writing in binary mode | |
821 @param extra(dict, None): conversion options | |
822 see [image.convert] for details | |
823 @return (Path): path to the converted image | |
824 @raise ValueError: an issue happened with source of dest | |
825 """ | |
826 if not source.is_file: | |
827 raise ValueError(f"Source file {source} doesn't exist!") | |
828 if dest is None: | |
829 # we use hash as id, to re-use potentially existing conversion | |
830 path_hash = hashlib.sha256(str(source).encode()).hexdigest() | |
831 uid = f"{source.stem}_{path_hash}_convert_png" | |
832 filename = f"{uid}.png" | |
833 if client is None: | |
834 cache = self.common_cache | |
835 else: | |
836 cache = client.cache | |
837 metadata = cache.getMetadata(uid=uid) | |
838 if metadata is not None: | |
839 # there is already a conversion for this image in cache | |
840 return metadata['path'] | |
841 else: | |
842 with cache.cacheData( | |
843 source='HOST_IMAGE_CONVERT', | |
844 uid=uid, | |
845 filename=filename) as cache_f: | |
846 | |
847 converted_path = await image.convert( | |
848 source, | |
849 dest=cache_f, | |
850 extra=extra | |
851 ) | |
852 return converted_path | |
853 else: | |
854 return await image.convert(source, dest, extra) | |
855 | |
856 | |
799 # local dirs | 857 # local dirs |
800 | 858 |
801 def getLocalPath(self, client, dir_name, *extra_path, **kwargs): | 859 def getLocalPath(self, client, dir_name, *extra_path, **kwargs): |
802 """retrieve path for local data | 860 """retrieve path for local data |
803 | 861 |