我正在编写一个读取位图(24位)的程序 在位图字符串中搜索0xff或0x0 填充一个零成本矩阵,其大小与图片大小相同 将值0x0的正确矩阵值更改为1。 然后,它读取每个二进制列并创建一个字符串 该字符串将转换为十六进制并保存在文档中。
图像只是黑白图像。
问题是我可以使用两个不同的测试图像。 一个是8x8,输出网格是预期的。
另一个是17x15,输出也如预期。
我制作了11x8px的几张新图像,这些图像完全错误。在某个地方,我在返回的位图字符串中有太多字符。
我读了位图,忽略了前54个字符,然后删除了其中扔出的所有奇数符号。任何不是0xff或0x0的内容都将被删除。
对于11x8px,我还剩下一些符号。
有问题的图像为11x8px,边缘周围有黑色像素,其他都没有。
总共应该为88像素,并且24位格式应包含264个条目,每3个成对显示BGR值。删除标题的54个第一项并删除奇数字符后,我有288个项。 24个字符过多,因此我的代码无效。 作为BMP加载的输入和输出网格的示例。 您会看到方形网格无法正确显示,而其他网格却不能正确显示。
方形BMP文件的字符串:
"BMV 6 ( ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ ÿÿÿ ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ "
有什么想法吗?这24个放错位置的符号在哪里,又意味着什么?
import numpy as np
import readBmp as ri
#test strings
str1 = " ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
ÿÿÿÿÿÿÿÿÿÿÿÿ ÿÿÿ ÿÿÿÿÿÿ ÿÿÿ ÿÿÿÿÿÿÿÿÿÿÿÿ
ÿÿÿÿÿÿÿÿÿ ÿÿÿÿÿÿ ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ"
str2 = "ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ ÿÿÿÿÿÿÿÿÿÿÿÿ
ÿÿÿ ÿÿÿÿÿÿ ÿÿÿ ÿÿÿÿÿÿÿÿÿÿÿÿ ÿÿÿÿÿÿÿÿÿ ÿÿÿÿÿÿ
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ"
def bmpToChar(filename,width,height):
#Get string from filename
string = ri.readBmp(filename)
#Amount of HexValues per pixel
hexValues = int(len(string)/(width*height))
if len(string)%(width*height) != 0:
print("ERROR! Unicode string is wrong lenght.\n Lenght",len(string),"Expected multiples of",width*height,"but got",len(string)%(width*height),"unkown values")
print("------------------------------------------------------")
print("Unicode string lenght:",len(string),"\nImage width:",width,"\nImage height:",height,"\nValues per pixel: ",hexValues)
#x,y position to start at
x = 1
y = 1
#pixel counter
count = 1
#Create array with zeros:
grid = np.zeros((height,width))
print("grid:\n",grid)
#Loop through values of bitmap file
while y <=height:
while x <=width:
#Loop for each value in bitmap from 0 to the last hex value
for i in range(0,hexValues):
if hex(ord(string[count-1]))=="0xff":
color = "white"
else:
color = "black"
#Only show x value and skip the rest
if count%hexValues == 0:
print("Hex entry nr:",count,hex(ord(string[count-1])),"at x:",x,",","y:",y,":", "color:",color,": pixel nr:",int(count/hexValues))
#Set correct binary value
if color == "black":
binValue = 1
else:
binValue = 0
#Change value of grid:
grid[(height-1)-(y-1),(x-1)] = binValue
print("New grid:\n",grid)
#else:
#print("Hex entry nr:",count,"not divideble by", hexValues)
count +=1
x += 1
x = 1
y += 1
print("Ended loops at x:",x," y:",y)
#Print final matrix:
print("Final grid:\n",grid)
#Save grid in file
file = open('CharInHex.txt','w')
#File Syntax prefix
file.write("{{")
for j in range(0,width):
#Reset binString to be empty
binString = ""
#Take current column and save it as binColumn
binColumn = grid[:,j]
print("\nbinColumn:",j+1,"\n",binColumn)
#For each binary entry in the binColumn save the entry to binString
for k in range(0,height):
#from bottom up height-1-k. From top just k
binString += str(int(binColumn[height-1-k]))
print("binString of column:",binString)
#Save as a hexa value
hexColumn = hex(int(binString,2))
print("hex(binString):",hexColumn)
#Save in file
file.write(hexColumn)
if j != (width-1):
file.write(",")
#File Syntax suffix
file.write("}}")
file.close()
#read file
fr = open('CharInHex.txt','r')
text = fr.read()
print("\n Saved document reads: \n",text)
fr.close()
#Reads Bitmap from filename and returns the string - first 54 chars.
def readBmp(filename):
#Read file
fr = open(filename+'.bmp','r')
bmpRaw = fr.read()
#Empty Header
fileInfo = ""
#Empty Pixel BGR string
string = ""
bmpRawLen = len(bmpRaw)
oddSymbols = 0
for i in range(0,54):
fileInfo += bmpRaw[i]
print("bitmap raw string:",bmpRaw,"\nRaw string
lenght",bmpRawLen,"\nFile info:",fileInfo)
print("-----------------------------------------------------------------
----------------")
print("Displaying header info:")
for j in range(0,54):
temp = hex(ord(fileInfo[j]))
intVal = int(ord(fileInfo[j]))
print("Entry:",j+1,"symbol:","'"+fileInfo[j]+"'","hex:",temp,"numeric value:",intVal)
#Create file without header:
print("\nRemoving header")
for k in range(54,bmpRawLen):
#Show each added symbol COMMENT OUT
print("Entry:",k+1,"symbol:","'"+bmpRaw[k]+"'","hex:",hex(ord(bmpRaw[k])),"int:",int(ord(bmpRaw[k])))
if (int(ord(bmpRaw[k])) == 255):
string += bmpRaw[k]
elif (int(ord(bmpRaw[k])) == 0):
string += bmpRaw[k]
else:
print("found odd symbol:","'"+bmpRaw[k]+"'","at entry:",k,"which was
not added to string")
oddSymbols += 1
print("Done. Removed header and",oddSymbols," odd symbols from rawBmp")
print("\nString without header:\n",string,"\nLenght of string:",len(string))
fr.close()
return string