Created
May 29, 2017 08:52
-
-
Save zhulianhua/54b9fb4cb6b083745fcc2b29861d42a7 to your computer and use it in GitHub Desktop.
Convert from PNG image to ASCII flag (0/1) file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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