MCPcopy Create free account
hub / github.com/MearaY/StegaPy / image_to_byte_array

Method image_to_byte_array

StegaPy/util/image_util.py:111–128  ·  view source on GitHub ↗

将PIL图像转换为字节数组

(image, filename=None, format='PNG')

Source from the content-addressed store, hash-verified

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):

Callers 6

embed_dataMethod · 0.80
get_diffMethod · 0.80
embed_dataMethod · 0.80
get_diffMethod · 0.80
embed_dataMethod · 0.80
get_diffMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected