Launch file works diffrently than rosrun?

Hi, i am dooing the openCV for robotics course. if i use the code from exercise 2.2 and i rosrun the python file, the cv2.imwrite function works correctly, but if i run the launch file the imwrite function will not save the jpg.

Here is my exemple code:

#!/usr/bin/env python

import rospy
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import cv2
import time

class Test_image(object):
    def __init__(self):
        self.image_sub = rospy.Subscriber("/camera/rgb/image_raw", Image, self.camera_calback)
        self.image_path = '/home/user/catkin_ws/src/opencv_for_robotics_images/Unit_2/Course_images/test_image_1.jpg'
        self.cv_bridge_object = CvBridge()
        self.cv_image = cv2.imread(self.image_path)

        self.ctr_c = False
        rospy.on_shutdown(self.shutdownhook)

    def read_and_show_image(self):
        try:
            img = cv2.imread(self.image_path)
        except CvBridgeError as e:
            print(e)

        cv2.imshow("name of an image: ZIGA", img)
        cv2.waitKey(0)
        drone_image = cv2.imwrite('drone_image.jpg', self.cv_image)
        try:
            cv2.destroyAllWindows()
            print("destroyed in camera call back")
        except:
            print("soeomthing went wrong with the destroying images")
        
    def camera_calback(self, data):
        try:
            self.cv_image = self.cv_bridge_object.imgmsg_to_cv2(data, desired_encoding="bgr8")
        except CvBridgeError as e:
            print (e)
        
        
    def imwrite(self):
        rospy.loginfo("writing image to file")
        cv2.imshow("name of an image: ZIGA displayed image", self.cv_image)
        cv2.waitKey(0)
        drone_image = cv2.imwrite('imafdsdvgfsdfsge.jpg', self.cv_image)
        print(drone_image)
        try:
            cv2.destroyAllWindows()
            print("destroyed the windows")
        except:
            print("soeomthing went wrong with the destroying images")

        
        



    def shutdownhook(self):
        rospy.loginfo("Robot shutting down")
        self.ctr_c = True

def main():
    rospy.init_node("test_node")
    open_cv_class = Test_image()
    #open_cv_class.read_and_show_image()
    print("executing imwrite function")
    #time.sleep(2)
    open_cv_class.imwrite()

    while not rospy.is_shutdown():
        rospy.spin()




if __name__ == '__main__':
    main()

    print("Ros is shutting down")




and here is the launch file:

<launch>
<node pkg="unit2" name="load_image_node" type="load_image.py" output="screen"/>
</launch>

any ideas why this could be?

thanks Ziga

Hello @ziga.rupret ,

Both cases are saving the image correctly. The difference is that when you execute the program with rosrun, the image is saved on the path where you are executing the command, so you will see it easily. When you execute the program using roslaunch, the image is saved in the hidden folder .ros (which it’s the default location for all sorts of “system data”):

Captura de pantalla 2021-10-14 a las 10.02.23

Best,

ok thanks for the explanaiton

ziga