Module refinery.units.formats.imgto
Expand source code Browse git
from __future__ import annotations
import io
from refinery.lib.structures import MemoryFile
from refinery.lib.types import Param
from refinery.units import Arg, Unit
class imgto(Unit):
"""
Convert an image to a given format.
"""
def __init__(
self,
format: Param[str, Arg.String(
help='An image file format like png, jpg, or bmp. The default is {default}.')] = 'png'
):
super().__init__(format=format)
@Unit.Requires('Pillow', ['formats'])
def _image():
from PIL import Image
return Image
def process(self, data):
try:
image = self._image.open(MemoryFile(data, output=bytes))
except ImportError:
raise
except Exception:
raise ValueError('input could not be parsed as an image')
with io.BytesIO() as out:
image.save(out, self.args.format)
return out.getvalue()
Classes
class imgto (format='png')
-
Convert an image to a given format.
Expand source code Browse git
class imgto(Unit): """ Convert an image to a given format. """ def __init__( self, format: Param[str, Arg.String( help='An image file format like png, jpg, or bmp. The default is {default}.')] = 'png' ): super().__init__(format=format) @Unit.Requires('Pillow', ['formats']) def _image(): from PIL import Image return Image def process(self, data): try: image = self._image.open(MemoryFile(data, output=bytes)) except ImportError: raise except Exception: raise ValueError('input could not be parsed as an image') with io.BytesIO() as out: image.save(out, self.args.format) return out.getvalue()
Ancestors
Subclasses
Class variables
var required_dependencies
var console
var reverse
var optional_dependencies
Inherited members