-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathperspective_path.py
executable file
·225 lines (152 loc) · 6.14 KB
/
perspective_path.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
'''
Perspective and Rotation Transform using paths to control operation
$Id: perspective_path.py,v 1.30 2014/02/17 17:16:13 sjg Exp $
(c) Stephen Geary, Feb 2014
2014.02.17 - SJG - Make sure gimp_undo_group_start() is not called until
after all possible returns due to detected errors.
2014.02.12 - SJG - Added rotation by path tool
'''
from gimpfu import *
import sys
import time
import gtk
import math
#----------------------------------------------------------------------------------
def plugin_rotate( image, drawable ):
# get points for transform from image
vectors = pdb.gimp_image_get_active_vectors(image)
nstrokes, strokes = pdb.gimp_vectors_get_strokes(vectors)
if nstrokes == 0:
# must be at least one stroke or we have no points
gimp.message( "No strokes found" )
return
stoke_type, n_points, p, closed = pdb.gimp_vectors_stroke_get_points(vectors, strokes[0])
if n_points != 12:
# Note that each (x,y) ordinate pairs becomes 6 values per ordinate pair
gimp.message( "Only found " + str(n_points/6) + " points, need 2" )
return
# do the transformation
pdb.gimp_context_set_transform_direction( TRANSFORM_FORWARD )
cx = image.width/2
cy = image.height/2
# note that y ordinates are opposite from what you expect
# in maths, as the top is zeroand the bottom is positive
y7 =image.height - 1 - p[7]
y1 =image.height - 1 - p[1]
if p[6] > p[0]:
angle = math.atan2( y7-y1, p[6]-p[0] )
else:
angle = math.atan2( y1-y7, p[0]-p[6] )
r45 = math.pi / 4.0
r90 = math.pi / 2.0
r135 = r45 + r90
if angle > 0 :
if angle > r45:
angle2 = angle - r90
else:
angle2 = angle
else:
absangle = math.fabs( angle )
if absangle > r45:
angle2 = r90 - absangle
else:
angle2 = angle
pdb.gimp_image_undo_group_start(image)
pdb.gimp_item_transform_rotate( drawable, angle2, True, cx, cy )
gimp.displays_flush()
pdb.gimp_image_undo_group_end(image)
#----------------------------------------------------------------------------------
def plugin_perspective( image, drawable ):
# get points for transform from image
vectors = pdb.gimp_image_get_active_vectors(image)
nstrokes, strokes = pdb.gimp_vectors_get_strokes(vectors)
if nstrokes == 0:
# must be at least one stroke or we have no points
gimp.message( "No strokes found" )
return
stoke_type, n_points, p, closed = pdb.gimp_vectors_stroke_get_points(vectors, strokes[0])
if n_points != 24:
# Note that each (x,y) ordinate pairs becomes 6 values per ordinate pair
gimp.message( "Only found " + str(n_points/6) + " points, need 4" )
return
# if force flag is True then we map the points to the top and bottom of
# the time by projecting lines from the points we have. This will produce
# a perspective correction suitable for focing building verticals to be
# vertical in the final image ( assuming you choose points on the
# correct lines in the image ).
# map the points to the top and bottom of the image
w = drawable.width
h = drawable.height
a = ( p[7] - p[1] ) / ( p[6] - p[0] )
b = p[7] - ( a * p[6] )
c = ( p[19] - p[13] ) / ( p[18] - p[12] )
d = p[19] - ( c * p[18] )
# now work out the intersection of the two lines
x0 = ( d - b ) / ( a - c )
y0 = ( a * x0 ) + b
# if y0 < 0 then the point is above the image otherwise it is below
#
# we use this to work out the bounding box we need
q = []
if y0 < 0:
# intersection above image
# use bounding box based on bottom corners of image
q.append( -( ( h-1 )*x0 ) / (y0+1-h) )
q.append( 0 )
q.append( ( h*x0 - x0 - w*y0 + y0 ) / ( h - 1 - y0 ) )
q.append( 0 )
q.append( 0 )
q.append( h-1 )
q.append( w-1 )
q.append( h-1 )
else:
# intersection below image
# use bounding box based on top corners of image
q.append( 0 )
q.append( 0 )
q.append( w-1 )
q.append( 0 )
q.append( ( x0*(h-1) ) / y0 )
q.append( h-1 )
q.append( ( y0*w - y0 + x0*h - x0 - (h-1)*(w-1) ) / y0 )
q.append( h-1 )
# need to work out the box to map to
# we do this by using the middle two x and y ordinates of the
# given points. We map to this.
# do the transformation
pdb.gimp_image_undo_group_start(image)
pdb.gimp_context_set_transform_direction( TRANSFORM_BACKWARD )
pdb.gimp_item_transform_perspective( drawable, q[0], q[1], q[2], q[3], q[4], q[5], q[6], q[7] )
gimp.displays_flush()
pdb.gimp_image_undo_group_end(image)
#----------------------------------------------------------------------------------
register(
"python_fu_sjg_perspective",
"Perspective transform using path from image.",
"Perspective transform using path from image.",
"Stephen Geary, ( sg euroapps com )",
"(c) 2014, Stephen Geary",
"2014",
"<Image>/Tools/Transform Tools/SJG Perspective from path",
"*",
[
],
[],
plugin_perspective,
)
register(
"python_fu_sjg_rotate",
"Rotate tp horizonal or vertical using path from image.",
"Rotate tp horizonal or vertical using path from image.",
"Stephen Geary, ( sg euroapps com )",
"(c) 2014, Stephen Geary",
"2014",
"<Image>/Tools/Transform Tools/SJG Rotation from path",
"*",
[
],
[],
plugin_rotate,
)
main()
#----------------------------------------------------------------------------------