[英]python rotate image by 90 degrees without using any libs
there are many methods to rotate img by 90 degree using OpenCV or PIL, how to do it by self-design code without using any libs? 有很多使用OpenCV或PIL将img旋转90度的方法,如何通过自行设计的代码而不使用任何库来实现? I have tried in the way of rotating a matrix, but the RGB elements make it fail
我已经尝试过旋转矩阵的方式,但是RGB元素使其失败
Did you try this logic? 您是否尝试过这种逻辑?
Rotating a 2D pixel array by 90 degrees 将2D像素阵列旋转90度
Usually c++ is a good way to form coding without calling any libs 通常,c ++是无需调用任何库即可形成代码的好方法
You can rotate a grid of pixels by 90° by a combination of transposition and reflection. 您可以通过转置和反射的组合将像素网格旋转90°。 You can do this in pure Python by using
zip
to transpose, and reversed
to reflect. 您可以在纯Python中执行此操作,方法是使用
zip
进行转置,然后reversed
进行反映。
Here's a short demo that does a 90° clockwise rotation. 这是一个简短的演示,它可以顺时针旋转90°。
# Some fake pixels
data = [
[(100, 101, 102), (105, 106, 107), (110, 111, 112), (115, 116, 117)],
[(120, 121, 122), (125, 126, 127), (130, 131, 132), (135, 136, 137)],
[(140, 141, 142), (145, 146, 147), (150, 151, 152), (155, 156, 157)],
[(160, 161, 162), (165, 166, 167), (170, 171, 172), (175, 176, 177)],
[(180, 181, 182), (185, 186, 187), (190, 191, 192), (195, 196, 197)],
]
new = [list(reversed(t)) for t in zip(*data)]
for row in new:
print(row)
output 输出
[(180, 181, 182), (160, 161, 162), (140, 141, 142), (120, 121, 122), (100, 101, 102)]
[(185, 186, 187), (165, 166, 167), (145, 146, 147), (125, 126, 127), (105, 106, 107)]
[(190, 191, 192), (170, 171, 172), (150, 151, 152), (130, 131, 132), (110, 111, 112)]
[(195, 196, 197), (175, 176, 177), (155, 156, 157), (135, 136, 137), (115, 116, 117)]
But seriously, it's faster (and simpler) to do this sort of thing with a library, unless the images are really small. 但是,严重的是,使用库执行这种操作更快(更简单),除非图像很小。 If you don't want to use PIL, you can do it with Numpy.
如果您不想使用PIL,则可以使用Numpy。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.