How to send Image as Mavlink Message

Actually , I tried to send Image as MavLink Message to QGC by following below link, but here I’m directly sending acknowledgement Data_Transmission_Handshake

Image Transmission Protocol · MAVLink Developer Guide

master = mavutil.mavlink_connection('udpout:', source_system=1, source_component=1, dialect="ardupilotmega")

Data_Tansmission_Handshake(sending image metadata) mavlink messsage

imageMetaData = mavlink2.MAVLink_data_transmission_handshake_message(
            type     =     mavlink2.MAVLINK_DATA_STREAM_IMG_JPEG,# Type of requested/acknowledged data. (type:uint8_t, values:MAVLINK_DATA_STREAM_TYPE),
            size     =     62882 ,#total data size (set on ACK only). [bytes] (type:uint32_t),
            width    =     1391,# (type:uint16_t),
            height   =     614,# (type:uint16_t),
            packets  =      1,#Number of packets being sent (set on ACK only). (type:uint16_t),
            payload  =     len(image_to_bytes_("image_path")),#Payload size per packet (normally 253 byte, see DATA field size in message ENCAPSULATED_DATA) (set on ACK only). [bytes] (type:uint8_t),
            jpg_quality =  100,) # JPEG quality. Values: [1-100]. [%] (type:uint8_t)

image_to_bytes function:

def image_to_bytes_(image_path, target_width=1391, target_height=614, target_payload_size=253):
    # Open the image file
    with open(image_path, 'rb') as image_file:
        # Read the image using PIL
        image =
        # Resize the image to the target dimensions
        resized_image = image.resize((target_width, target_height))
        # Convert the image to a numpy array of uint8_t
        image_array = np.array(resized_image, dtype=np.uint8)
        # Flatten the array to a 1D array
        flattened_array = image_array.flatten()
        # Take only the first target_payload_size elements
        truncated_array = flattened_array[:target_payload_size]
        # Pad the array with zeros if needed to reach target_payload_size
        padded_array = np.pad(truncated_array, (0, target_payload_size - len(truncated_array)), 'constant')
        # Convert the array to bytes
        bytes_data = bytes(padded_array)
        print("bytes_data  ",len(bytes_data))
        return bytes_data
except FileNotFoundError:
    print(f"Error: File '{image_path}' not found.")
    return None

Encapsulated Data(send image chunks) mavlink messsage

imageData = mavlink2.MAVLink_encapsulated_data_message(

At QGC end , QGC is able to read this mavlink message, but in console its showing below message:

Using QByteRef with an index pointing outside the valid range of a QByteArray. The corresponding behavior is deprecated, and will be changed in a future version of Qt.

ImageProtocolManagerLog: getImage: Known header QImage::loadFromData failed

I don’t know where I’m going wrong whether I’m not sending data correctly or some other thing. Please help me with this issue.

Thanks in advance.