| 225 | |
| 226 | |
| 227 | def reverse_quad_transform(image, quad_to_map_to, alpha): |
| 228 | # forward mapping, for simplicity |
| 229 | |
| 230 | result = Image.new("RGBA",image.size) |
| 231 | result_pixels = result.load() |
| 232 | |
| 233 | width, height = result.size |
| 234 | |
| 235 | for y in range(height): |
| 236 | for x in range(width): |
| 237 | result_pixels[x,y] = (0,0,0,0) |
| 238 | |
| 239 | p1 = (quad_to_map_to[0],quad_to_map_to[1]) |
| 240 | p2 = (quad_to_map_to[2],quad_to_map_to[3]) |
| 241 | p3 = (quad_to_map_to[4],quad_to_map_to[5]) |
| 242 | p4 = (quad_to_map_to[6],quad_to_map_to[7]) |
| 243 | |
| 244 | p1_p2_vec = (p2[0] - p1[0],p2[1] - p1[1]) |
| 245 | p4_p3_vec = (p3[0] - p4[0],p3[1] - p4[1]) |
| 246 | |
| 247 | for y in range(height): |
| 248 | for x in range(width): |
| 249 | pixel = image.getpixel((x,y)) |
| 250 | |
| 251 | y_percentage = y / float(height) |
| 252 | x_percentage = x / float(width) |
| 253 | |
| 254 | # interpolate vertically |
| 255 | pa = (p1[0] + p1_p2_vec[0] * y_percentage, p1[1] + p1_p2_vec[1] * y_percentage) |
| 256 | pb = (p4[0] + p4_p3_vec[0] * y_percentage, p4[1] + p4_p3_vec[1] * y_percentage) |
| 257 | |
| 258 | pa_to_pb_vec = (pb[0] - pa[0],pb[1] - pa[1]) |
| 259 | |
| 260 | # interpolate horizontally |
| 261 | p = (pa[0] + pa_to_pb_vec[0] * x_percentage, pa[1] + pa_to_pb_vec[1] * x_percentage) |
| 262 | |
| 263 | try: |
| 264 | result_pixels[p[0],p[1]] = (pixel[0],pixel[1],pixel[2],min(int(alpha * 255),pixel[3])) |
| 265 | except Exception: |
| 266 | pass |
| 267 | |
| 268 | return result |