ソースを参照

Output and HTML page with images as data URLs rather than outputting the
image directly.

Andrew Klopper 6 年 前
コミット
00432432ce
共有4 個のファイルを変更した55 個の追加12 個の削除を含む
  1. 14 9
      home/templates/home/index.html
  2. 33 0
      home/templates/home/output.html
  3. 8 2
      home/views.py
  4. 0 1
      home/xword.py

+ 14 - 9
home/templates/home/index.html

@@ -1,11 +1,16 @@
1
+<!DOCTYPE html>
1 2
 <html>
2
-	<head>
3
-		<title>Crossword Extractor</title>
4
-	</head>
5
-	<body>
6
-		<form enctype="multipart/form-data" method="post" action="">
7
-			<div>{{ form }}</div>
8
-			<div><input type="submit" value="Upload"></div>
9
-		</form>
10
-	</body>
3
+  <head>
4
+    <meta charset="UTF-8">
5
+    <title>Crossword Extractor</title>
6
+  </head>
7
+  <body>
8
+    <form enctype="multipart/form-data" method="post" action="">
9
+      <div>{{ form }}</div>
10
+      <div><input type="submit" value="Upload"></div>
11
+    </form>
12
+  </body>
11 13
 </html>
14
+<!--
15
+vim:ts=2:sw=2:expandtab
16
+-->

+ 33 - 0
home/templates/home/output.html

@@ -0,0 +1,33 @@
1
+<!DOCTYPE html>
2
+<html>
3
+  <head>
4
+    <meta charset="UTF-8">
5
+    <title>Extracted Crossword</title>
6
+    <style>
7
+      img {
8
+        width: 600px;
9
+      }
10
+    </style>
11
+  </head>
12
+  <body>
13
+    {% if warnings %}
14
+    <div>
15
+      Warnings:
16
+      <ul>
17
+        {% for warning in warnings %}
18
+          <li>{{ warning }}</li>
19
+        {% endfor %}
20
+      </ul>
21
+    </div>
22
+    {% endif %}
23
+    <div>
24
+      <a href="{{ image_url }}" download="{{ image_file_name }}">Download</a>
25
+    </div>
26
+    <div>
27
+      <img alt="Crossword" src="{{ image_url }}">
28
+    </div>
29
+  </body>
30
+</html>
31
+<!--
32
+vim:ts=2:sw=2:expandtab
33
+-->

+ 8 - 2
home/views.py

@@ -1,3 +1,5 @@
1
+from base64 import b64encode
2
+from datetime import datetime
1 3
 from django.http import HttpResponse, HttpResponseBadRequest
2 4
 from django.shortcuts import render
3 5
 from django.views import View
@@ -13,5 +15,9 @@ class HomeView(View):
13 15
         form = CrosswordForm(request.POST, request.FILES)
14 16
         if not form.is_valid():
15 17
             return HttpResponseBadRequest('Invalid form data')
16
-        image, warning = extract_crossword(form.cleaned_data['file'].temporary_file_path())
17
-        return HttpResponse(image, content_type='image/png')
18
+        image, warnings = extract_crossword(form.cleaned_data['file'].temporary_file_path())
19
+        return render(request, 'home/output.html', {
20
+            'warnings': warnings,
21
+            'image_file_name': 'xword_{}.png'.format(datetime.now().strftime('%Y%m%d_%H%M%S')),
22
+            'image_url': 'data:image/png;base64,' + b64encode(image).decode()
23
+        })

+ 0 - 1
home/xword.py

@@ -1,7 +1,6 @@
1 1
 import math
2 2
 import cv2
3 3
 import numpy as np
4
-import peakutils
5 4
 import copy
6 5
 import argparse
7 6