8000 add examples from previous version · libvips/pyvips@6b688f2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6b688f2

Browse files
committed
add examples from previous version
1 parent 9418143 commit 6b688f2

18 files changed

+423
-0
lines changed

README.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ with::
2323
import pyvips
2424
Vips = pyvips
2525

26+
Instead of the `pyvips = Vips`, you can also swap all `Vips` for `pyvips` with
27+
eg.:
28+
29+
%s/Vips/pyvips/g
30+
2631
Background
2732
----------
2833

examples/cod.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/python
2+
3+
import sys
4+
5+
import logging
6+
#logging.basicConfig(level = logging.DEBUG)
7+
8+
import pyvips
9+
10+
#pyvips.cache_set_trace(True)
11+
12+
def to_polar(image):
13+
"""Transform image coordinates to polar.
14+
15+
The image is transformed so that it is wrapped around a point in the
16+
centre. Vertical straight lines become circles or segments of circles,
17+
horizontal straight lines become radial spokes.
18+
"""
19+
# xy image, origin in the centre, scaled to fit image to a circle
20+
xy = pyvips.Image.xyz(image.width, image.height)
21+
xy -= [image.width / 2.0, image.height / 2.0]
22+
scale = min(image.width, image.height) / float(image.width)
23+
xy *= 2.0 / scale
24+
25+
index = xy.polar()
26+
27+
# scale vertical axis to 360 degrees
28+
index *= [1, image.height / 360.0]
29+
30+
return image.mapim(index)
31+
32+
def to_rectangular(image):
33+
"""Transform image coordinates to rectangular.
34+
35+
The image is transformed so that it is unwrapped from a point in the
36+
centre. Circles or segments of circles become vertical straight lines,
37+
radial lines become horizontal lines.
38+
"""
39+
# xy image, vertical scaled to 360 degrees
40+
xy = pyvips.Image.xyz(image.width, image.height)
41+
xy *= [1, 360.0 / image.height]
42+
43+
index = xy.rect()
44+
45+
# scale to image rect
46+
scale = min(image.width, image.height) / float(image.width)
47+
index *= scale / 2.0
48+
index += [image.width / 2.0, image.height / 2.0]
49+
50+
return image.mapim(index)
51+
52+
a = pyvips.Image.new_from_file(sys.argv[1])
53+
a = to_polar(a)
54+
a = to_rectangular(a)
55+
a.write_to_file(sys.argv[2])
56+
57+

examples/read_profile.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/python
2+
3+
import sys
4+
5+
#import logging
6+
#logging.basicConfig(level = logging.DEBUG)
7+
8+
import pyvips
9+
10+
#pyvips.cache_set_trace(True)
11+
12+
a = pyvips.Image.new_from_file(sys.argv[1])
13+
14+
profile = a.get_value("icc-profile-data")
15+
16+
with open('x.icm', 'w') as f:
17+
f.write(profile)
18+
19+

examples/try10.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/python
2+
3+
import sys
4+
5+
import logging
6+
#logging.basicConfig(level = logging.DEBUG)
7+
8+
import pyvips
9+
10+
a = pyvips.Image.black(100, 100)
11+
12+
a = a.draw_circle(128, 50, 50, 20)
13+
14+
b = a.hough_circle(scale = 1, min_radius = 15, max_radius = 25)
15+
16+
b.write_to_file("x.v")

examples/try11.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/python
2+
3+
import sys
4+
5+
#import logging
6+
#logging.basicConfig(level = logging.DEBUG)
7+
8+
import pyvips
9+
10+
a = pyvips.Image.new_from_file(sys.argv[1])
11+
12+
ipct = a.get("ipct-data")
13+
14+
print "ipct = ", ipct.get()
15+
16+
a.remove("ipct-data")
17+
18+
a.write_to_file("x.jpg")

examples/try12.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/python
2+
3+
import sys
4+
5+
import pyvips
6+
7+
im = pyvips.Image.new_from_file(sys.argv[1], access = pyvips.Access.SEQUENTIAL)
8+
9+
footer = pyvips.Image.black(im.width, 150)
10+
left_text = pyvips.Image.text("left corner", dpi = 300)
11+
right_text = pyvips.Image.text("right corner", dpi = 300)
12+
footer = footer.insert(left_text, 50, 50)
13+
footer = footer.insert(right_text, im.width - right_text.width - 50, 50)
14+
footer = footer.ifthenelse(0, [255, 0, 0], blend = True)
15+
16+
im = im.insert(footer, 0, im.height, expand = True)
17+
18+
im.write_to_file(sys.argv[2])

examples/try13.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/python
2+
3+
import sys
4+
5+
import logging
6+
logging.basicConfig(level = logging.DEBUG)
7+
8+
import pyvips

examples/try14.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/python
2+
3+
import sys
4+
5+
import logging
6+
#logging.basicConfig(level = logging.DEBUG)
7+
8+
import pyvips
9+
10+
a = pyvips.Image.black(100, 100)
11+
b = a.bandjoin(2)
12+
13+
b.write_to_file("x.v")
14+
15+
txt = pyvips.Image.text("left corner", dpi = 300)
16+
17+
c = txt.ifthenelse(2, [0, 255, 0], blend = True)
18+
19+
c.write_to_file("x2.v")

examples/try16.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/python3
2+
3+
import sys
4+
5+
import logging
6+
logging.basicConfig(level = logging.DEBUG)
7+
8+
import pyvips
9+
10+
# pyvips.cache_set_trace(True)
11+
12+
a = pyvips.Image.new_from_file(sys.argv[1])
13+
14+
x = a.erode([[128, 255, 128],
15+
[255, 255, 255],
16+
[128, 255, 128]])
17+
18+
x.write_to_file(sys.argv[2])
19+
20+

examples/try17.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/python3
2+
3+
import sys
4+
5+
import logging
6+
#logging.basicConfig(level = logging.DEBUG)
7+
8+
import pyvips
9+
10+
# pyvips.cache_set_trace(True)
11+
12+
a = pyvips.Image.new_from_file(sys.argv[1])
13+
14+
a = a[1:]
15+
16+
a.write_to_file(sys.argv[2])
17+
18+

examples/try2.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/python
2+
3+
import logging
4+
logging.basicConfig(level = logging.DEBUG)
5+
6+
import pyvips
7+
8+
pyvips.cache_set_trace(True)
9+
10+
try:
11+
a = pyvips.Image.new_from_file("/home/john/pics/babe.poop")
12+
except pyvips.Error, e:
13+
print e
14+
15+
a = pyvips.Image.new_from_file("/home/john/pics/babe.jpg")
16+
b = pyvips.Image.new_from_file("/home/john/pics/k2.jpg")
17+
18+
print 'a =', a
19+
print 'b =', b
20+
21+
out = pyvips.call("add", a, b)
22+
23+
print 'out =', out
24+
25+
out = a.add(b)
26+
27+
print 'out =', out
28+
29+
out = out.linear([1, 1, 1], [2, 2, 2])
30+
31+
out.write_to_file("x.v")

examples/try3.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/python
2+
3+
import sys
4+
5+
#import logging
6+
#logging.basicConfig(level = logging.DEBUG)
7+
8+
import pyvips
9+
10+
pyvips.cache_set_trace(True)
11+
12+
a = pyvips.Image.new_from_file(sys.argv[1])
13+
print a.max()
14+
print a.max()
15+

examples/try4.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/python
2+
3+
import sys
4+
5+
#import logging
6+
#logging.basicConfig(level = logging.DEBUG)
7+
8+
import pyvips
9+
10+
a = pyvips.Image.new_from_file(sys.argv[1])
11+
12+
b = pyvips.Image.new_from_file(sys.argv[2])
13+
14+
c = a.join(b, pyvips.Direction.HORIZONTAL,
15+
expand = True,
16+
shim = 100,
17+
align = pyvips.Align.CENTRE,
18+
background = [128, 255, 128])
19+
20+
c.write_to_file(sys.argv[3])

examples/try5.py

Lines changed: 102 additions & 0 deletions
48DA
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/python
2+
3+
import sys
4+
5+
#import logging
6+
#logging.basicConfig(level = logging.DEBUG)
7+
8+
import pyvips
9+
10+
a = pyvips.Image.new_from_file(sys.argv[1])
11+
12+
def should_equal(test, a, b):
13+
if abs(a - b) > 0.01:
14+
print '%s: seen %g and %g' % (test, a, b)
15+
sys.exit(1)
16+
17+
def bandsplit(a):
18+
return [a.extract_band(i) for i in range(0, a.bands)]
19+
20+
# test operator overloads
21+
22+
# addition
23+
b = a + 12
24+
should_equal('add constant', a.avg() + 12, b.avg())
25+
26+
b = a + [12, 0, 0]
27+
x = map (lambda x: x.avg()) bandsplit(a)
28+
y = map (lambda x: x.avg()) bandsplit(b)
29+
x[0] += 12
30+
should_equal('add multiband constant', sum(x), sum(y))
31+
32+
33+
b = a + [12, 0, 0]
34+
b = a + b
35+
b = 12 + a
36+
b = [12, 0, 0] + a
37+
38+
b = a - 12
39+
b = a - [12, 0, 0]
40+
b = a - b
41+
b = 12 - a
42+
b = [12, 0, 0] - a
43+
44+
b = a * 12
45+
b = a * [12, 1, 1]
46+
b = a * b
47+
b = 12 * a
48+
b = [12, 1, 1] * a
49+
50+
b = a / 12
51+
b = a / [12, 1, 1]
52+
b = 12 / a
53+
b = [12, 1, 1] / a
54+
b = a / b
55+
56+
b = a // 12
57+
b = a // [12, 1, 1]
58+
b = 12 // a
59+
b = [12, 1, 1] // a
60+
b = a // b
61+
62+
b = a % 12
63+
b = a % [12, 1, 1]
64+
b = a % b
65+
66+
b = a ** 12
67+
b = a ** [12, 1, 1]
68+
b = 12 ** a
69+
b = [12, 1, 1] ** a
70+
b = a ** b
71+
72+
b = a << 12
73+
b = a << [12, 1, 1]
74+
b = a << b
75+
76+
b = a >> 12
77+
b = a >> [12, 1, 1]
78+
b = a >> b
79+
80+
b = a & 12
81+
b = a & [12, 1, 1]
82+
b = 12 & a
83+
b = [12, 1, 1] & a
84+
b = a & b
85+
86+
b = a | 12
87+
b = a | [12, 1, 1]
88+
b = 12 | a
89+
b = [12, 1, 1] | a
90+
b = a | b
91+
92+
b = a ^ 12
93+
b = a ^ [12, 1, 1]
94+
b = 12 ^ a
95+
b = [12, 1, 1] ^ a
96+
b = a ^ b
97+
98+
b = -a
99+
b = +a
100+
b = abs(a)
101+
b = ~a
102+

0 commit comments

Comments
 (0)
0