Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Attribute Error
#1
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'
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
maybe

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

ichabod801 was a second faster.
Reply
#4
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.
Reply
#5
r1, g1, b1, *_ = rgb1
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error: audioio has no attribute 'AudioOut' netwrok 3 594 Oct-22-2023, 05:53 PM
Last Post: netwrok
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,216 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Getting 'NoneType' object has no attribute 'find' error when WebScraping with BS Franky77 2 5,167 Aug-17-2021, 05:24 PM
Last Post: Franky77
  Attribute Error received not understood (Please Help) crocolicious 5 2,614 Jun-19-2021, 08:45 PM
Last Post: crocolicious
  error in scapy attribute 'haslayer' evilcode1 5 6,439 Mar-02-2021, 11:19 AM
Last Post: evilcode1
  attribute error instead of correct output MaartenRo 2 2,147 Aug-28-2020, 10:22 AM
Last Post: Larz60+
  attribute error stumped on how to fix it. hank4eva 7 4,597 Aug-11-2020, 04:47 AM
Last Post: hank4eva
  Attribute Error - trying to create a pixel array out of PNG files The_Sarco 1 1,970 Apr-29-2020, 07:10 PM
Last Post: deanhystad
  Attribute Error for Rename / Replace warden89 6 7,700 Jan-07-2020, 06:08 PM
Last Post: warden89
  Reading DBF files from Amazon s3 throwing error - 'int' object has no attribute 'isa abeesm 1 2,861 Sep-22-2019, 05:49 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020