orjson and save

This commit is contained in:
1F616EMO 2023-08-29 18:37:13 +08:00
parent 1384459941
commit 30f807bec0
No known key found for this signature in database
GPG Key ID: EF52EFA8E05859B2
3 changed files with 22 additions and 7 deletions

View File

@ -158,3 +158,6 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear # and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/ #.idea/
out.png

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
import json, numpy as np, matplotlib.pyplot as plt import orjson, numpy as np, matplotlib.pyplot as plt
def color_int2tuple(colorint): def color_int2tuple(colorint):
# https://stackoverflow.com/a/2262152, License https://creativecommons.org/licenses/by-sa/2.5/ # https://stackoverflow.com/a/2262152, License https://creativecommons.org/licenses/by-sa/2.5/
@ -12,13 +12,13 @@ def main(argv):
return 1 return 1
filename = argv[1] filename = argv[1]
try: try:
file = open(filename, "r") file = open(filename, "rb")
except IOError: except IOError:
print("Error opening file.") print("Error opening file.")
raise raise
else: else:
with file: with file:
data = json.load(file) data = orjson.loads(file.read())
assert "map" in data, "Invalid JSON savefile format" assert "map" in data, "Invalid JSON savefile format"
plts = np.array(tuple(tuple(color_int2tuple(int(c)) for c in r) for r in data["map"]), dtype=np.uint8) plts = np.array(tuple(tuple(color_int2tuple(int(c)) for c in r) for r in data["map"]), dtype=np.uint8)
if len(argv) >= 3: if len(argv) >= 3:
@ -34,11 +34,11 @@ def main(argv):
plts = np.rot90(plts, axes = (0,1)) plts = np.rot90(plts, axes = (0,1))
case "-90" | "270" | "+270": case "-90" | "270" | "+270":
plts = np.rot90(plts, k = -1, axes = (0,1)) plts = np.rot90(plts, k = -1, axes = (0,1))
plt.figure(facecolor=(0, 0, 0))
plt.imshow(plts) plt.imshow(plts)
try: plt.axis('off')
plt.show() plt.savefig('out.png', bbox_inches='tight')
except KeyboardInterrupt: print("Image saved at out.png.")
pass
return 0 return 0
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -0,0 +1,12 @@
contourpy==1.1.0
cycler==0.11.0
fonttools==4.42.1
kiwisolver==1.4.5
matplotlib==3.7.2
numpy==1.25.2
orjson==3.9.5
packaging==23.1
Pillow==10.0.0
pyparsing==3.0.9
python-dateutil==2.8.2
six==1.16.0