-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathImageIO.py
37 lines (30 loc) · 1.09 KB
/
ImageIO.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import os
import wx
import Utils
import base64
srcPrefix = "data:image/png;base64,"
from io import BytesIO
def toBufFromImage( image ):
ss = BytesIO()
image.SaveFile( ss, wx.BITMAP_TYPE_PNG )
return srcPrefix + base64.b64encode(ss.getbuffer()).decode()
def toImageFromBuf( buf ):
return wx.Image( BytesIO(base64.b64decode(buf[len(srcPrefix):])), wx.BITMAP_TYPE_PNG )
def toBufFromBitmap( bitmap ):
return toBufFromImage( wx.Image(bitmap) )
def toBufFromFile( fname, type=wx.BITMAP_TYPE_ANY ):
return toBufFromImage( wx.Image(fname, type) )
def toBitmapFromBuf( buf ):
return wx.Bitmap( toImageFromBuf(buf) )
if __name__ == '__main__':
app = wx.App(False)
image = wx.Image( os.path.join(Utils.getImageFolder(), 'CrossMgr.png'), wx.BITMAP_TYPE_PNG )
buf1 = toBufFromImage( image )
buf2 = toBufFromImage( toImageFromBuf(buf1) )
buf3 = toBufFromFile( os.path.join(Utils.getImageFolder(), 'CrossMgr.png'), wx.BITMAP_TYPE_PNG )
assert buf1 == buf2
assert buf2 == buf3
print( len(buf1), len(buf2), len(buf3) )
print( Utils.ToJson(buf1) )
img = toImageFromBuf( buf1 )
bmp = toBitmapFromBuf( buf2 )