Skip to content

Instantly share code, notes, and snippets.

@fk128
Created April 6, 2018 16:58
Show Gist options
  • Save fk128/9b3f6f6e0c35abf77f79f13d2a378c99 to your computer and use it in GitHub Desktop.
Save fk128/9b3f6f6e0c35abf77f79f13d2a378c99 to your computer and use it in GitHub Desktop.
generate a random colormap for itk-snap. Useful for supervoxels.
import random
import sys
def print_xml_format(color, index, value):
if value == 0 or value == 1:
ctype = 'Discontinuous'
else:
ctype = 'Continuous'
print('''<folder key="ControlPoint[{}]" >
<entry key="Index" value="{}" />
<entry key="Type" value="{}" />
<folder key="Left" >
<entry key="A" value="255" />
<entry key="B" value="{}" />
<entry key="G" value="{}" />
<entry key="R" value="{}" />
</folder>
<folder key="Right" >
<entry key="A" value="255" />
<entry key="B" value="{}" />
<entry key="G" value="{}" />
<entry key="R" value="{}" />
</folder>
</folder>'''.format(str(index).zfill(4), value, ctype, color[0], color[1], color[2], color[0], color[1], color[2]))
def print_header(num_points):
print('''<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE registry [
<!ELEMENT registry (entry*,folder*)>
<!ELEMENT folder (entry*,folder*)>
<!ELEMENT entry EMPTY>
<!ATTLIST folder key CDATA #REQUIRED>
<!ATTLIST entry key CDATA #REQUIRED>
<!ATTLIST entry value CDATA #REQUIRED>
]>
<registry>
<entry key="NumberOfControlPoints" value="{}" />
<entry key="Preset" value="Custom" />'''.format(num_points))
def print_footer():
print('</registry>')
def main():
'''
generate a random colormap to be used by itk-snap
example: python generate_random_colormap_itk-snap.py 20 > random20.xml
'''
if len(sys.argv) > 1:
num_points = max(int(sys.argv[1]),2)
else:
num_points = 300
# with open('randomcolormap.xml', 'w') as sys.stdout:
print_header(num_points)
for i in range(num_points):
r = random.randint(1,255)
g = random.randint(1,255)
b = random.randint(1,255)
color = [r,g,b]
print_xml_format(color, i, i/(num_points-1))
print_footer()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment