From 19c415c6d7e57b033939b133d4aee791fd67f01c Mon Sep 17 00:00:00 2001 From: KV Date: Wed, 19 Aug 2020 09:40:01 +0200 Subject: [PATCH] Add aspect_ratio() function that reads image size from file - To be able to find an image file with relative path, the directory of the .gv file output is injected into each Image data object. - aspect_ratio() prints a warning and returns 1:1 ratio if it fails. --- src/wireviz/DataClasses.py | 8 +++++--- src/wireviz/wireviz.py | 5 +++++ src/wireviz/wv_helper.py | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/wireviz/DataClasses.py b/src/wireviz/DataClasses.py index a27e082..c71771f 100644 --- a/src/wireviz/DataClasses.py +++ b/src/wireviz/DataClasses.py @@ -3,12 +3,14 @@ from typing import Optional, List, Any, Union from dataclasses import dataclass, field -from wireviz.wv_helper import int2tuple +from pathlib import Path +from wireviz.wv_helper import int2tuple, aspect_ratio from wireviz import wv_colors @dataclass class Image: + gv_dir: Path # Directory of .gv file injected as context during parsing # Attributes of the image object : src: str scale: Optional[str] = None # false | true | width | height | both @@ -35,10 +37,10 @@ class Image: # If only one dimension is specified, compute the other because both are required. if self.height: if not self.width: - self.width = self.height # Assuming 1:1 aspect ratio for now. + self.width = self.height * aspect_ratio(self.gv_dir.joinpath(self.src)) else: if self.width: - self.height = self.width # Assuming 1:1 aspect ratio for now. + self.height = self.width / aspect_ratio(self.gv_dir.joinpath(self.src)) @dataclass diff --git a/src/wireviz/wireviz.py b/src/wireviz/wireviz.py index 3adc078..5543e09 100755 --- a/src/wireviz/wireviz.py +++ b/src/wireviz/wireviz.py @@ -44,6 +44,11 @@ def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, st if len(yaml_data[sec]) > 0: if ty == dict: for key, attribs in yaml_data[sec].items(): + # The Image dataclass might need to open an image file with a relative path. + image = attribs.get('image') + if isinstance(image, dict): + image['gv_dir'] = Path(file_out if file_out else '').parent # Inject context + if sec == 'connectors': if not attribs.get('autogenerate', False): harness.add_connector(name=key, **attribs) diff --git a/src/wireviz/wv_helper.py b/src/wireviz/wv_helper.py index 33936ed..5535ce7 100644 --- a/src/wireviz/wv_helper.py +++ b/src/wireviz/wv_helper.py @@ -158,6 +158,21 @@ def open_file_write(filename): def open_file_append(filename): return open(filename, 'a', encoding='UTF-8') + +def aspect_ratio(image_src): + try: + from PIL import Image + image = Image.open(image_src) + if image.width > 0 and image.height > 0: + return image.width / image.height + print(f'aspect_ratio(): image size is {image.width} x {image.height}') + except ModuleNotFoundError as error: + print(f'aspect_ratio(): {error}') + except FileNotFoundError as error: + print(f'aspect_ratio(): {error}') + return 1 # Assume 1:1 when unable to read actual image size + + def manufacturer_info_field(manufacturer, mpn): if manufacturer or mpn: return f'{manufacturer if manufacturer else "MPN"}{": " + str(mpn) if mpn else ""}'