forked from pymupdf/PyMuPDF-Utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sierpinski-fitz.py
75 lines (56 loc) · 2.31 KB
/
sierpinski-fitz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from __future__ import print_function
"""
Created on Wed Jan 30 07:00:00 2019
@author: Jorj
@copyright: (c) 2019 Jorj X. McKie
@license: GNU GPL 3.0
Purpose
--------
Demonstrate the use of PyMuPDF Pixmaps by creating Sierpinski's carpet.
The final picture will be a square with each edge having a length of 3**n.
This is one of several alternative versions, which directly fills the
image's areas, which correspond to "holes" in the carpet.
There is another script available (sierpinski-punch.py), which uses a recursive
approach, which may appear more intuitive.
Comments in the loop indicate different ways to fill the "holes":
1. setRect: fill with some predefined color
2. copyPixmap: copy the corresponding part from some other pixmap.
Here you could use the pixmap of some arbitrary image, for
example some photo.
Whichever method you use, the punch function should show a similar performance.
The time required to save the resulting PNG image very much depends on image
"complexity": it will be longer if you used some photo image in method 2
(copyPixmap) above.
"""
import fitz, time
print(fitz.__doc__)
if not list(map(int, fitz.VersionBind.split("."))) >= [1, 14, 8]:
raise SystemExit("need PyMuPDF v1.14.8 for this script")
mytime = time.clock if str is bytes else time.perf_counter
n = 6
d = 3 ** n # edge length
t0 = mytime()
ir = (0, 0, d, d) # the pixmap rectangle
pm = fitz.Pixmap(fitz.csRGB, ir, False)
pm.setRect(pm.irect, (255, 255, 0)) # fill it with some background color
color = (0, 0, 255) # color to fill the punch holes
# define 'fill' pixmap for the punch holes
fill = fitz.Pixmap(pm, 0) # copy pm
fill.invertIRect(fill.irect) # inverted colors of pm
for lvl in range(0, n + 1):
step = 3 ** (n - lvl)
for x in range(0, 3 ** lvl):
x0 = x * step
if x % 3 == 1:
for y in range(0, 3 ** lvl):
y0 = y * step
if y % 3 == 1:
# pm.setRect((y0, x0, y0 + step, x0 + step), color)
pm.copyPixmap(fill, (y0, x0, y0 + step, x0 + step))
t1 = mytime()
pm.writeImage("sierpinski-fitz.png")
t2 = mytime()
print("Sierpinski's carpet fitz")
print("------------------------")
print("%g sec filling the pixmap" % round(t1 - t0, 3))
print("%g sec saving the picture" % round(t2 - t1, 3))