ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

print UInt8MultiArray values in python

asked 2021-12-27 08:50:46 -0600

v.leto gravatar image
Hi! I would like to save into 3 arrays the fields of a custum message:

std_msgs/UInt8MultiArray  pixel
std_msgs/Float64MultiArray nord
std_msgs/Float64MultiArray est

this is my callback to listen to the message. When I try to print pixel I get letters. How could I transform them to integers from 0 to 255? thanks

 def callback(data):
        #rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.pixel.data)
        #rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.est.data)
        #rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.nord.data)
        pixel= np.array(data.pixel.data)
        print(pixel)
        est = np.array(data.est.data)
        nord= np.array(data.nord.data)
edit retag flag offensive close merge delete

Comments

Hi @v.leto can you please post what you get? Is it hexadecimal values?

osilva gravatar image osilva  ( 2021-12-27 13:36:10 -0600 )edit

Hi @osilva!

 print(pixel)

outputs

hcc`hda`dadhgcfc_bcccaacaacebfdhfcifggadfhhbihiffcgfddbeafbceacfgbfbdcgfddebab_`da_bccfgeb`ceedce`dac`fad`bcbcacfaddcacdfchbcafecibhchcfefjifgaddfggdieeg`cdffgdgfjhdgbdedjdhdfdegcfddd`eb`fecehjefdhhdcfcdcdhicdbbhbadgchgfijggiehflikpikighhffhhgchdeccdegfhbda`badaecdgdedb`adbcbc``dgiggd`ceegcbbhbhdbbadccdbbbaaahgggfedgdefddecbifffciijiefbgchfeffgaddfeefbddcdcbdecacaaabcce`ededdeafcdaabaaaed`fade`cb`c^^_bbbeba`_dcbcb``cddcdffdfgddhlggjjnlimlijh

they are numbers from 0 to 255 but they are stored like strings

v.leto gravatar image v.leto  ( 2021-12-27 14:40:34 -0600 )edit

I have read that it is a base64 because uint8[] are converted by rosbridge to base64,...

v.leto gravatar image v.leto  ( 2021-12-27 15:37:09 -0600 )edit

Ok then use this tutorial to decode: https://stackabuse.com/encoding-and-d...

import base64

base64_message = 'UHl0aG9uIGlzIGZ1bg=='
base64_bytes = base64_message.encode('ascii')
message_bytes = base64.b64decode(base64_bytes)
message = message_bytes.decode('ascii')

print(message)

Should Python is fun

osilva gravatar image osilva  ( 2021-12-27 15:49:05 -0600 )edit

Your code works. I tried to modify mine in order to look like yours but I get errors. I used an online decorder and I think my sting is not base64 as it doesn't end with ==. my string· eehggehhdeagefhfddbcadbecbcdcdaciffabbfchaaddbdcecafabbedecccececdbfdehhfibcaabfddcgegaabacddfcbcbfeedfdccddccfbagdfceceddadcefhggbfjdebicfggffddfcgcefhehcddfchdgcfacdbfegfifgdahceidiehjjghhddafhgdaggegaefkihngihhejifehjikjdkgjgighechgehdedcbdadddaddccfcdebdcacbcacafddecdadbacedbbadbcbcabbccgdbfgdebekifidghdegbehfhfdfgihigifbcighidcdcbefcccgceaddcfhdhfbdbbcdehddcccfddbceaecacbbbbfegidfbfddbdcfdegfchfeaeeidcbccgfbh`efcfgdhkhhhjimkkhffiljjnlhmgicbfeffgeefdbeggfdbaegfdeidcbfdhdjiihdhhhccdihfhifahehgdehhigjihhllemmlimgfjwrfmlnliflldbcfiijfhhhaehhhlmllqoqojlmnkfllorrqstgnirqmejppnkhqmjiijildghgehdihjnnnlkfklkkhiiaffgmkgejijhdihhhkefgddjklihjoqfnnqiimqmomnlnlikhedcgfhgjkihgighfhjfkikkhghmohhhacghehfeeghlkihdgfiehcfddbachchcabadeeegedffdejkhkmrmpqslnkolrrsklqpxtsqpxsuyxxrqpprurrmqrslnifklckpk

v.leto gravatar image v.leto  ( 2021-12-28 10:00:49 -0600 )edit

Hi @v.leto, take a look at this discussion as there is more to the conversion that I thought: https://github.com/gramaziokohler/ros...

pil_img = Image.fromarray(img)
buff = BytesIO()
pil_img.save(buff, format="JPEG")
new_image_string = base64.b64encode(buff.getvalue()).decode("utf-8")
osilva gravatar image osilva  ( 2021-12-28 10:17:32 -0600 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2021-12-28 10:23:42 -0600

v.leto gravatar image

Hi! Thank you very much for your interest @osilva. I finally found a solution...and maybe it was easier than what I thought:

 pixel =data.pixel.data
 x=[ord(c) for c in pixel]

gives you a list in x that can be used in python

edit flag offensive delete link more

Comments

Glad you found the answer.

osilva gravatar image osilva  ( 2021-12-28 10:26:54 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2021-12-27 08:50:46 -0600

Seen: 309 times

Last updated: Dec 28 '21