Skip to content

Instantly share code, notes, and snippets.

@zhulianhua
Created May 29, 2017 08:52
Show Gist options
  • Save zhulianhua/54b9fb4cb6b083745fcc2b29861d42a7 to your computer and use it in GitHub Desktop.
Save zhulianhua/54b9fb4cb6b083745fcc2b29861d42a7 to your computer and use it in GitHub Desktop.
Convert from PNG image to ASCII flag (0/1) file
import numpy as np
import matplotlib.image as mpimg
def rgb2gray(rgb):
''' Convert rgb to gray '''
return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])
def gray2bin(gray):
''' Convert gray to binary (0,1)'''
#return (gray>0.5).astype('uint8')
return (gray<0.5).astype('uint8')
fname = 'cylinder_simple2'
img = mpimg.imread(fname+'.png')
np.savetxt(fname+'.dat', gray2bin(rgb2gray(img)), fmt="%d")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment