Skip to content

Commit 3a14cbe

Browse files
author
Jacob Erickson
committed
Add save/load from png
1 parent e212fc9 commit 3a14cbe

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

src/wireviz/wireviz.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from typing import Any, Tuple
99

1010
import yaml
11+
from PIL import Image
12+
from PIL.PngImagePlugin import PngInfo
1113

1214
if __name__ == '__main__':
1315
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
@@ -181,6 +183,7 @@ def check_designators(what, where): # helper function
181183

182184
if file_out is not None:
183185
harness.output(filename=file_out, fmt=('png', 'svg'), view=False)
186+
save_yaml_to_png(file_out,yaml_input)
184187

185188
if return_types is not None:
186189
returns = []
@@ -223,6 +226,16 @@ def parse_cmdline():
223226
parser.add_argument('--prepend-file', action='store', type=str, metavar='YAML_FILE')
224227
return parser.parse_args()
225228

229+
def save_yaml_to_png(file_out,yaml_input):
230+
with Image.open(fp=f'{file_out}.png') as im:
231+
txt = PngInfo()
232+
txt.add_itxt('yaml',yaml_input,zip=True)
233+
im.save(fp=f'{file_out}.png',pnginfo=txt)
234+
235+
def read_yaml_from_png(file_in):
236+
with Image.open(fp=f'{file_in}.png') as im:
237+
im.load()
238+
return im.text['yaml']
226239

227240
def main():
228241

@@ -232,16 +245,19 @@ def main():
232245
print(f'Error: input file {args.input_file} inaccessible or does not exist, check path')
233246
sys.exit(1)
234247

235-
with open_file_read(args.input_file) as fh:
236-
yaml_input = fh.read()
237-
238-
if args.prepend_file:
239-
if not os.path.exists(args.prepend_file):
240-
print(f'Error: prepend input file {args.prepend_file} inaccessible or does not exist, check path')
241-
sys.exit(1)
242-
with open_file_read(args.prepend_file) as fh:
243-
prepend = fh.read()
244-
yaml_input = prepend + yaml_input
248+
if ".png" in args.input_file:
249+
yaml_input = read_yaml_from_png(args.input_file.replace('.png',''))
250+
else:
251+
with open_file_read(args.input_file) as fh:
252+
yaml_input = fh.read()
253+
254+
if args.prepend_file:
255+
if not os.path.exists(args.prepend_file):
256+
print(f'Error: prepend input file {args.prepend_file} inaccessible or does not exist, check path')
257+
sys.exit(1)
258+
with open_file_read(args.prepend_file) as fh:
259+
prepend = fh.read()
260+
yaml_input = prepend + yaml_input
245261

246262
if not args.output_file:
247263
file_out = args.input_file

0 commit comments

Comments
 (0)