use*_*179 5 python translation rotation scale python-imaging-library
我目前正在开发一个项目,尝试使用 Python 成像库创建希尔伯特曲线。我创建了一个函数,它将通过每次迭代生成曲线的新坐标,并将它们放入各种列表中,然后我希望能够移动、旋转和缩放。我想知道是否有人可以给我一些提示或方法来做到这一点,因为我完全无能为力。仍在处理大量代码。
#! usr/bin/python
import Image, ImageDraw
import math
# Set the starting shape
img = Image.new('RGB', (1000, 1000))
draw = ImageDraw.Draw(img)
curve_X = [0, 0, 1, 1]
curve_Y = [0, 1, 1, 0]
combinedCurve = zip(curve_X, curve_Y)
draw.line((combinedCurve), fill=(220, 255, 250))
iterations = 5
# Start the loop
for i in range(0, iterations):
# Make 4 copies of the curve
copy1_X = list(curve_X)
copy1_Y = list(curve_Y)
copy2_X = list(curve_X)
copy2_Y = list(curve_Y)
copy3_X = list(curve_X)
copy3_Y = list(curve_Y)
copy4_X = list(curve_X)
copy4_Y = list(curve_Y)
# For copy 1, rotate it by 90 degree clockwise
# Then move it to the bottom left
# For copy 2, move it to the top left
# For copy 3, move it to the top right
# For copy 4, rotate it by 90 degrees anticlockwise
# Then move it to the bottom right
# Finally, combine all the copies into a big list
combinedCurve_X = copy1_X + copy2_X + copy3_X + copy4_X
combinedCurve_Y = copy1_Y + copy2_Y + copy3_Y + copy4_Y
# Make the initial curve equal to the combined one
curve_X = combinedCurve_X[:]
curve_Y = combinedCurve_Y[:]
# Repeat the loop
# Scale it to fit the canvas
curve_X = [x * xSize for x in curve_X]
curve_Y = [y * ySize for y in curve_Y]
# Draw it with something that connects the dots
curveCoordinates = zip(curve_X, curve_Y)
draw.line((curveCoordinates), fill=(255, 255, 255))
img2=img.rotate(180)
img2.show()
Run Code Online (Sandbox Code Playgroud)
这是一个处理矩阵的解决方案(这对于这种类型的计算是有意义的,最终,2D 坐标是具有 1 列的矩阵!),
缩放非常简单,只需将矩阵的每个元素乘以缩放因子即可:
scaled = copy.deepcopy(original)
for i in range(len(scaled[0])):
scaled[0][i]=scaled[0][i]*scaleFactor
scaled[1][i]=scaled[1][i]*scaleFactor
Run Code Online (Sandbox Code Playgroud)
移动非常容易,你所要做的就是将偏移量添加到矩阵的每个元素,这里有一个使用矩阵乘法的方法:
import numpy as np
# Matrix multiplication
def mult(matrix1,matrix2):
# Matrix multiplication
if len(matrix1[0]) != len(matrix2):
# Check matrix dimensions
print 'Matrices must be m*n and n*p to multiply!'
else:
# Multiply if correct dimensions
new_matrix = np.zeros(len(matrix1),len(matrix2[0]))
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
new_matrix[i][j] += matrix1[i][k]*matrix2[k][j]
return new_matrix
Run Code Online (Sandbox Code Playgroud)
然后创建你的翻译矩阵
import numpy as np
TranMatrix = np.zeros((3,3))
TranMatrix[0][0]=1
TranMatrix[0][2]=Tx
TranMatrix[1][1]=1
TranMatrix[1][2]=Ty
TranMatrix[2][2]=1
translated=mult(TranMatrix, original)
Run Code Online (Sandbox Code Playgroud)
最后,旋转有点棘手(你知道你的旋转角度吗?):
import numpy as np
RotMatrix = np.zeros((3,3))
RotMatrix[0][0]=cos(Theta)
RotMatrix[0][1]=-1*sin(Theta)
RotMatrix[1][0]=sin(Theta)
RotMatrix[1][1]=cos(Theta)
RotMatrix[2][2]=1
rotated=mult(RotMatrix, original)
Run Code Online (Sandbox Code Playgroud)
关于我所做的一些进一步阅读:
所以基本上,如果您在代码中插入这些操作,将向量乘以旋转/平移矩阵,它应该可以工作
编辑
我刚刚发现这个 Python 库似乎提供了所有类型的转换:http://toblerity.org/shapely/index.html
| 归档时间: |
|
| 查看次数: |
19715 次 |
| 最近记录: |