文字比图片占用更多空间

时间:2020-01-30 14:56:01

标签: image storage

我一直在进行图像处理,发现有些奇怪的东西。我通过遍历图片并将其写在文本文件中来获取信息。即使不以任何方式添加空格或字符(没有添加换行符,最终,我只是对图像进行了灰度处理,而不是使用颜色)。尽管我所做的每一次更改都切断了图像的大小,但最终文本仍比原始图像大(即使经过压缩)。我什至实现了一个映射来用字母近似范围,所以我使用一个字符作为范围(k-> 220,230;程序假定所有k = 225),而不是3个字符数(rgb vals)。为什么即使经过灰度缩放和压缩,图像文件也比我的文本文件小?

def convertToText(img):
    """
    Send the img information into a text file. 
    @return:(row,col) of image
    """
    load=cv2.imread(img)
    height, width, layers = load.shape
    text=""
    #print(height,width, layers)
    file1=open(r"C:\Users\Devansh\Desktop\Projects\img\test2.txt","w+")
    x=list()
    sPixel=list()
    for i in range(height):
        for j in range(width):
            pixel=load[i,j]
            sPixel.append( int(str(pixel[0])+str(pixel[1])+str(pixel[2])) )
            #text=text+(encode(str(pixel[0]))+encode(str(pixel[1]))+encode(str(pixel[2]))) #text
            grayed=grayScale(pixel)
            x.append([0,grayed])
            #print("Fitting:",x,sPixel)
            text=text+ str( encodeGray(grayed))
            #file1.write('\n')
    clf.fit(x,sPixel)
    file1.write(text)

    return (height,width)

def convertToImg(height,width):
    file1=open(r"C:\Users\Devansh\Desktop\Projects\img\test2.txt")
    blank_image = np.zeros((height,width,3), np.uint8)
    i=0 #height index
    j=0 #width index
    colors=list(file1.read())
    for index in range(0,len(colors),1):
        #TODO: work on the stupid ungraying
        #print("Color:",colors[index])
        x=[[0,decode(colors[index])]]
        #prediction=(clf.predict(x))
        if(prediction==0):
            blank_image[i][j]=[0,0,0]
        else:
            predictionS=str(prediction)
            #index=
            pixelAvg=average(predictionS)
            blank_image[i][j]=[int(pixelAvg[0]),int(pixelAvg[1]),int(pixelAvg[2])]
        #print("Pixel: ",pixelAvg)
        blank_image[i][j]=[int(decode(colors[index])),int(decode(colors[index+1])),int(decode(colors[index+2]))]
        if ( j<width-1 ):
            j+=1
        else:
            j=0
            i+=1
    cv2.imshow("Get Wrecked Shree", blank_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
step=20
stepGrey=0.05
colorMap=dict()
chars=list(ascii_uppercase)

def createMap():

    i=0
    for middle in range (0,256,step):
        colorMap[chars[i]]=middle
        i+=1
    #print(colorMap)
    return colorMap

def createGreyMap():
    for middle in range (21):
        colorMap[chars[middle]]=float(middle/20)
    #print(colorMap)
    return colorMap


def encode(color):
    """
    Encode the pixels color into the map
    """
    color=int(color)
    colorMap=createMap() #get the mapping for pixel value to letter
    alpha=color//step #the alphabet corresponding to the number
    return(str(list(colorMap.keys())[alpha]))

def encodeGray(grayed):
    """
    Take grayscale value and map it to our charset
    """
    colorMap=createGreyMap()
    alpha=int(grayed/stepGrey) #the alphabet corresponding to the number
    return(str(list(colorMap.keys())[alpha]))


def decode(char):
    """
    Return color value corresponding to the char
    """
    char=str(char).capitalize()
    colorMap=createMap()
    #print(colorMap['A'])
    return(int(colorMap[char]))

def decodeGrey(char):
    """
    Return color value corresponding to the char
    """
    char=str(char).capitalize()
    colorMap=createGreyMap()
    #print(colorMap['A'])
    return( ungray(colorMap[char]) )


def grayScale(n):
    """
    Take pixel and returns the mathematical grayscaled value
    """
    r,g,b =n[0]/255, n[1]/255, n[2]/255 #fractional values
    linearCoeff= 0.2126 *r + 0.7152*g + 0.0722*b #math and explaination in documentation
    grayed=0
    if(linearCoeff<=0.0031308): # implementing the gamma adjustement
        grayed= 12.92*linearCoeff
    else:
        grayed=1.055* pow(linearCoeff,1/2.4) - 0.055 #non linear factor
    #return grayed
    return grayed

def ungray(n):
    """
    Take grayscaled n and return ungrayed pixel color
    """
    #TODO: find the actual probability distribution.
    return int(n*255)

运行此操作可能会出现一些错误,因为我尝试为项目添加一些机器学习功能,但是基本功能应该可以使用。 enter image description here

0 个答案:

没有答案