Commit 2b078b81 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

draw.py: improve drawing output resolution

parent 42f7417d
......@@ -27,26 +27,43 @@ from analyze import analyze_entries
from locks import acquire_lock
from util import *
import numpy
import PIL
PIL.Image.MAX_IMAGE_PIXELS = 99999999999
def cache_draw(f):
@wraps(f)
def wrap(entry, alt, **kwargs):
def wrap(entry, alt, svg=False, thumb=False, **kwargs):
if len(kwargs) > 0:
return f(entry, alt, **kwargs)
base = f"{process_entry.root}/cache/draw/{entry.name}"
fname = f"{base}/{alt}.png"
bfname = f"{base}/{alt}"
for k in sorted(kwargs.keys()):
bfname += f"-{k}{kwargs[k]}"
if svg:
fname = f"{bfname}.svg"
else:
fname = f"{bfname}.png"
if not os.path.exists(fname):
os.makedirs(base, exist_ok=True)
buf = f(entry, alt)
buf = f(entry, alt, svg)
if buf is not None:
with open(fname, "wb") as fo:
shutil.copyfileobj(buf, fo)
if os.path.exists(fname):
return open(fname, "rb")
return None
else:
return None
if not svg and thumb:
tfname = f"{bfname}-thumb.png"
if not os.path.exists(tfname):
im = PIL.Image.open(fname)
im.thumbnail((1000, 1000), reducing_gap=10)
im.save(tfname, "PNG")
fname = tfname
return open(fname, "rb")
return wrap
@cache_draw
def draw_entry(entry, alt, svg=False, size=1000, bg_color="#cdcdcd",
def draw_entry(entry, alt, svg=False, size=None, bg_color="#cdcdcd",
edge_color=None, full=False):
if svg:
......@@ -68,8 +85,10 @@ def draw_entry(entry, alt, svg=False, size=1000, bg_color="#cdcdcd",
c = gt.label_largest_component(g, directed=False)
if c.fa.sum() > .6 * g.num_vertices() and not full:
g = gt.Graph(gt.GraphView(g, vfilt=c), prune=True)
g = gt.GraphView(g, vfilt=c)
if size is None:
size = max(2000, int(20 * numpy.sqrt(g.num_vertices())))
gt.graph_draw(g, pos=g.vp["_pos"], fmt=fmt, edge_color=edge_color,
bg_color=bg_color, output_size=(size, size),
adjust_aspect=False, output=buf)
......@@ -95,4 +114,4 @@ if __name__ == "__main__":
analyze_entries([entry], cache_only=True)
for alt, g in entry.parse(cache_only=True, lazy=True):
print(f"\t{alt}...")
draw_entry(entry, alt)
draw_entry(entry, alt, thumb=True)
......@@ -206,7 +206,9 @@ def net_draw(net, alt=None):
try:
entry = entries[net]
if len(request.args) > 0:
thumb = bool(request.args.get('thumb', False))
if not thumb and len(request.args) > 0:
svg = bool(request.args.get('svg', False))
size = min(int(request.args.get('size', 1000)), 3000)
bg_color = request.args.get('bg_color', "#cdcdcd")
......@@ -215,7 +217,7 @@ def net_draw(net, alt=None):
buf = draw.draw_entry(entry, alt, svg=svg, size=size,
bg_color=bg_color, edge_color=edge_color)
else:
buf = draw.draw_entry(entry, alt)
buf = draw.draw_entry(entry, alt, thumb=thumb)
if bool(request.args.get('svg', False)):
return send_file(buf, mimetype="image/svg+xml")
......
......@@ -205,13 +205,18 @@ active
{% endif %}
{% set props = props | map('first') %}
{% if '_pos' in props %}
<a href="#{{alt}}" id="{{alt}}_draw">
<figure style="width:24%; display: inline-block;">
{% if file[1] is none %}
<img src="/draw/{{entry.name}}" alt="{{alt}} drawing" style="width: 100%"/>
<a href="/draw/{{entry.name}}">
<img src="/draw/{{entry.name}}?thumb=True" alt="{{alt}} drawing" style="width: 100%"/>
</a>
{% else %}
<img src="/draw/{{entry.name}}/{{alt}}" alt="{{alt}} drawing" style="width: 100%"/>
<figcaption style="text-align: center">{{ alt }}</figcaption>
<a href="/draw/{{entry.name}}/{{alt}}">
<img src="/draw/{{entry.name}}/{{alt}}?thumb=True" alt="{{alt}} drawing" style="width: 100%"/>
</a>
<figcaption style="text-align: center">
<a href="#{{alt}}" id="{{alt}}_draw">{{ alt }}</a>
</figcaption>
{% endif %}
</figure>
</a>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment