Skip to content

Instantly share code, notes, and snippets.

@bbarad
Created October 3, 2019 21:50
Show Gist options
  • Save bbarad/f9b1602c9a19e274737308610dc00734 to your computer and use it in GitHub Desktop.
Save bbarad/f9b1602c9a19e274737308610dc00734 to your computer and use it in GitHub Desktop.
Convert Class Average MRC File to PNGs
import imageio
import mrcfile
import os
import sys
def make_photos(basename, working_directory="."):
"""
Convert MRC file with stack of classes to series of scaled PNGs for web viewing.
Args:
basename (str): name of desired folder within class_images - usually the same name as the mrc file.
working_directory (str): the base directory where :py:mod:`live_2d` is working.
Returns:
str: Directory path with new PNGs written out.
"""
if not os.path.isdir(os.path.join(working_directory, "class_images")):
os.mkdir(os.path.join(working_directory, "class_images"))
if not os.path.isdir(os.path.join(working_directory, "class_images", basename)):
os.mkdir(os.path.join(working_directory, "class_images", basename))
photo_dir = os.path.join(working_directory, "class_images", basename)
with mrcfile.open(os.path.join(working_directory, "{}.mrc".format(basename)), "r") as stack:
for index, item in enumerate(stack.data):
imageio.imwrite(os.path.join(photo_dir, "{}.png".format(index+1)), item)
return photo_dir
if __name__ == "__main__":
filename = sys.argv[1]
assert(filename[-4:] == ".mrc")
basename = filename[:-4]
print(make_photos(basename))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment