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.
This commit is contained in:
KV 2020-08-19 09:40:01 +02:00
parent 285a28dff6
commit 19c415c6d7
3 changed files with 25 additions and 3 deletions

View File

@ -3,12 +3,14 @@
from typing import Optional, List, Any, Union from typing import Optional, List, Any, Union
from dataclasses import dataclass, field 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 from wireviz import wv_colors
@dataclass @dataclass
class Image: class Image:
gv_dir: Path # Directory of .gv file injected as context during parsing
# Attributes of the image object <img>: # Attributes of the image object <img>:
src: str src: str
scale: Optional[str] = None # false | true | width | height | both 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 only one dimension is specified, compute the other because both are required.
if self.height: if self.height:
if not self.width: 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: else:
if self.width: 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 @dataclass

View File

@ -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 len(yaml_data[sec]) > 0:
if ty == dict: if ty == dict:
for key, attribs in yaml_data[sec].items(): 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 sec == 'connectors':
if not attribs.get('autogenerate', False): if not attribs.get('autogenerate', False):
harness.add_connector(name=key, **attribs) harness.add_connector(name=key, **attribs)

View File

@ -158,6 +158,21 @@ def open_file_write(filename):
def open_file_append(filename): def open_file_append(filename):
return open(filename, 'a', encoding='UTF-8') 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): def manufacturer_info_field(manufacturer, mpn):
if manufacturer or mpn: if manufacturer or mpn:
return f'{manufacturer if manufacturer else "MPN"}{": " + str(mpn) if mpn else ""}' return f'{manufacturer if manufacturer else "MPN"}{": " + str(mpn) if mpn else ""}'