-
Notifications
You must be signed in to change notification settings - Fork 13
/
base64_handler.py
81 lines (61 loc) · 2.21 KB
/
base64_handler.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
from pathlib import Path
import struct
import base64
try:
from .definitions import APP_ROOT
except ImportError as e:
print(e)
import traceback
print(traceback.format_exc())
from definitions import APP_ROOT
# http://coreygoldberg.blogspot.com/2013/01/python-verify-png-file-and-get-image.html
def get_image_info(data):
if is_png(data):
w, h = struct.unpack(b'>LL', data[16:24])
width = int(w)
height = int(h)
else:
raise Exception('not a png image')
return width, height
def is_png(data):
return data[:8] == b'\211PNG\r\n\032\n' and (data[12:16] == b'IHDR')
# https://blender.stackexchange.com/questions/240137/is-it-possible-to-create-image-data-from-a-base64-encoded-png
def image_from_data(name, data, height=1, width=1):
import bpy
# Create image, width and height are dummy values
img = bpy.data.images.new(name, height, width)
img.use_fake_user = True # otherwise it won't save to the file
# Set packed file data
img.pack(data=data, data_len=len(data))
# img.reload()
# Switch to file source so it uses the packed file
img.source = 'FILE'
return img
# TODO: will be used for stud.io parts that have textures
# TexMap.base64_to_png(filename, img_data)
def base64_to_png_data(base64_str):
try: # bytes
return base64.decodebytes(base64_str)
except TypeError as e: # string
print(e)
import traceback
print(traceback.format_exc())
base64_str = bytes(base64_str.encode())
return base64.decodebytes(base64_str)
except Exception as e:
print(e)
import traceback
print(traceback.format_exc())
return None
def image_from_base64_str(filename, base64_str):
img_data = base64_to_png_data(base64_str)
return image_from_data(filename, img_data)
def named_png_from_base64_str(filename, base64_str):
filename = f"{Path(filename).stem}.png"
return image_from_base64_str(filename, base64_str)
# basename prevents writing to any place but APP_ROOT
def write_png_data(app_root, filename, data):
filepath = os.path.join(app_root, f"{os.path.basename(filename)}.png")
with open(filepath, 'wb') as file:
file.write(data)