RGB Int到RGB-Python
如何将RGB整数转换为相应的RGB元组RGB Int到RGB-Python,python,integer,rgb,Python,Integer,Rgb,如何将RGB整数转换为相应的RGB元组(R,G,B)?看起来很简单,但我在谷歌上找不到任何东西 我知道,对于每个RGB(r,g,b)都有整数n=r256^2+g256+b,我如何在Python中解决相反的问题,即给定一个n,我需要r,g,b值 Blue = RGBint mod 256 Green = RGBint / 256 mod 256 Red = RGBint / 256 / 256 mod 256 一旦您知道如何获得它,就可以非常简单地实现它。:) Upd:添加了python函
(R,G,B)
?看起来很简单,但我在谷歌上找不到任何东西
我知道,对于每个RGB(r,g,b)
都有整数n=r256^2+g256+b
,我如何在Python中解决相反的问题,即给定一个n
,我需要r
,g
,b
值
Blue = RGBint mod 256
Green = RGBint / 256 mod 256
Red = RGBint / 256 / 256 mod 256
一旦您知道如何获得它,就可以非常简单地实现它。:)
Upd:添加了python函数。不确定是否有更好的方法,但这在Python3和2.4上有效
def rgb_int2tuple(rgbint):
return (rgbint // 256 // 256 % 256, rgbint // 256 % 256, rgbint % 256)
还有一种使用位移位和掩蔽的优秀解决方案,无疑比Nils Pipenbrick发布的更快。可能有一种更短的方法:
dec=10490586
hex="%06x" % dec
r=hex[:2]
g=hex[2:4]
b=hex[4:6]
rgb=(r,g,b)
编辑:这是错误的-给出十六进制的答案,OP int。
EDIT2:经过改进以减少痛苦和失败-需要“%06x”来确保十六进制始终显示为六位数[感谢Peter Hansen的评论]。我当然不是Python专家,但据我所知,它与C具有相同的运算符 如果是这样的话,这应该是可行的,而且应该比使用模和除法快得多
Blue = RGBint & 255
Green = (RGBint >> 8) & 255
Red = (RGBint >> 16) & 255
它所做的是屏蔽每种情况下的最低字节(二进制和255..等于8个一位)。对于绿色和红色分量,它的作用相同,但首先将颜色通道移到最低字节。我假设您有一个包含RGB值的32位整数(例如ARGB)。然后,您可以使用
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)
如果只接受divmod(值,(divider1,divider2,divider3…)
建议,它也会简化各种时间转换
>>> import struct
>>> str='aabbcc'
>>> struct.unpack('BBB',str.decode('hex'))
(170, 187, 204)
for python3:
>>> struct.unpack('BBB', bytes.fromhex(str))
及
这将生成以下整数:
>>> 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]
对于任何使用谷歌Appengine图像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))
如果您使用的是NumPy,并且您有一个RGBINT数组,您还可以更改其数据类型以提取红色、绿色、蓝色和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')
增加了上面提到的内容。简洁的一行选择
# 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)
你说的RGB整数是什么意思?就是我刚才在问题中编辑的公式
(255,255,255)
=16777215
注意:int 255是“红色”:“#ff0000”,不要忘记使用整数除法而不是浮点除法。尽管它使用整数除法和模,但我认为我的答案不值得降级。不,它不值得降级表决。人们还没有阅读向下投票箭头工具提示,并相信向下投票意味着“我不喜欢这个”。注意:int 255是“红色”:“#ff0000”,如果十六进制字符串没有预期的六位数,这也会失败。使用“%06x”可避免出现这种情况。与其他解决方案+1相比,速度会慢得惊人(2个函数调用),但是与使用%和//的解决方案相比,速度优势应该是最小的。当然比使用函数调用的解决方案(divmod、struct.[un]pack等)要快得多。
>>> 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')
# 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)