Skip to content

Commit 5ec0e34

Browse files
authored
Merge pull request #75 from bertsky/fix-clip-mode-with-alpha
clipping: fix mode with input alpha
2 parents a6f90ab + 70b5615 commit 5ec0e34

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

ocrd_cis/ocrd-tool.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"git_url": "https://github.com/cisocrgroup/ocrd_cis",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
44
"tools": {
55
"ocrd-cis-ocropy-binarize": {
66
"executable": "ocrd-cis-ocropy-binarize",

ocrd_cis/ocropy/clip.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import os.path
44
import numpy as np
55
from PIL import Image, ImageStat, ImageOps
6-
from scipy.ndimage import filters
76
from shapely.geometry import Polygon
87
from shapely.prepared import prep
98

@@ -128,7 +127,7 @@ def process(self):
128127
else:
129128
background = background.median[0]
130129
if level == 'region':
131-
background_image = Image.new('L', page_image.size, background)
130+
background_image = Image.new(page_image.mode, page_image.size, background)
132131
page_array = pil2array(page_image)
133132
page_bin = np.array(page_array <= midrange(page_array), np.uint8)
134133
# in absolute coordinates merely for comparison/intersection
@@ -137,13 +136,14 @@ def process(self):
137136
# in relative coordinates for mask/cropping
138137
polygons = [coordinates_of_segment(region, page_image, page_coords)
139138
for region in regions]
140-
masks = [pil2array(polygon_mask(page_image, polygon)).astype(np.uint8)
141-
for polygon in polygons]
142-
for i, mask in enumerate(masks[num_texts:], num_texts):
139+
for i, polygon in enumerate(polygons[num_texts:], num_texts):
143140
# for non-text regions, extend mask by 3 pixels in each direction
144141
# to ensure they do not leak components accidentally
145142
# (accounts for bad cropping of such regions in GT):
146-
masks[i] = filters.maximum_filter(mask, 7)
143+
polygon = Polygon(polygon).buffer(3).exterior.coords[:-1] # keep open
144+
polygons[i] = polygon
145+
masks = [pil2array(polygon_mask(page_image, polygon)).astype(np.uint8)
146+
for polygon in polygons]
147147
for i, region in enumerate(regions):
148148
if i >= num_texts:
149149
break # keep non-text regions unchanged
@@ -172,7 +172,7 @@ def process(self):
172172
continue
173173
region_image, region_coords = self.workspace.image_from_segment(
174174
region, page_image, page_coords, feature_selector='binarized')
175-
background_image = Image.new('L', region_image.size, background)
175+
background_image = Image.new(region_image.mode, region_image.size, background)
176176
region_array = pil2array(region_image)
177177
region_bin = np.array(region_array <= midrange(region_array), np.uint8)
178178
# in absolute coordinates merely for comparison/intersection

0 commit comments

Comments
 (0)