Not really sure what might be wrong.
One idea is to save the image to disk the way you have been doing, then read it in as a string to compare it with your flattened variant. There might be other differences that you need to account for.
You might want to convert your variant back to a U8 array, then convert it to a string. This might produce a different result.
By the way, I only count 4 bytes, unless the same sequence (FFD8 FFE1) is repeated. Each two digits is a byte.
Bruce
Bruce Ammons
Ammons Engineering