def compress_str(s):
    """
    Compress a string by replacing consecutive duplicate characters with a count
    of the number of characters followed by the character.
    """
    if not s:
        return s
    compressed_str = []
    count = 1
    for i in range(1, len(s)):
        if s[i] == s[i-1]:
            count += 1
        else:
            compressed_str.append(str(count))
            compressed_str.append(s[i-1])
            count = 1
    compressed_str.append(str(count))
    compressed_str.append(s[-1])
    return ''.join(compressed_str)