让我解释一下我的意思:我从9张图片中获取数据,包括r,g,b值和亮度,计算每张图片的平均值,然后将每张图片的值写入.txt文件(在记事本中) )每张图片的字符串。我现在正试图从该文件中取任意两个字符串,并通过差值公式计算值的差异(差值= val.1 - val.2 / val.2)。我无法弄清楚的是如何读取.txt文件并使用两个不同的字符串进行计算。我非常感谢任何提示或帮助。
这是我到目前为止所拥有的:
from graphics import *
def loadImage(image):
totalBrightness = 0
totalR = 0
totalG = 0
totalB = 0
win = GraphWin("Picture", 400, 500)
testImage = Image(Point(200,250), image)
testImage.draw(win)
throwAway = win.getMouse()
theWidth = testImage.getWidth()
theHeight = testImage.getHeight()
for i in range(0,theWidth):
for j in range(0,theHeight):
r,g,b = testImage.getPixel(i,j)
brightness = int(round(0.299*r + 0.587*g + 0.114*b))
totalR = totalR + r
totalG = totalG + g
totalB = totalB + b
totalBrightness = totalBrightness + brightness
return (image, totalBrightness, totalR, totalG, totalB)
def ImageAnalysis():
infile = open("Picture_Features.txt","a+") #cousin told me 'a+' work here if that's important
print(loadImage("mystery1.GIF"), file = infile)
print(loadImage("mystery2.GIF"), file = infile)
print(loadImage("mystery3.GIF"), file = infile)
答案 0 :(得分:0)
据我所知,该方法相当简单:
应该是这样的:
f = open("Picture_Features.txt", 'r')
l = f.readlines()
f.close()
# Convert string to list with split
for i in range(len(l)):
l[i] = l[i][1:-1].split(', ')
# Let's take the first 2 entries (not the image string in the front)
e1, e2 = map(int, l[0][1:]), map(int, l[1][1:])
diff_formula = lambda x1, x2: (x1 - x2) / x2
result = map(diff_formula, e1, e2)