为什么从一个数组转换为图像时,有位转移? [英] Why is there bit shifting when converting to an image from an array?

查看:268
本文介绍了为什么从一个数组转换为图像时,有位转移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个从numpy的数组的QPixmap。该numpy的阵列图像将是2D(即无颜色信息只是灰度)。

I'm trying to create a QPixmap from a numpy array. The numpy array image is going to be 2D (ie no color info just grayscale).

我在努力适应这个回答以我的需要,但是我不太懂这行:

I'm trying to adapt this answer to my needs however I don't quite understand this line:

b = (255 << 24 | a[:,:,0] << 16 | a[:,:,1] << 8 | a[:,:,2]).flatten() # pack RGB values

有一些bitshifting回事,一些位ING,但我不太明白是诚实的。

There is some bitshifting going on and some bitwise or' ing but I don't quite get it to be honest.

所以,我的简单化示例如下:

So my dumbed down example is as follows:

x, y = np.meshgrid(np.arange(1920), np.arange(1080), indexing='ij'); z = np.sin(0.03*x)*np.cos(0.005*y)
imgPNFN = z
if imgPNFN.ndim == 2:
    imgPNFN = imgPNFN[:,:,np.newaxis].repeat(3, 2)
imMin = np.min(imgPNFN)
imDelta = np.max(imgPNFN) - np.min(imgPNFN)
im1 = ((imgPNFN-imMin)/imDelta*255).astype(np.uint32)+1 #<-- had to add 1 for some reason otherwise got weird results...
im2 = 255 << 24 | im1[:,:,0] << 16 | im1[:,:,1] << 8 | im1[:,:,2]
im3 = QtGui.QImage(im2, im2.shape[1], im2.shape[0], QtGui.QImage.Format_RGB32)

这似乎工作,但是当我的形象是莉娜即:

which seems to work but when my image is lena ie:

from scipy.misc import lena
l = sp.misc.lena()
imgPNFN = l
#etc...

它不工作...我想这是因为我缺乏的是什么bitshifting正在做的理解......另外,如果有更好的方式做RGB(即我到复制的东西pretend我有相同的值),如果我能够避免,不知怎的,那将是巨大的。

It doesn't work... I think it's because of my lack of understanding of what the bitshifting is doing... Also, if there is a better way to do the RGB (ie I'm copying things over to pretend I have the same values) if I could avoid that somehow that would be great.

在此先感谢!

推荐答案

我假设你想知道为什么位转换和 -ing正在发生的事情。那么,你需要加入 A [lpha] 研究[编者按] B [攻略] 字节在一起,形成一个整数值。像素数据是整数的2D阵列;标量值。他们不是字节的元组。

I assume that you want to know why the bit-shifting and or-ing is happening. Well, you need to join the A[lpha], R[ed], G[reen], and B[lue] bytes together to form a single integer value. Pixel data is a 2D array of integer; scalar value. They are not tuples of bytes.

颜色: 长春花

A: 255 -> 11111111
R: 204 -> 11001100
G: 204 -> 11001100
B: 255 -> 11111111

公式

Value = A << 24 | R << 16 | G << 8 | B

位数学

  11111111000000000000000000000000
          110011000000000000000000
                  1100110000000000
 +                        11111111
 ---------------------------------
  11111111110011001100110011111111

基础转换

11111111110011001100110011111111 <分> 2 = 4291611903 <分> 10

Base conversion

111111111100110011001100111111112 = 429161190310

,具有4291611903的像素值。

Based on the bit manipulation above, the color Periwinkle, with 100% opacity in ARGB, has a pixel value of 4,291,611,903.

这篇关于为什么从一个数组转换为图像时,有位转移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆