Reputation: 1
I'm trying to recover separately value R, G & B from a ppm file in three different matrices.
I wrote this script :
#Read file
feep_file = open("feep.ascii.ppm", "r")
def createMatrixRGB(filename):
matrixR = []
matrixG = []
matrixB = []
tableR = []
tableG = []
tableB = []
tmp = False
for lines in filename:
x = lines.split()
for y in x:
if(y == 0 and tmp == False):
tableR.append(y)
if(y == 1 and tmp == False):
tableG.append(y)
if(y == 2 and tmp == False):
tableB.append(y)
tmp = True
matrixR.append(tableR)
matrixG.append(tableG)
matrixB.append(tableB)
tableR = []
tableG = []
tableB = []
return matrixR, matrixB, matrixG
#Read lines
magic_number = feep_file.readline()
name_file = feep_file.readline()
dimension = feep_file.readline()
k = feep_file.readline()
print(magic_number)
ppm_matrix = createMatrixRGB(feep_file)
feep_file.close()
print(ppm_matrix)
The problem is that I have this result :
P3
([[]], [[]], [[]])
I don't see where I did something wrong. Thanks for your help
Upvotes: 0
Views: 285
Reputation: 32144
The main issue is not converting strings to integers, and the usage of y
as an index.
Since it's a coding practice, I am not going to just give a solution.
I will try to show you how to use the debugger to find the problem.
Place a breakpoint, run with Debug, and add x
and y
to the watch:
As you can see, the value of y
is '0'
, and the type is str
.
You can also evaluate expressions in the debugger Console (while debugging).
Paste the expression: y == 0 and tmp == False
:
As you can see, the expression: y == 0 and tmp == False
evaluates to False
.
You may run a single step, and notice that the cursor doesn't step to tableR.append(y)
(but skip to if(y == 1 and tmp == False)
), because the condition is False
.
The reason we are getting False
is that y
== '0'
, but not 0
.
Now evaluate the expression after converting y
to int
:
int(y) == 0 and tmp == False
:
As you can see int(y) == 0
is True
.
After all that...
I don't think you have meant to use the value of y
.
I think you meant to use the index of y
in x
.
[Note: Your conventions are wrong - normally y
is the line, and x
is the position in the line, but I kept your names].
Corrected code (at least partially corrected):
#Read file
feep_file = open("feep.ascii.ppm", "r")
def createMatrixRGB(filename):
matrixR = []
matrixG = []
matrixB = []
tableR = []
tableG = []
tableB = []
for lines in filename:
x = lines.split()
color_index = 0
for y in x:
if color_index % 3 == 0: # Use color_index modulo 3 (like counting 0, 1, 2, 0, 1, 2...)
tableR.append(int(y))
if color_index % 3 == 1:
tableG.append(int(y))
if color_index % 3 == 2:
tableB.append(int(y))
color_index += 1
matrixR.append(tableR)
matrixG.append(tableG)
matrixB.append(tableB)
tableR = []
tableG = []
tableB = []
return matrixR, matrixB, matrixG
#Read lines
magic_number = feep_file.readline()
name_file = feep_file.readline()
dimension = feep_file.readline()
k = feep_file.readline()
print(magic_number)
ppm_matrix = createMatrixRGB(feep_file)
feep_file.close()
print(ppm_matrix)
Result:
([[0, 0, 0, 15], [0, 0, 0, 0], [0, 0, 0, 0], [15, 0, 0, 0]], [[0, 0, 0, 15], [0, 7, 0, 0], [0, 0, 7, 0], [15, 0, 0, 0]], [[0, 0, 0, 0], [0, 15, 0, 0], [0, 0, 15, 0], [0, 0, 0, 0]])
Upvotes: 1