65 def image_to_image(self, data: bytes, source_format: DataTypes) -> CONV_CONST.ConversionResult:
67 Convert image data from one format to another using Pillow.
69 data (bytes): The image data to convert.
70 source_format (DataTypes): The original image format.
72 ConversionResult: The converted image data in a contained dataclass.
74 converted_data: Optional[bytes] =
None
75 destination_format = MEDIA_TYPES.get_conversion_target(source_format)
76 if destination_format
is None:
77 return CONV_CONST.ConversionResult(
80 from_type=source_format,
81 to_type=source_format,
84 if source_format == destination_format:
85 return CONV_CONST.ConversionResult(
88 from_type=source_format,
89 to_type=source_format,
92 pillow_format: Optional[str] = PILLOW_FORMAT_ALIASES.get(
95 if pillow_format
is None:
96 self.
disp.log_warning(
97 f
"No Pillow alias for {destination_format}, skipping conversion"
99 return CONV_CONST.ConversionResult(
102 from_type=source_format,
103 to_type=destination_format,
107 with Image.open(BytesIO(data))
as img:
108 output_buffer = BytesIO()
111 format=pillow_format
or destination_format.name
113 converted_data = output_buffer.getvalue()
114 return CONV_CONST.ConversionResult(
117 from_type=source_format,
118 to_type=destination_format,
119 result=converted_data
121 except Exception
as e:
122 self.
disp.log_error(f
"Image conversion error: {e}")
123 return CONV_CONST.ConversionResult(
126 from_type=source_format,
127 to_type=destination_format,