Skip to content

Instantly share code, notes, and snippets.

@mikeyakymenko
Created April 7, 2013 19:39
Show Gist options
  • Save mikeyakymenko/5332141 to your computer and use it in GitHub Desktop.
Save mikeyakymenko/5332141 to your computer and use it in GitHub Desktop.
Кодирование картинок в base64
#!/usr/bin/env python
# coding: utf-8
import os
import urllib
def rel(*x):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), *x)
directory = rel()
ends = ('.jpg', '.png', '.gif')
f = open('codes.txt', 'ab')
images = filter(lambda x: x.endswith(ends), os.listdir(directory));
for a in images:
encoded = urllib.quote(open(a, "rb").read().encode("base64"))
ending = a.split('.')[1]
base64 = 'data:image/%s;base64,' % ending
f.write(a + '\n' + base64 + encoded + '\n\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment