MMSS19
MMSS19

Reputation: 95

.ppm image mirrored is upside down and low quality

I have this program that should mirror a .ppm image.

however, it produces an upside down mirrored image. how can I prevent it from flipping the image upside down?

code:

takePPM = input("Enter the pmm file name: ")
try:
    openFile = open(takePPM, "r")
    readFile = openFile.readlines()
except FileNotFoundError:
    print("File not found")
    exit(1)
lists = []
for row in readFile[3:]:
    lists.append(row.split())

#convert each list in lists to lists of 3-tuples
target = []
for list in lists:
    target.append([tuple(list[i:i+3]) for i in range(0,len(list),3)])
revList = []
for i in reversed(target):
    revList.append(i)

string = ''
for row in revList:
    for pixel in row:
        for value in pixel:
            string += value + ' '
    string+='\n'


newFile = open("Mirror_" + takePPM, "w")
newFile.write((readFile[0]))
newFile.write((readFile[1]))
newFile.write((readFile[2]))
newFile.write(string)

input: enter image description here

expected output: enter image description here

what I get:

enter image description here

Upvotes: 0

Views: 351

Answers (1)

Ture Pålsson
Ture Pålsson

Reputation: 6776

Well, mirroring across the horizontal is also mirroring. :-)

It looks like you are assuming that there will be one line in the input file for each row of pixels. This is dangerous; there is no guarantee that this will be the case.

Anyway, if your assumption holds, then lists will contain a list of triplets for each row. You then reverse the order of these lists, rather than reversing the order of the triplets within each row!

Upvotes: 1

Related Questions