You do run into padding issues:
>>> open('pianoavatar.jpg').read(8192).encode('base64')[-5:]
'IIE=\n'
Base64 decoding stops when it encounters the =
padding marker. Your second read finds such a marker at the 10924th character.
You need to adjust your chunk size to be divisible by 3 instead to avoid padding in the middle of your output file. Use a chunk size of 8190, for example.
When reading, you need to use a buffersize that's a multiple of 4 to avoid running into alignment issues as well. 8192
would do fine there, but you must ensure this restriction is met in your functions. You'd be better off defaulting to the base64 expanded chunk size for the input chunks; 10920 for an encoding chunk size of 8190 (4 base64 characters for every 3 bytes encoded).
Demo:
>>> write_base64_file_from_file('pianoavatar.jpg', 'test.b64', 8190)
bin <type 'str'> data len: 8190
b64 <type 'str'> data len: 10920
bin <type 'str'> data len: 8190
b64 <type 'str'> data len: 10920
bin <type 'str'> data len: 1976
b64 <type 'str'> data len: 2636
Reading now works just fine, even at your original chunk size of 8192:
>>> write_file_from_base64_file('test.b64', 'test.jpg', 8192)
b64 <type 'str'> data len: 8192
bin <type 'str'> data len: 6144
b64 <type 'str'> data len: 8192
bin <type 'str'> data len: 6144
b64 <type 'str'> data len: 8092
bin <type 'str'> data len: 6068
You can force the buffersize to be aligned in your functions with a simple modulus:
def write_base64_file_from_file(src_fname, b64_fname, chunk_size=8190):
chunk_size -= chunk_size % 3 # align to multiples of 3
# ...
def write_file_from_base64_file(b64_fname, dst_fname, chunk_size=10920):
chunk_size -= chunk_size % 4 # align to multiples of 4
# ...