def ensure_bytes(value):
    """Converts value to bytes.

    Converts bytearray and str to bytes. Scanners may create
    child files that are one of these types, this method is used on
    every file object to ensure the file data is always bytes.

    Args:
        value: Value that needs conversion to bytes.

    Returns:
        A byte representation of value.
    """
    if isinstance(value, bytearray):
        return bytes(value)
    elif isinstance(value, str):
        return value.encode('utf-8')
    return value