给定字符串垂直翻转(反转)图像?

发布于 2024-12-16 15:05:28 字数 132 浏览 2 评论 0原文

所以我有一串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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执手闯天涯 2024-12-23 15:05:28

彻底做你想做的事,这是一种继续进行的方法:

>>> img = 'ABCDEFGHIJKL'
>>> x, y = 4, 3
>>> def chunks(l, n):
...     for i in xrange(0, len(l), n):
...         yield l[i:i+n]
... 
>>> [row for row in chunks(img, x)]
['ABCD', 'EFGH', 'IJKL']
>>> ''.join(reversed([row for row in chunks(img, x)]))
'IJKLEFGHABCD'

但是,除非你有非常小的图像,否则你最好通过numpy,因为这至少比 Cpython 快一个数量级数据类型。您应该查看 flipup 函数。示例:

>>> A
array([[ 1.,  0.,  0.],
       [ 0.,  2.,  0.],
       [ 0.,  0.,  3.]])
>>> np.flipud(A)
array([[ 0.,  0.,  3.],
       [ 0.,  2.,  0.],
       [ 1.,  0.,  0.]])

编辑:考虑添加一个完整的示例,以防您以前从未使用过 NumPy。当然,只有对于非 2x2 的图像,转换才有价值,因为实例化数组会产生开销......

>>> import numpy as np
>>> img = [0x00, 0x01, 0x02, 0x03]
>>> img
[0, 1, 2, 3]
>>> x = y = 2
>>> aimg = np.array(img).reshape(x, y)
>>> aimg
array([[0, 1],
       [2, 3]])
>>> np.flipud(aimg)
array([[2, 3],
       [0, 1]])

To do what you want to the letter this is one way to proceed:

>>> img = 'ABCDEFGHIJKL'
>>> x, y = 4, 3
>>> def chunks(l, n):
...     for i in xrange(0, len(l), n):
...         yield l[i:i+n]
... 
>>> [row for row in chunks(img, x)]
['ABCD', 'EFGH', 'IJKL']
>>> ''.join(reversed([row for row in chunks(img, x)]))
'IJKLEFGHABCD'

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:

>>> A
array([[ 1.,  0.,  0.],
       [ 0.,  2.,  0.],
       [ 0.,  0.,  3.]])
>>> np.flipud(A)
array([[ 0.,  0.,  3.],
       [ 0.,  2.,  0.],
       [ 1.,  0.,  0.]])

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....

>>> import numpy as np
>>> img = [0x00, 0x01, 0x02, 0x03]
>>> img
[0, 1, 2, 3]
>>> x = y = 2
>>> aimg = np.array(img).reshape(x, y)
>>> aimg
array([[0, 1],
       [2, 3]])
>>> np.flipud(aimg)
array([[2, 3],
       [0, 1]])
屋檐 2024-12-23 15:05:28

假设您的图像位于数组 img 中,然后执行

img.reverse();
#also need to flip each row
for row in img:
  row.reverse();

say you have the image in array img, then do

img.reverse();
#also need to flip each row
for row in img:
  row.reverse();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文