HuguesdeF commited on
Commit
1c92412
1 Parent(s): b47f2f2

Bugfix, back to old version

Browse files
Files changed (2) hide show
  1. Corriger.py +1 -0
  2. code/functions.py +38 -12
Corriger.py CHANGED
@@ -164,6 +164,7 @@ elif authentication_status:
164
  buf = BytesIO()
165
  # img_stacked = np.hstack((resized_image_input, resized_img_final))
166
  img_final = Image.fromarray(resized_img_final).convert("L")
 
167
  img_final.save(buf, format="PNG")
168
  byte_im = buf.getvalue()
169
 
 
164
  buf = BytesIO()
165
  # img_stacked = np.hstack((resized_image_input, resized_img_final))
166
  img_final = Image.fromarray(resized_img_final).convert("L")
167
+
168
  img_final.save(buf, format="PNG")
169
  byte_im = buf.getvalue()
170
 
code/functions.py CHANGED
@@ -2,15 +2,16 @@ import cv2
2
  import numpy as np
3
  import matplotlib.pyplot as plt
4
  import os
 
5
  import cairosvg
6
  from potrace import POTRACE_CORNER, Path, Bitmap
7
  import io
8
  from PIL import Image, ImageStat
9
 
10
- import streamlit
11
  from PIL import Image
12
 
13
- @streamlit.cache_data
14
  def pipeline_svg(image_input, size_value, level=3, streamlit=False, threshold=0, kernel_type=cv2.MORPH_ELLIPSE, dilate_lines_value=0):
15
  """
16
  uint8 ==> uint8
@@ -40,17 +41,22 @@ def process_svg(img, size_value=12, level=1, streamlit=False, kernel_type=cv2.MO
40
  kernel = get_kernel_ellipse(size, kernel_type=kernel_type)
41
  img = cv2.erode(img, kernel, iterations=1)
42
 
43
- #cv2.imwrite(image_path, img)
44
- img_array = convert_to_svg_and_back(img)
 
 
 
 
 
45
 
46
  img_array = binarise(img_array)
47
- img_bin = (255 - img_array)
48
  img_bin = img_bin.astype('uint8')
49
  image_already_added = np.zeros_like(img_bin)
50
 
51
  target_min_size = max(1, size_value)
52
 
53
- image_final = img_bin
54
  for i in range(target_min_size+1):
55
  size = 2 * i + 1
56
  kernel = get_kernel_ellipse(size, kernel_type=kernel_type)
@@ -65,14 +71,22 @@ def process_svg(img, size_value=12, level=1, streamlit=False, kernel_type=cv2.MO
65
  kernel = get_kernel_ellipse(size, kernel_type=kernel_type)
66
  dilate_image_petits_objets = cv2.dilate(image_petits_objets, kernel, iterations=1)
67
 
68
- image_already_added = (image_already_added + image_petits_objets)
69
 
70
  if i > level:
71
- image_final = (image_final + dilate_image_petits_objets)
 
 
 
 
 
 
 
 
 
72
 
73
- #cv2.imwrite("image_finale.png", (255 - image_final))
74
- image = convert_to_svg_and_back((255 - image_final))
75
  return image
 
76
  def get_kernel_ellipse(size, kernel_type=cv2.MORPH_ELLIPSE):
77
  list_coords = [size, size]
78
  return cv2.getStructuringElement(kernel_type, (list_coords[0], list_coords[1]),
@@ -101,7 +115,16 @@ def remove_solo_pixels(image, kernel_size=3):
101
  dilation = dilation.astype('uint8')
102
  return dilation
103
 
104
- def convert_to_svg_and_back(image_array) -> np.array:
 
 
 
 
 
 
 
 
 
105
  image_pil = Image.fromarray(image_array)
106
 
107
  bm = Bitmap(image_pil, blacklevel=0.5)
@@ -115,7 +138,9 @@ def convert_to_svg_and_back(image_array) -> np.array:
115
 
116
  image = backend_svg_no_file(image_pil, plist)
117
 
118
- return np.array(image)
 
 
119
 
120
  def backend_svg_no_file(image, path: Path):
121
  output = f'<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="{image.width}" height="{image.height}" viewBox="0 0 {image.width} {image.height}">'
@@ -139,6 +164,7 @@ def backend_svg_no_file(image, path: Path):
139
  output += f'<path stroke="none" fill="#000000" fill-rule="evenodd" d="{"".join(parts)}"/>'
140
 
141
  output += "</svg>"
 
142
  # From svg to png (bytes)
143
  image_data = cairosvg.surface.PNGSurface.convert(output)
144
  image = Image.open(io.BytesIO(image_data)).split()[-1]
 
2
  import numpy as np
3
  import matplotlib.pyplot as plt
4
  import os
5
+ import copy
6
  import cairosvg
7
  from potrace import POTRACE_CORNER, Path, Bitmap
8
  import io
9
  from PIL import Image, ImageStat
10
 
11
+ import streamlit as st
12
  from PIL import Image
13
 
14
+ @st.cache_data
15
  def pipeline_svg(image_input, size_value, level=3, streamlit=False, threshold=0, kernel_type=cv2.MORPH_ELLIPSE, dilate_lines_value=0):
16
  """
17
  uint8 ==> uint8
 
41
  kernel = get_kernel_ellipse(size, kernel_type=kernel_type)
42
  img = cv2.erode(img, kernel, iterations=1)
43
 
44
+ cv2.imwrite(image_path, img)
45
+
46
+ #st.image(img / 255.0, caption="Image après premiere svg and back", use_column_width='auto')
47
+
48
+ img_array = convert_to_svg_and_back(image_path)
49
+
50
+ #st.image(img_array / 255.0, caption="Image après premiere svg and back", use_column_width='auto')
51
 
52
  img_array = binarise(img_array)
53
+ img_bin = 255 - img_array
54
  img_bin = img_bin.astype('uint8')
55
  image_already_added = np.zeros_like(img_bin)
56
 
57
  target_min_size = max(1, size_value)
58
 
59
+ image_final = copy.deepcopy(img_bin)
60
  for i in range(target_min_size+1):
61
  size = 2 * i + 1
62
  kernel = get_kernel_ellipse(size, kernel_type=kernel_type)
 
71
  kernel = get_kernel_ellipse(size, kernel_type=kernel_type)
72
  dilate_image_petits_objets = cv2.dilate(image_petits_objets, kernel, iterations=1)
73
 
74
+ image_already_added += image_petits_objets
75
 
76
  if i > level:
77
+ image_final += dilate_image_petits_objets
78
+
79
+ cv2.imwrite("image_finale.png", (255 - image_final))
80
+ #st.image((255 - image_final) / 255.0, caption="(255 - image_final)", use_column_width='auto')
81
+
82
+ #image = convert_to_svg_and_back((255-image_final))
83
+ #image = 255 - image
84
+ #st.image((image) / 255.0, caption="convert_to_svg_and_back_new", use_column_width='auto')
85
+
86
+ image = convert_to_svg_and_back("image_finale.png")
87
 
 
 
88
  return image
89
+
90
  def get_kernel_ellipse(size, kernel_type=cv2.MORPH_ELLIPSE):
91
  list_coords = [size, size]
92
  return cv2.getStructuringElement(kernel_type, (list_coords[0], list_coords[1]),
 
115
  dilation = dilation.astype('uint8')
116
  return dilation
117
 
118
+ def convert_to_svg_and_back(image_path):
119
+ cmd_to_svg = f"potracer {image_path} -b svg -o images/image.svg"
120
+ cmd_to_raster = f"convert images/image.svg -colorspace Gray images/output.png"
121
+
122
+ assert (os.system(cmd_to_svg)) == 0, f"Error with {cmd_to_svg}"
123
+ assert (os.system(cmd_to_raster)) == 0, f"Error with {cmd_to_raster}"
124
+
125
+ return np.array(Image.open("images/output.png").convert('L'))
126
+
127
+ def convert_to_svg_and_back_new(image_array) -> np.array:
128
  image_pil = Image.fromarray(image_array)
129
 
130
  bm = Bitmap(image_pil, blacklevel=0.5)
 
138
 
139
  image = backend_svg_no_file(image_pil, plist)
140
 
141
+ image = np.array(image)
142
+
143
+ return image
144
 
145
  def backend_svg_no_file(image, path: Path):
146
  output = f'<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="{image.width}" height="{image.height}" viewBox="0 0 {image.width} {image.height}">'
 
164
  output += f'<path stroke="none" fill="#000000" fill-rule="evenodd" d="{"".join(parts)}"/>'
165
 
166
  output += "</svg>"
167
+ print(output)
168
  # From svg to png (bytes)
169
  image_data = cairosvg.surface.PNGSurface.convert(output)
170
  image = Image.open(io.BytesIO(image_data)).split()[-1]