给定字符串垂直翻转(反转)图像?
所以我有一串RGBA图像数据,每个像素都是一个字节长。我也知道图像的 x 和 y 分辨率。现在我想以一种导致图像垂直翻转或反转的方式编辑字符串,这意味着像素的第一“行”成为最后一行,反之亦然,对于所有其他“行”也是如此。有没有快速的方法来做到这一点?
So I have a string of RGBA image data, each pixel is a byte long. I know the image's x and y resolution too. Now I want to edit the string in a way which would cause the image to be flipped or reversed vertically, which means have the first "row" of pixels become the last row and the opposite, and like this for all other "rows". Is there a fast way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要彻底做你想做的事,这是一种继续进行的方法:
但是,除非你有非常小的图像,否则你最好通过numpy,因为这至少比 Cpython 快一个数量级数据类型。您应该查看 flipup 函数。示例:
编辑:考虑添加一个完整的示例,以防您以前从未使用过 NumPy。当然,只有对于非 2x2 的图像,转换才有价值,因为实例化数组会产生开销......
To do what you want to the letter this is one way to proceed:
HOWEVER, unless you have very small images, you would be better off passing through numpy, as this is at the very minimum an order of magnitude faster than Cpython datatypes. You should look at at the flipup function. Example:
EDIT: thought to add a complete example in case you have never worked with NumPy before. Of course the conversion is worth only for images that are not 2x2, as instantiating the array has an overhead....
假设您的图像位于数组 img 中,然后执行
say you have the image in array img, then do