def decode(self, imgObj):
        if not _checkIsStringIO(imgObj):
            raise TypeError('Given object is not a StringIO instance.')

        # Checking of image according to django.forms.fields.ImageField
        try:
            imgObj.seek(0)
            img = Image.open(imgObj)
            img.verify()
        except:
            raise ValueError('Content of given image could not be verified.')

        imgObj.seek(0)
        img = Image.open(imgObj)
        img.load()

        # Everything ok, convert PIL.Image to ROS and return it
        if img.mode == 'P':
            img = img.convert('RGB')

        rosimage = sensor_msgs.msg.Image()
        rosimage.encoding = ImageConverter._ENCODINGMAP_PY_TO_ROS[img.mode]
        (rosimage.width, rosimage.height) = img.size
        rosimage.step = (ImageConverter._PIL_MODE_CHANNELS[img.mode]
                         * rosimage.width)
        rosimage.data = img.tostring()
        return rosimage
Convert a image stored (PIL library readable image file format)
dataset/ETHPy150Open rapyuta/rce/rce-core/rce/util/converters/image.py/ImageConverter.decode