tweak double-encode logic for image data
tweak double-encode logic for image data

instead of only b64-encoding for valid image data,
b64-encode unless it is specifically detected as b64-encoded valid image data.
This prevents garbage data from skipping the b64 step,
and causing unicode errors.

Closes #3081


ipython/ipython,ipython/ipython