从预设调色板将图像像素转换为文本
image pixel to text conversion from preset pallete
我有一个脚本可以获取已转换为自定义调色板(颜色)的图像,并使用该调色板遍历每个像素并创建一个文本文件,该文件的每个像素都有一个与颜色相关的字符(索引转换)
但是当我在游戏中使用结果时,似乎与图像有很大的不同,如果我在文本创建过程中做错了什么,谁能告诉我。
谢谢!
import matplotlib.image as image
import numpy as np
import time
#colors=[[255, 138, 114],[255, 191, 112],[255, 255, 139],[223, 249, 114],[255, 90, 79],[255, 152, 75],[255, 255, 75],[196, 70, 62],[255, 111, 72],[255, 157, 67],[122, 133, 70],[201, 101, 86],[208, 120, 83],[213, 152, 91],[137, 137, 84],[186, 255, 153],[185, 255, 243],[183, 255, 255],[182, 229, 255],[92, 153, 74],[94, 221, 168],[92, 242, 255],[103, 164, 255],[87, 113, 71],[84, 134, 103],[86, 139, 144],[80, 105, 154],[119, 125, 91],[120, 137, 110],[118, 141, 132],[117, 131, 151],[255, 192, 255],[255, 213, 255],[255, 182, 212],[203, 114, 70],[150, 78, 255],[193, 107, 153],[255, 89, 104],[134, 84, 65],[105, 69, 144],[126, 68, 96],[207, 62, 91],[120, 73, 59],[154, 117, 146],[157, 105, 117],[176, 101, 106],[103, 71, 65],[255, 188, 117],[255, 255, 255],[170, 151, 125],[220, 150, 97],[255, 254, 214],[137, 118, 101],[157, 107, 79],[232, 205, 171],[99, 86, 75],[99, 76, 63],[201, 171, 142],[84, 72, 64],[255, 255, 255]]
colors=[[191, 74, 84],[179, 24, 45],[107, 11, 38],[113, 42, 56],[115, 60, 55],[169, 51, 39],[198, 86, 46],[195, 119, 81],[199, 184, 101],[200, 175, 46],[161, 92, 37],[119, 89, 61],[59, 74, 51],[40, 70, 36],[99, 148, 43],[127, 159, 83],[96, 168, 118],[16, 93, 47],[4, 54, 42],[43, 69, 60],[46, 79, 84],[3, 74, 74],[98, 187, 183],[96, 175, 200],[19, 156, 200],[6, 80, 114],[45, 84, 105],[44, 73, 117],[4, 52, 124],[25, 99, 194],[96, 148, 206],[148, 125, 205],[74, 25, 191],[43, 19, 117],[79, 66, 118],[81, 55, 92],[57, 24, 75],[111, 60, 127],[188, 137, 204],[192, 111, 163],[192, 42, 79],[119, 12, 68],[95, 49, 78],[30, 17, 37],[55, 27, 39],[63, 38, 44],[115, 59, 44],[172, 123, 91],[130, 93, 75],[82, 58, 58],[34, 27, 43],[113, 111, 116],[140, 137, 141],[167, 167, 169],[199, 199, 199],[91, 94, 101],[67, 70, 79],[39, 42, 59],[18, 20, 41],]
filename=str(input("Filename:"))
full_file_address=filename
img=image.imread(full_file_address)
color_code=list(range(0,61))
color_tolerance= 5
print(colors[46])
print(len(colors))
print('The Shape of the image is:',img.shape)
width=img.shape[1]
height=img.shape[0]
RecRoomMatrix= np.zeros((width,height))
current_Layer=0
current_Pixel=0
current_Color=0
IndexConvert=['~','`','!','@','#','$','%','^','&','*','(',')','_','-','+','=','{','}','[',']','|',';',':','"','<',',','.','>','?','/','†','‡','‹','•','™','›','¡','¤','¦','«','°','±','²','³','»','¹','¼','½','¾','¿','¨','¬','¯','·','¸','º','¢','£','ò','ó',' ']
finaltext=""
for l in img:
for p in l:
for c in colors:
if p[0]<=c[0]+color_tolerance and p[0]>=c[0]-color_tolerance and p[1]<=c[1]+color_tolerance and p[1]>=c[1]-color_tolerance and p[2]<=c[2]+color_tolerance and p[2]>=c[2]-color_tolerance:
RecRoomMatrix[current_Pixel][current_Layer]=int(color_code[current_Color])
finaltext=finaltext+(IndexConvert[current_Color])
#print(IndexConvert[current_Color])
current_Color =0
break
elif current_Color > 61:
current_Color =0
RecRoomMatrix[current_Pixel][current_Layer] = 100
finaltext = finaltext + (IndexConvert[-1])
print("color not found")
else:
current_Color += 1
#print("pixel:", current_Pixel, " done of:", width)
current_Pixel +=1
#print("why")
print("layer:",current_Layer," done of:", height)
current_Pixel=0
current_Layer += 1
if current_Layer>height-1:
break
#print(RecRoomMatrix)
#print(RecRoomMatrix.size)
#print(RecRoomMatrix[0])
print("finished")
Final_List=RecRoomMatrix.tolist()
#print(Final_List)
Final_Name=filename+".txt"
textfile = open(Final_Name, "w")
for element in Final_List:
textfile.write(str(element))
textfile.close()
finalstring=""
for j in finaltext:
finalstring=finalstring+j
#print(finalstring)
Final_Name2=filename+"string.txt"
textfile2 = open(Final_Name2, "w")
textfile2.write(finalstring)
textfile2.close()
#print(finaltext)
#np.savetxt(filename, RecRoomMatrix.astype(int), delimiter =", ")
图像结果
[1]: https://i.stack.imgur.com/UirjX.png
原图
[2]: https://i.stack.imgur.com/i8v44.jpg
也许这可以给你一点线索。这会为整个数组中的每个像素找到最接近的匹配项。请注意,我已经删除了很多不必要的代码,并且我正在使用 enumerate
,因此您不必单独跟踪循环编号。
我不知道您使用的是什么字符集,所以“IndexConvert”数组在 cut-and-paste 期间弄乱了。我用字母数字代替了它。请注意,您不需要字符列表——您可以使用简单的字符串。
# *-* encoding: latin1 *-*
import matplotlib.image as image
import numpy as np
colors=[[191, 74, 84],[179, 24, 45],[107, 11, 38],[113, 42, 56],[115, 60, 55],[169, 51, 39],[198, 86, 46],[195, 119, 81],[199, 184, 101],[200, 175, 46],[161, 92, 37],[119, 89, 61],[59, 74, 51],[40, 70, 36],[99, 148, 43],[127, 159, 83],[96, 168, 118],[16, 93, 47],[4, 54, 42],[43, 69, 60],[46, 79, 84],[3, 74, 74],[98, 187, 183],[96, 175, 200],[19, 156, 200],[6, 80, 114],[45, 84, 105],[44, 73, 117],[4, 52, 124],[25, 99, 194],[96, 148, 206],[148, 125, 205],[74, 25, 191],[43, 19, 117],[79, 66, 118],[81, 55, 92],[57, 24, 75],[111, 60, 127],[188, 137, 204],[192, 111, 163],[192, 42, 79],[119, 12, 68],[95, 49, 78],[30, 17, 37],[55, 27, 39],[63, 38, 44],[115, 59, 44],[172, 123, 91],[130, 93, 75],[82, 58, 58],[34, 27, 43],[113, 111, 116],[140, 137, 141],[167, 167, 169],[199, 199, 199],[91, 94, 101],[67, 70, 79],[39, 42, 59],[18, 20, 41],]
filename='samp1.jpg' #str(input("Filename:"))
img=image.imread(filename)
print('The Shape of the image is:',img.shape)
width=img.shape[1]
height=img.shape[0]
RecRoomMatrix= np.zeros((width,height), dtype=np.uint8)
IndexConvert='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678'
def findmin(rgb):
return colors.index(min(colors, key=lambda c:
(rgb[0]-c[0])*(rgb[0]-c[0]) +
(rgb[1]-c[1])*(rgb[1]-c[1]) +
(rgb[2]-c[2])*(rgb[2]-c[2])
))
finaltext=""
for li, l in enumerate(img):
for pi, p in enumerate(l):
ci = findmin( p )
RecRoomMatrix[pi][li]=ci
finaltext=finaltext+IndexConvert[ci]
print("layer:",li," done of:", height)
print("finished")
Final_List=RecRoomMatrix.tolist()
print(Final_List)
Final_Name=filename+".txt"
textfile = open(Final_Name, "w")
for element in Final_List:
print(element, file=textfile)
textfile.close()
Final_Name2=filename+"string.txt"
textfile2 = open(Final_Name2, "w", encoding='utf-8')
textfile2.write(finaltext)
textfile2.close()
我有一个脚本可以获取已转换为自定义调色板(颜色)的图像,并使用该调色板遍历每个像素并创建一个文本文件,该文件的每个像素都有一个与颜色相关的字符(索引转换)
但是当我在游戏中使用结果时,似乎与图像有很大的不同,如果我在文本创建过程中做错了什么,谁能告诉我。
谢谢!
import matplotlib.image as image
import numpy as np
import time
#colors=[[255, 138, 114],[255, 191, 112],[255, 255, 139],[223, 249, 114],[255, 90, 79],[255, 152, 75],[255, 255, 75],[196, 70, 62],[255, 111, 72],[255, 157, 67],[122, 133, 70],[201, 101, 86],[208, 120, 83],[213, 152, 91],[137, 137, 84],[186, 255, 153],[185, 255, 243],[183, 255, 255],[182, 229, 255],[92, 153, 74],[94, 221, 168],[92, 242, 255],[103, 164, 255],[87, 113, 71],[84, 134, 103],[86, 139, 144],[80, 105, 154],[119, 125, 91],[120, 137, 110],[118, 141, 132],[117, 131, 151],[255, 192, 255],[255, 213, 255],[255, 182, 212],[203, 114, 70],[150, 78, 255],[193, 107, 153],[255, 89, 104],[134, 84, 65],[105, 69, 144],[126, 68, 96],[207, 62, 91],[120, 73, 59],[154, 117, 146],[157, 105, 117],[176, 101, 106],[103, 71, 65],[255, 188, 117],[255, 255, 255],[170, 151, 125],[220, 150, 97],[255, 254, 214],[137, 118, 101],[157, 107, 79],[232, 205, 171],[99, 86, 75],[99, 76, 63],[201, 171, 142],[84, 72, 64],[255, 255, 255]]
colors=[[191, 74, 84],[179, 24, 45],[107, 11, 38],[113, 42, 56],[115, 60, 55],[169, 51, 39],[198, 86, 46],[195, 119, 81],[199, 184, 101],[200, 175, 46],[161, 92, 37],[119, 89, 61],[59, 74, 51],[40, 70, 36],[99, 148, 43],[127, 159, 83],[96, 168, 118],[16, 93, 47],[4, 54, 42],[43, 69, 60],[46, 79, 84],[3, 74, 74],[98, 187, 183],[96, 175, 200],[19, 156, 200],[6, 80, 114],[45, 84, 105],[44, 73, 117],[4, 52, 124],[25, 99, 194],[96, 148, 206],[148, 125, 205],[74, 25, 191],[43, 19, 117],[79, 66, 118],[81, 55, 92],[57, 24, 75],[111, 60, 127],[188, 137, 204],[192, 111, 163],[192, 42, 79],[119, 12, 68],[95, 49, 78],[30, 17, 37],[55, 27, 39],[63, 38, 44],[115, 59, 44],[172, 123, 91],[130, 93, 75],[82, 58, 58],[34, 27, 43],[113, 111, 116],[140, 137, 141],[167, 167, 169],[199, 199, 199],[91, 94, 101],[67, 70, 79],[39, 42, 59],[18, 20, 41],]
filename=str(input("Filename:"))
full_file_address=filename
img=image.imread(full_file_address)
color_code=list(range(0,61))
color_tolerance= 5
print(colors[46])
print(len(colors))
print('The Shape of the image is:',img.shape)
width=img.shape[1]
height=img.shape[0]
RecRoomMatrix= np.zeros((width,height))
current_Layer=0
current_Pixel=0
current_Color=0
IndexConvert=['~','`','!','@','#','$','%','^','&','*','(',')','_','-','+','=','{','}','[',']','|',';',':','"','<',',','.','>','?','/','†','‡','‹','•','™','›','¡','¤','¦','«','°','±','²','³','»','¹','¼','½','¾','¿','¨','¬','¯','·','¸','º','¢','£','ò','ó',' ']
finaltext=""
for l in img:
for p in l:
for c in colors:
if p[0]<=c[0]+color_tolerance and p[0]>=c[0]-color_tolerance and p[1]<=c[1]+color_tolerance and p[1]>=c[1]-color_tolerance and p[2]<=c[2]+color_tolerance and p[2]>=c[2]-color_tolerance:
RecRoomMatrix[current_Pixel][current_Layer]=int(color_code[current_Color])
finaltext=finaltext+(IndexConvert[current_Color])
#print(IndexConvert[current_Color])
current_Color =0
break
elif current_Color > 61:
current_Color =0
RecRoomMatrix[current_Pixel][current_Layer] = 100
finaltext = finaltext + (IndexConvert[-1])
print("color not found")
else:
current_Color += 1
#print("pixel:", current_Pixel, " done of:", width)
current_Pixel +=1
#print("why")
print("layer:",current_Layer," done of:", height)
current_Pixel=0
current_Layer += 1
if current_Layer>height-1:
break
#print(RecRoomMatrix)
#print(RecRoomMatrix.size)
#print(RecRoomMatrix[0])
print("finished")
Final_List=RecRoomMatrix.tolist()
#print(Final_List)
Final_Name=filename+".txt"
textfile = open(Final_Name, "w")
for element in Final_List:
textfile.write(str(element))
textfile.close()
finalstring=""
for j in finaltext:
finalstring=finalstring+j
#print(finalstring)
Final_Name2=filename+"string.txt"
textfile2 = open(Final_Name2, "w")
textfile2.write(finalstring)
textfile2.close()
#print(finaltext)
#np.savetxt(filename, RecRoomMatrix.astype(int), delimiter =", ")
图像结果 [1]: https://i.stack.imgur.com/UirjX.png
原图 [2]: https://i.stack.imgur.com/i8v44.jpg
也许这可以给你一点线索。这会为整个数组中的每个像素找到最接近的匹配项。请注意,我已经删除了很多不必要的代码,并且我正在使用 enumerate
,因此您不必单独跟踪循环编号。
我不知道您使用的是什么字符集,所以“IndexConvert”数组在 cut-and-paste 期间弄乱了。我用字母数字代替了它。请注意,您不需要字符列表——您可以使用简单的字符串。
# *-* encoding: latin1 *-*
import matplotlib.image as image
import numpy as np
colors=[[191, 74, 84],[179, 24, 45],[107, 11, 38],[113, 42, 56],[115, 60, 55],[169, 51, 39],[198, 86, 46],[195, 119, 81],[199, 184, 101],[200, 175, 46],[161, 92, 37],[119, 89, 61],[59, 74, 51],[40, 70, 36],[99, 148, 43],[127, 159, 83],[96, 168, 118],[16, 93, 47],[4, 54, 42],[43, 69, 60],[46, 79, 84],[3, 74, 74],[98, 187, 183],[96, 175, 200],[19, 156, 200],[6, 80, 114],[45, 84, 105],[44, 73, 117],[4, 52, 124],[25, 99, 194],[96, 148, 206],[148, 125, 205],[74, 25, 191],[43, 19, 117],[79, 66, 118],[81, 55, 92],[57, 24, 75],[111, 60, 127],[188, 137, 204],[192, 111, 163],[192, 42, 79],[119, 12, 68],[95, 49, 78],[30, 17, 37],[55, 27, 39],[63, 38, 44],[115, 59, 44],[172, 123, 91],[130, 93, 75],[82, 58, 58],[34, 27, 43],[113, 111, 116],[140, 137, 141],[167, 167, 169],[199, 199, 199],[91, 94, 101],[67, 70, 79],[39, 42, 59],[18, 20, 41],]
filename='samp1.jpg' #str(input("Filename:"))
img=image.imread(filename)
print('The Shape of the image is:',img.shape)
width=img.shape[1]
height=img.shape[0]
RecRoomMatrix= np.zeros((width,height), dtype=np.uint8)
IndexConvert='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678'
def findmin(rgb):
return colors.index(min(colors, key=lambda c:
(rgb[0]-c[0])*(rgb[0]-c[0]) +
(rgb[1]-c[1])*(rgb[1]-c[1]) +
(rgb[2]-c[2])*(rgb[2]-c[2])
))
finaltext=""
for li, l in enumerate(img):
for pi, p in enumerate(l):
ci = findmin( p )
RecRoomMatrix[pi][li]=ci
finaltext=finaltext+IndexConvert[ci]
print("layer:",li," done of:", height)
print("finished")
Final_List=RecRoomMatrix.tolist()
print(Final_List)
Final_Name=filename+".txt"
textfile = open(Final_Name, "w")
for element in Final_List:
print(element, file=textfile)
textfile.close()
Final_Name2=filename+"string.txt"
textfile2 = open(Final_Name2, "w", encoding='utf-8')
textfile2.write(finaltext)
textfile2.close()