Created
April 11, 2016 22:03
-
-
Save parthpower/6f5b760e4dd41dd411840010fe188957 to your computer and use it in GitHub Desktop.
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
from sys import argv | |
import cv2 | |
import numpy as np | |
def genTxtFile(imageFilePath,txtFilepath): | |
inputImage = cv2.imread(imageFilePath) | |
grayInputImage = cv2.cvtColor(inputImage,cv2.COLOR_RGB2GRAY) | |
try: | |
txtFile = open(txtFilepath,"w") | |
except: | |
return | |
for i in grayInputImage: | |
for j in i: | |
txtFile.write(str(j)+'\n') | |
return grayInputImage.shape | |
def genImageFile(imageFilePath,imageShape,txtFilepath): | |
try: | |
txtFile = open(txtFilepath,"r") | |
except: | |
return | |
imageMat = np.zeros(imageShape,np.uint8) | |
for i in range(imageShape[0]): | |
for j in range(imageShape[1]): | |
imageMat[i][j] = int(txtFile.readline()) | |
return cv2.imwrite(imageFilePath,imageMat) | |
if __name__ == '__main__': | |
if len(argv)<4: | |
print("Usage: python %s <operation> [height] [width] <imageFile> <textFile>\n\ | |
\toperation: img2txt,txt2img\n\ | |
\theight width only for txt2img"%(argv[0])) | |
exit() | |
if argv[1] == "img2txt": | |
genTxtFile(argv[2],argv[3]) | |
elif argv[1] == "txt2img": | |
genImageFile(argv[4],(int(argv[2]),int(argv[3])),argv[5]) | |
else: | |
print("Usage: python %s <operation> [imageShape(height,width)] <imageFile> <textFile>\n\toperation: img2txt or txt2img"%(argv[0])) | |
exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment