![]() |
Attribute Error - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Attribute Error (/thread-20550.html) |
Attribute Error - Adit99 - Aug-17-2019 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
RE: Attribute Error - ichabod801 - Aug-17-2019 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. RE: Attribute Error - Axel_Erfurt - Aug-17-2019 maybe r, g, b = [int(i) for i in rgb] ichabod801 was a second faster. RE: Attribute Error - Adit99 - Aug-18-2019 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 rgband here is the error Also, I can help you with my whole code if required. It performs image steganography.
RE: Attribute Error - wavic - Aug-18-2019 r1, g1, b1, *_ = rgb1 RE: Attribute Error - ichabod801 - Aug-18-2019 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. |