将PIL图像转换为字节数组
(image, filename=None, format='PNG')
| 109 | |
| 110 | @staticmethod |
| 111 | def image_to_byte_array(image, filename=None, format='PNG'): |
| 112 | """将PIL图像转换为字节数组""" |
| 113 | try: |
| 114 | # 根据文件名确定格式 |
| 115 | if filename: |
| 116 | ext = filename.rsplit('.', 1)[-1].lower() |
| 117 | if ext in ['jpg', 'jpeg']: |
| 118 | format = 'JPEG' |
| 119 | elif ext == 'png': |
| 120 | format = 'PNG' |
| 121 | elif ext == 'bmp': |
| 122 | format = 'BMP' |
| 123 | |
| 124 | output = io.BytesIO() |
| 125 | image.save(output, format=format) |
| 126 | return output.getvalue() |
| 127 | except Exception as e: |
| 128 | raise Exception(f"无法保存图像: {str(e)}") |
| 129 | |
| 130 | @staticmethod |
| 131 | def generate_random_image(num_pixels): |
no outgoing calls
no test coverage detected