is-page-empty 1.0.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
try:
|
|
2
|
+
import skimage
|
|
3
|
+
def load_image(fn):
|
|
4
|
+
return skimage.io.imread(fn)
|
|
5
|
+
except ImportError:
|
|
6
|
+
try:
|
|
7
|
+
import imageio.v3 as iio
|
|
8
|
+
def load_image(fn):
|
|
9
|
+
return iio.imread(fn)
|
|
10
|
+
except ImportError:
|
|
11
|
+
import PIL, numpy
|
|
12
|
+
def load_image(fn):
|
|
13
|
+
im = PIL.Image.open(fn)
|
|
14
|
+
return numpy.array(im)
|
|
15
|
+
|
|
16
|
+
import sys
|
|
17
|
+
|
|
18
|
+
CUTOFF = 0.02
|
|
19
|
+
|
|
20
|
+
def score(im):
|
|
21
|
+
ratio_bright = (im > 127).mean()
|
|
22
|
+
return min(ratio_bright, 1-ratio_bright)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def is_empty(im, cutoff=CUTOFF):
|
|
26
|
+
if isinstance(im, str):
|
|
27
|
+
im = load_image(im)
|
|
28
|
+
if len(im.shape) == 3:
|
|
29
|
+
im = im[..., 0] / 3 + im[..., 1] / 3 + im[..., 2] / 3
|
|
30
|
+
|
|
31
|
+
smaller_dim, larger_dim = sorted(im.shape)
|
|
32
|
+
max_stride = smaller_dim / 10
|
|
33
|
+
|
|
34
|
+
scores = [
|
|
35
|
+
score(im[
|
|
36
|
+
x:int(x+max_stride),
|
|
37
|
+
y:int(y+max_stride)
|
|
38
|
+
])
|
|
39
|
+
for x in range(0, (im.shape[0] - int(max_stride) - 1), int(max_stride))
|
|
40
|
+
for y in range(0, (im.shape[1] - int(max_stride) - 1), int(max_stride))
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
im_score = sum(sorted(scores)[-3:]) / 3
|
|
44
|
+
return im_score < cutoff, im_score
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def main():
|
|
48
|
+
import argparse
|
|
49
|
+
parser = argparse.ArgumentParser()
|
|
50
|
+
parser.add_argument('infile')
|
|
51
|
+
parser.add_argument('-c', dest='cutoff', type=float, default=CUTOFF)
|
|
52
|
+
parser.add_argument('-r', dest='retval', action='store_true')
|
|
53
|
+
parser.add_argument('-v', dest='verbose', action='store_true')
|
|
54
|
+
args = parser.parse_args()
|
|
55
|
+
empty, im_score = is_empty(args.infile, args.cutoff)
|
|
56
|
+
if args.verbose:
|
|
57
|
+
verdict = '' if empty else 'not '
|
|
58
|
+
print(f"File {args.infile} is {verdict}empty (score: {im_score})")
|
|
59
|
+
if args.retval:
|
|
60
|
+
status=(0 if empty else 1)
|
|
61
|
+
sys.exit(status)
|
|
62
|
+
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: is-page-empty
|
|
3
|
+
Version: 1.0.4
|
|
4
|
+
Summary: A very simple script that tests whether an image is likely empty.
|
|
5
|
+
Project-URL: Homepage, https://github.com/johannes-bauer/is_page_empty
|
|
6
|
+
Author-email: Johannes Bauer <atomm-clap@interesting-problems.de>
|
|
7
|
+
License: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: argument-parser,cli,subcommands
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
13
|
+
Requires-Python: >=3.10
|
|
14
|
+
Provides-Extra: imageio
|
|
15
|
+
Requires-Dist: imageio>=2.0.0; extra == 'imageio'
|
|
16
|
+
Provides-Extra: pil
|
|
17
|
+
Requires-Dist: numpy>=2.2; extra == 'pil'
|
|
18
|
+
Requires-Dist: pillow>=12.0.0; extra == 'pil'
|
|
19
|
+
Provides-Extra: skimage
|
|
20
|
+
Requires-Dist: scikit-image>=0.20.0; extra == 'skimage'
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
|
|
23
|
+
# is_page_empty
|
|
24
|
+
|
|
25
|
+
A simple package/script that checks whether a given image file is empty.
|
|
26
|
+
|
|
27
|
+
Install it with one of the optional dependencies `skimage`, `imageio`, or `pil` or provide that skimage, imageio, or pillow are installed.
|
|
28
|
+
|
|
29
|
+
Then you can check whether an image file is empty using the commandline:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
if is_page_empty some_image.png; then echo "Yes, it's empty"; else echo "No, it's not empty."; fi
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Or you can use the package to check programmatically
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
import is_page_empty
|
|
39
|
+
if is_empty('path/to/image/file.png'):
|
|
40
|
+
print("That file is empty.")
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The script simply cuts the page into small squares, counts the number of pixels below a certain threshold in each, and compares the sum of those numbers in the squares with the highest numbers to a cutoff threshold.
|
|
44
|
+
|
|
45
|
+
This has been very robust, in my experience, for scanned text; in particular in double-sided prints, where some of the print on the other side may shine through, and in pages with punched holes, which are rendered black in the print.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
is_page_empty/__init__.py,sha256=JbN4rTxBYuId_hY0EFbcJdEkIZcI39fgIKxKAUPLh84,1737
|
|
2
|
+
is_page_empty-1.0.4.dist-info/METADATA,sha256=3aQTf7fxfP8B0qg5YZiu6XccoDc8x146VkQ_QSdALf8,1856
|
|
3
|
+
is_page_empty-1.0.4.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
4
|
+
is_page_empty-1.0.4.dist-info/entry_points.txt,sha256=UF9TaZcI1JzdtdbziPLEmKtbclCivNHW9fIhHolywr8,53
|
|
5
|
+
is_page_empty-1.0.4.dist-info/licenses/LICENSE,sha256=tdWMFnpC0OpoU7-yfwW0RcSvdMsIufbN9MzsDU2OK4Y,1071
|
|
6
|
+
is_page_empty-1.0.4.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 johannes-bauer
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|