Reputation: 39
Hello I'm a newbie at python. In my function, I keep getting warnings of unused variables r,g,b,a. Can someone explain it for me?
def encode_image(self,img, msg):
length = len(msg)
if length > 255:
print("text too long! (don't exeed 255 characters)")
return False
encoded = img.copy()
width, height = img.size
index = 0
for row in range(height):
for col in range(width):
if img.mode != 'RGB':
r, g, b ,a = img.getpixel((col, row))
elif img.mode == 'RGB':
r, g, b = img.getpixel((col, row))
# first value is length of msg
if row == 0 and col == 0 and index < length:
asc = length
elif index <= length:
c = msg[index -1]
asc = ord(c)
else:
asc = b
encoded.putpixel((col, row), (r, g , asc))
index += 1
return encoded
Unused variable 'a' pylint(unused-variable)
Unused variable 'r' pylint(unused-variable)
Unused variable 'g' pylint(unused-variable)
Unused variable 'b' pylint(unused-variable)
r, g, b ,a = img.getpixel((col, row))
TypeError: cannot unpack non-iterable int object
Upvotes: 0
Views: 200
Reputation: 462
Your check if img.mode !='RGB'
is not sufficient. Seems like your image is greyscale, hence getpixel()
will only return one integer value, hence it cannot split an integer into 4 variables. (i.e. can't unpack to 4 variables)
Use img.getbands()
to find how many bands are there and then unpack accordingly.
Upvotes: 1