25

如何将 RGB 整数转换为相应的 RGB 元组(R,G,B)?看起来很简单,但我在谷歌上找不到任何东西。

我知道对于每个 RGB(r,g,b)你都有 integer n = r256^2 + g256 + b,我如何在 Python 中解决相反的问题,IE 给定一个n,我需要r, g,b值。

4

11 回答 11

55

我绝对不是 Python 专家,但据我所知,它具有与 C 相同的运算符。

如果是这样,这应该可以工作,而且它也应该比使用模数和除法快得多。

Blue =  RGBint & 255
Green = (RGBint >> 8) & 255
Red =   (RGBint >> 16) & 255

它在每种情况下屏蔽最低字节的作用(二进制和 255 .. 等于 8 个 1 位)。对于绿色和红色分量,它的作用相同,但首先将颜色通道转移到最低字节。

于 2010-02-14T18:06:30.943 回答
16

从 RGB 整数:

Blue =  RGBint mod 256
Green = RGBint / 256 mod 256
Red =   RGBint / 256 / 256 mod 256

一旦你知道如何获得它,这可以非常简单地实现。:)

更新:添加了 python 功能。不确定是否有更好的方法,但这适用于 Python 3 和 2.4

def rgb_int2tuple(rgbint):
    return (rgbint // 256 // 256 % 256, rgbint // 256 % 256, rgbint % 256)

Nils Pipenbrinck 发布的还有一个使用位移和掩码的出色解决方案,其速度无疑要快得多。

于 2010-02-14T17:59:08.653 回答
9
>>> import struct
>>> str='aabbcc'
>>> struct.unpack('BBB',str.decode('hex'))
(170, 187, 204)

for python3:
>>> struct.unpack('BBB', bytes.fromhex(str))

>>> rgb = (50,100,150)
>>> struct.pack('BBB',*rgb).encode('hex')
'326496'

for python3:
>>> bytes.hex(struct.pack('BBB',*rgb))
于 2010-11-28T10:05:09.770 回答
8
>>> r, g, b = (111, 121, 131)
>>> packed = int('%02x%02x%02x' % (r, g, b), 16)

这将产生以下整数:

>>> packed
7305603

然后,您可以使用长显式方式解压它:

>>> packed % 256
255
>>> (packed / 256) % 256
131
>>> (packed / 256 / 256) % 256
121
>>> (packed / 256 / 256 / 256) % 256
111

..或更紧凑的方式:

>>> b, g, r = [(packed >> (8*i)) & 255 for i in range(3)]
>>> r, g, b

示例适用于任意数量的数字,例如 RGBA 颜色:

>>> packed = int('%02x%02x%02x%02x' % (111, 121, 131, 141), 16)
>>> [(packed >> (8*i)) & 255 for i in range(4)]
[141, 131, 121, 111]
于 2011-12-01T11:53:59.523 回答
6

我假设您有一个包含 RGB 值(例如 ARGB)的 32 位整数。struct然后你可以使用模块解压二进制数据:

# Create an example value (this represents your 32-bit input integer in this example).
# The following line results in exampleRgbValue = binary 0x00FF77F0 (big endian)
exampleRgbValue = struct.pack(">I", 0x00FF77F0)

# Unpack the value (result is: a = 0, r = 255, g = 119, b = 240)
a, r, g, b = struct.unpack("BBBB", exampleRgbValue)
于 2010-02-14T18:14:45.400 回答
3

只是给使用 Google 的 Appengine Images Python API 的任何人的注意事项。我发现我不得不提供一个具有 32 位 RGB 颜色值的方法。

具体来说,如果您使用 API 转换 PNG(或任何具有透明像素的图像),则需要为 execute_transforms 方法提供一个名为 transparent_substitution_rgb 的参数,该参数必须是 32 位 RGB 颜色值。

借用 dbr 的回答,我想出了一个类似的方法:

def RGBTo32bitInt(r, g, b):
  return int('%02x%02x%02x' % (r, g, b), 16)

transformed_image = image.execute_transforms(output_encoding=images.JPEG, transparent_substitution_rgb=RGBTo32bitInt(255, 127, 0))
于 2013-09-03T23:26:26.997 回答
2
def unpack2rgb(intcol):
    tmp, blue= divmod(intcol, 256)
    tmp, green= divmod(tmp, 256)
    alpha, red= divmod(tmp, 256)
    return alpha, red, green, blue

如果只divmod(value, (divider1, divider2, divider3…))接受这个建议,它也会简化各种时间转换。

于 2010-03-03T18:08:05.377 回答
0

这样做可能有一种更短的方法:

dec=10490586
hex="%06x" % dec
r=hex[:2]
g=hex[2:4]
b=hex[4:6]
rgb=(r,g,b)

编辑:这是错误的 - 以十六进制给出答案,OP 想要 int。EDIT2:改进以减少痛苦和失败 - 需要 '%06x' 以确保十六进制始终显示为六位数 [感谢彼得汉森的评论]。

于 2010-02-14T18:05:32.973 回答
0

如果你使用 NumPy 并且你有一个 RGBints 数组,你也可以改变它的 dtype 来提取红色、绿色、蓝色和 alpha 分量:

>>> type(rgbints)
numpy.ndarray
>>> rgbints.shape
(1024L, 768L)
>>> rgbints.dtype
dtype('int32')
>>> rgbints.dtype = dtype('4uint8')
>>> rgbints.shape
(1024L, 768L, 4L)
>>> rgbints.dtype
dtype('uint8')
于 2015-07-28T16:19:33.587 回答
0
def int2color(red,green,blue):
    def chex(c):
        if c < 16:
            return "0"+hex(c)[2:]
        else:
            return hex(c)[2:]

    return "#{}{}{}".format(chex(red),chex(green),chex(blue))

>>> int2color(49, 105, 0)
#316900
于 2022-01-09T11:44:12.513 回答
-1

添加到上面提到的内容。简洁的单行替代方案。

# 2003199 or #E190FF is Dodger Blue.
tuple((2003199 >> Val) & 255 for Val in (16, 8, 0))
# (30, 144, 255)

并避免将来出现任何混乱。

from collections import namedtuple
RGB = namedtuple('RGB', ('Red', 'Green', 'Blue'))
rgb_integer = 16766720  # Or #ffd700 is Gold.
# The unpacking asterisk prevents a TypeError caused by missing arguments.
RGB(*((rgb_integer >> Val) & 255 for Val in (16, 8, 0)))
# RGB(Red=255, Green=215, Blue=0)
于 2015-10-09T14:11:05.890 回答