| 1 |
|
| 2 |
|
| 3 |
import glob |
| 4 |
import os.path as osp |
| 5 |
|
| 6 |
import PIL.Image |
| 7 |
|
| 8 |
|
| 9 |
def tabulate(rows): |
| 10 |
html = "<table>" |
| 11 |
for row in rows: |
| 12 |
html += "\n\t<tr>" |
| 13 |
for col in row: |
| 14 |
html += "\n\t\t<td>{}</td>".format(col) |
| 15 |
html += "\n\t</tr>" |
| 16 |
html += "\n</table>" |
| 17 |
return html |
| 18 |
|
| 19 |
|
| 20 |
def _get_github_image_url(relpath: str) -> str: |
| 21 |
return f"https://github.com/wkentaro/imgviz/raw/main/{relpath}" |
| 22 |
|
| 23 |
|
| 24 |
def main(): |
| 25 |
examples = [] |
| 26 |
for py_file in sorted(glob.glob("examples/*.py")): |
| 27 |
img_file = osp.splitext(osp.basename(py_file))[0] + ".jpg" |
| 28 |
img_file = osp.join("examples/.readme", img_file) |
| 29 |
if not osp.exists(img_file): |
| 30 |
continue |
| 31 |
img = PIL.Image.open(img_file) |
| 32 |
width = 20.0 / img.height * img.width |
| 33 |
examples.append( |
| 34 |
( |
| 35 |
f'<pre><a href="{py_file}">{py_file}</a></pre>', |
| 36 |
f'<img src="{_get_github_image_url(relpath=img_file)}" width="{width}%" />', |
| 37 |
) |
| 38 |
) |
| 39 |
examples = tabulate(examples) |
| 40 |
|
| 41 |
|
| 42 |
dependencies = [] |
| 43 |
for req in ["matplotlib", "numpy", "Pillow>=5.3.0", "PyYAML"]: |
| 44 |
pkg = req |
| 45 |
for sep in "<=>": |
| 46 |
pkg = pkg.split(sep)[0] |
| 47 |
dependencies.append("- [{0}](https://pypi.org/project/{1})".format(req, pkg)) |
| 48 |
dependencies = "\n".join(dependencies) |
| 49 |
|
| 50 |
py_file = "getting_started.py" |
| 51 |
with open(py_file) as f: |
| 52 |
active = False |
| 53 |
lines = [] |
| 54 |
for line in f: |
| 55 |
if line == "# GETTING_STARTED {{\n": |
| 56 |
active = True |
| 57 |
continue |
| 58 |
elif line == "# }} GETTING_STARTED\n": |
| 59 |
active = False |
| 60 |
continue |
| 61 |
if active: |
| 62 |
lines.append(line) |
| 63 |
getting_started = "".join(lines) |
| 64 |
|
| 65 |
README = """\ |
| 66 |
<!-- DO NOT EDIT THIS FILE MANUALLY. This file is generated by generate_readme.py. --> |
| 67 |
|
| 68 |
<h1 align="center"> |
| 69 |
imgviz |
| 70 |
</h1> |
| 71 |
|
| 72 |
<h4 align="center"> |
| 73 |
Image Visualization Tools |
| 74 |
</h4> |
| 75 |
|
| 76 |
<div align="center"> |
| 77 |
<a href="https://pypi.python.org/pypi/imgviz"><img src="https://img.shields.io/pypi/v/imgviz.svg"></a> |
| 78 |
<a href="https://pypi.org/project/imgviz"><img src="https://img.shields.io/pypi/pyversions/imgviz.svg"></a> |
| 79 |
<a href="https://github.com/wkentaro/imgviz/actions"><img src="https://github.com/wkentaro/imgviz/workflows/ci/badge.svg"></a> |
| 80 |
</div> |
| 81 |
|
| 82 |
<div align="center"> |
| 83 |
<a href="#installation"><b>Installation</b></a> | |
| 84 |
<a href="#getting-started"><b>Getting Started</b></a> | |
| 85 |
<a href="#examples"><b>Examples</b></a> | |
| 86 |
<a href="https://github.com/wkentaro/imgviz-cpp"><b>C++ Version</b></a> |
| 87 |
</div> |
| 88 |
|
| 89 |
<br/> |
| 90 |
|
| 91 |
<div align="center"> |
| 92 |
<img src="https://github.com/wkentaro/imgviz/raw/main/.readme/getting_started.jpg" width="95%"> |
| 93 |
</div> |
| 94 |
|
| 95 |
## Installation |
| 96 |
|
| 97 |
```bash |
| 98 |
pip install imgviz |
| 99 |
|
| 100 |
# there are optional dependencies like skimage, below installs all. |
| 101 |
pip install imgviz[all] |
| 102 |
``` |
| 103 |
|
| 104 |
|
| 105 |
## Dependencies |
| 106 |
|
| 107 |
{dependencies} |
| 108 |
|
| 109 |
## Getting Started |
| 110 |
|
| 111 |
```python |
| 112 |
# getting_started.py |
| 113 |
|
| 114 |
{getting_started}``` |
| 115 |
|
| 116 |
## [Examples](examples) |
| 117 |
|
| 118 |
{examples} |
| 119 |
""" |
| 120 |
|
| 121 |
README = README.format( |
| 122 |
getting_started=getting_started, |
| 123 |
dependencies=dependencies, |
| 124 |
examples=examples, |
| 125 |
) |
| 126 |
|
| 127 |
print(README, end="") |
| 128 |
|
| 129 |
|
| 130 |
if __name__ == "__main__": |
| 131 |
main() |