numpy图像阵列:如何有效地从RGB切换到十六进制 [英] Numpy Image Arrays: How to efficiently switch from RGB to Hex
本文介绍了numpy图像阵列:如何有效地从RGB切换到十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在使用嵌套的for循环将RGB图像转换为十六进制值的图像数组,但是对于大图像来说,它太慢了.有谁知道快速方法或库可以帮助我从RGB切换回十六进制?
I have been using nested for loops to turn RGB images into an image array of Hex values, but it is too slow for large images. Does anyone know a quick way and or a library that can help me switch back and from RGB to HEX?
@ragingSloth
edit: @ragingSloth
这是我想出的,但是对于我所需要的来说太慢了:
This is what I came up with but it is too slow for what I need:
def rgb_to_hex(array):
(x, y, z) = array.shape
for v in range(0, x):
for u in range(0, y):
array[v, u] = int('%02x%02x%02x' % (array[v, u, 0], array[v, u, 1], array[v, u, 2]))
推荐答案
使用烧杯的想法,您还可以消除double for循环:
Using beaker's idea, you can also eliminate the double for-loop:
def tohex(array):
array = np.asarray(array, dtype='uint32')
return ((array[:, :, 0]<<16) + (array[:, :, 1]<<8) + array[:, :, 2])
这篇关于numpy图像阵列:如何有效地从RGB切换到十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文