Python Forum

Full Version: Attribute Error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
trying to convert an integer tuple to a binary (string) tuple.
getting an Attribute error as follow.
def __int_to_bin(rgb):
        r, g, b = rgb.split(' ')
        return ('{0:08b}'.format(r,),
                '{0:08b}'.format(g,),
                '{0:08b}'.format(b,))
this is the error
Error:
File "C:/Users/adit1/AppData/Local/Programs/Python/Python37-32/Stego/aug17.py", line 12, in __int_to_bin r, g, b = rgb.split(' ') AttributeError: 'tuple' object has no attribute 'split'
As you say, it's a tuple. Tuples don't have a split method, that's a string method. I would use a generator expression with a tuple:

def _int_to_rgb(rgb):
    return tuple('{0:08b}'.format(x) for x in rgb)
Note that the trailing comma syntax, like (r,), is only needed when defining one item tuples (to clarify them as tuples, rather than using parentheses to force the evaluation of an expression). That syntax is not needed elsewhere, certainly not in function or method calls.
maybe

r, g, b = [int(i) for i in rgb]

ichabod801 was a second faster.
Thank you, attribute error is solved but i am facing another one in tuples at later stage. a vslue error
merging two rgb tuples, both params are string tuples.
def __merge_rgb(rgb1, rgb2):
        r1, g1, b1 = rgb1
        r2, g2, b2 = rgb2
        rgb = (r1[:4] + r2[:4],
               g1[:4] + g2[:4],
               b1[:4] + b2[:4])
        return rgb
and here is the error
Error:
File "C:\Users\adit1\AppData\Local\Programs\Python\Python37-32\Stego\aug17.py", line 29, in __merge_rgb r1, g1, b1 = rgb1 ValueError: too many values to unpack (expected 3)
Also, I can help you with my whole code if required. It performs image steganography.
r1, g1, b1, *_ = rgb1
Wavic's suggestion will get past the problem. However, the problem is that one of your rgb1 tuples has four or more items. This could be caused by an earlier error in your program or by data not matching your assumptions. For example, it could be CMYK color data, which uses four values. If you try to interpret the first three values as RGB that's going to lead to problems.

I would really suggest tracking down where the unexpectedly long rgb value is coming from. You could put this check at different points in the program flow:

if len(rgb) > 3:
    print(rgb)
To detect and show the longer values.