#!/usr/bin/env python
import glob
import os.path as osp
import PIL.Image
def tabulate(rows):
html = "
"
for row in rows:
html += "\n\t"
for col in row:
html += "\n\t\t| {} | ".format(col)
html += "\n\t
"
html += "\n
"
return html
def _get_github_image_url(relpath: str) -> str:
return f"https://github.com/wkentaro/imgviz/raw/main/{relpath}"
def main():
examples = []
for py_file in sorted(glob.glob("examples/*.py")):
img_file = osp.splitext(osp.basename(py_file))[0] + ".jpg"
img_file = osp.join("examples/.readme", img_file)
if not osp.exists(img_file):
continue
img = PIL.Image.open(img_file)
width = 20.0 / img.height * img.width
examples.append(
(
f'{py_file}',
f'
', # NOQA: E501
)
)
examples = tabulate(examples)
# TODO: read from pyproject.toml
dependencies = []
for req in ["matplotlib", "numpy", "Pillow>=5.3.0", "PyYAML"]:
pkg = req
for sep in "<=>":
pkg = pkg.split(sep)[0]
dependencies.append("- [{0}](https://pypi.org/project/{1})".format(req, pkg))
dependencies = "\n".join(dependencies)
py_file = "getting_started.py"
with open(py_file) as f:
active = False
lines = []
for line in f:
if line == "# GETTING_STARTED {{\n":
active = True
continue
elif line == "# }} GETTING_STARTED\n":
active = False
continue
if active:
lines.append(line)
getting_started = "".join(lines)
README = """\
imgviz
Image Visualization Tools
## Installation
```bash
pip install imgviz
# there are optional dependencies like skimage, below installs all.
pip install imgviz[all]
```
## Dependencies
{dependencies}
## Getting Started
```python
# getting_started.py
{getting_started}```
## [Examples](examples)
{examples}
""" # NOQA
README = README.format(
getting_started=getting_started,
dependencies=dependencies,
examples=examples,
)
print(README, end="")
if __name__ == "__main__":
main()