Skip to content

Instantly share code, notes, and snippets.

@parthpower
Created April 11, 2016 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parthpower/6f5b760e4dd41dd411840010fe188957 to your computer and use it in GitHub Desktop.
Save parthpower/6f5b760e4dd41dd411840010fe188957 to your computer and use it in GitHub Desktop.
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