Jon Snow
Jon Snow

Reputation: 240

Publisher Subscriber Model Overloaded Import Names

So I'm trying to create a program in ROS using Python that publishes images, but I have two things I import that are both called 'Image'. When I run the program, I get this error message.

  File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/topics.py", line 812, in __init__
    super(Publisher, self).__init__(name, data_class, Registration.PUB)
  File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/topics.py", line 138, in __init__
    raise ValueError("data_class [%s] is not a class"%data_class) 
ValueError: data_class [<module 'PIL.Image' from '/usr/lib/python2.7/dist-packages/PIL/Image.pyc'>] is not a class

When I take out the lines importing PIL Image and ImageFilter, the line initializing image1, and the line publishing image1, the program works fine because (I think) there's no confusion between two imported Image objects. Is there a way to tell the program to differentiate between the two Images?

#!/usr/bin/env python

import rospy
from sensor_msgs.msg import Image
from PIL import Image, ImageFilter

def camera():
    pub = rospy.Publisher('rgb', Image, queue_size=10)
    image1 = Image.open('dog.png')
    pub.publish(image1)
if __name__ == '__main__':
    try:
        camera()
    except rospy.ROSInterruptException:
        pass

Upvotes: 0

Views: 1596

Answers (1)

luator
luator

Reputation: 5019

You can rename things during import:

from sensor_msgs.msg import Image as ImageMsg

This way you can avoid the name collision.

Upvotes: 1

Related Questions