我正在生成带有10位数据的渐变图片,为了压缩目的,我必须将其混合到16位。我使用此代码,但是它非常慢。有什么更好的方法吗?
def gradient_pic(num,f):
pic = np.zeros(2580,dtype= np.uint8)
pixvalue = np.random.randint(0,50,size=1,dtype=np.uint8)[0]
for i in range (0,1544):
for chunk in range(0,258):
pic[chunk*5] = (pixvalue<<6)+((pixvalue+1)>>4)
pic[chunk*5+1] = (((pixvalue+1)&15)<<12) + ((pixvalue+2)<<2)+ ((pixvalue+3)>>8)
pic[chunk*5+2] = (((pixvalue+3)&255)<<8) + ((pixvalue+4)>>2)
pic[chunk*5+3] = (((pixvalue+4)&3)<<14) + ((pixvalue+5)<<4)+ ((pixvalue+6)>>6)
pic[chunk*5+4] = (((pixvalue+6)&63)<<10) + pixvalue+7
pixvalue = (pixvalue+8)%1023
f.write(pic)
print(num)