Skip to content

Instantly share code, notes, and snippets.

@migurski
Last active December 17, 2015 13:29
Show Gist options
  • Save migurski/5617692 to your computer and use it in GitHub Desktop.
Save migurski/5617692 to your computer and use it in GitHub Desktop.
Store sparse bundle disk images as a series of 24 bit losslessly-encoded PNG images. Let Flickr’s 1TB of storage hold them for you.
#!/usr/bin/env python
''' Convert a series of PNG images to a sparse bundle disk image.
Image file names are passed in as command line arguments.
Example:
ls *.png | xargs deimgify.py
Output:
Goodtimes.sparsebundle/Info.bckup
Goodtimes.sparsebundle/Info.plist
Goodtimes.sparsebundle/bands/0
Goodtimes.sparsebundle/bands/1
Goodtimes.sparsebundle/bands/2
...
Goodtimes.sparsebundle/token
'''
from struct import unpack
from os.path import dirname
from os import makedirs
from PIL import Image
from sys import argv
for img_path in argv[1:]:
image = Image.open(img_path)
bytes = image.tostring()
row_len = image.size[0] * 3
#
# First row of pixels is a pair of lengths and the filename.
#
len_path, len_data = unpack('!II', bytes[:8])
data_path = bytes[8:8 + len_path].decode('utf8')
#
# Subsequent rows are the raw data.
#
data = bytes[row_len:row_len + len_data]
try:
makedirs(dirname(data_path))
except OSError:
pass
with open(data_path, 'w') as output:
output.write(data)
print data_path
#!/usr/bin/env python
''' Convert a sparse bundle disk image to a series of PNG images.
Files in the disk image are passed in as command line arguments.
Example:
find Goodtimes.sparsebundle -type f | xargs imgify.py
Output:
imgify-VxZ1k5.png
imgify-SynUtR.png
...
imgify-rPfWxH.png
'''
from os import close
from struct import pack
from tempfile import mkstemp
from PIL import Image
from sys import argv
row_len = 1024 * 3
zeros = '\x00' * row_len
for data_path in argv[1:]:
handle, img_path = mkstemp(dir='.', prefix='imgify-', suffix='.png')
close(handle)
with open(data_path) as input:
path = data_path.encode('utf8')
data = input.read()
rows = []
#
# First row of pixels is a pair of lengths and the filename.
#
rows.append(pack('!II', len(path), len(data)) + path)
rows[-1] += zeros[len(rows[-1]):]
#
# Subsequent rows are the raw data.
#
for off in range(0, len(data), row_len):
rows.append(data[off:off + row_len])
rows[-1] += zeros[len(rows[-1]):]
img = Image.fromstring('RGB', (1024, len(rows)), ''.join(rows))
img.save(img_path)
print img_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment