Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Image Flipper You are to write a program USING PYTHON which “flips” the “pixels”

ID: 3861133 • Letter: I

Question

Image Flipper

You are to write a program USING PYTHON which “flips” the “pixels” of ascii-art images. ascii-art is an image that is created by assembling rows and columns of characters to resemble an image. Below are two examples. The first is an ascii “picture” of a person. This is saved in a file named person.txt:

And this image is named linux.txt, and it depicts the penguin logo for linux:

Notice that each of these images has the exact same number of characters on each row. This is important, because rows in an image must be all of the same length for it to appear “square.” In some cases, this means padding the ends of the lines with space characters. Your program should work on any ascii image file (with any width and height), so don’t just test with these two. We will use these two as a running example for this assignment spec.

You will write a program that flips the characters in these images in various ways. You program will take three inputs. The first shall be the name of the file containing the “image” to flip. The second will be the name of a (new) file to save the flipped image into. The third will be either “lr” or “ud” (indicating whether to flip the image “left-right” or “up-down”) You should use the input() function to grab these values. An example of reading these values in looks like the following:

Select an image file: res/person.txt

Select an output file: flippedperson.txt

Select a direction (lr, ud): lr

...

Your program must ensure that the third input is lr or ud. If not, it should print a warning message, and then ask for the input again. It should do this until a correct input is provided.

Flipping person.txt and linux.txt in lr mode would result in the output files having these contents (respectively):

               

Flipping them both in ud mode would result in these contents:

   

Note that flipping an image in the same direction twice in a row should result in the exact same image as what was started with. I suggest you use this technique to ensure that you are flipping correctly.

When implementing this, you may only use the following built-in list and string functions:

len()

list()

append()

No others should be used! You man not, use reverse, or any others. You must use 2D lists to represent the rows and columns of the text images!

#00#

Explanation / Answer

Code:

inFileName = raw_input("Please enter input file name :")
outFileName = raw_input("Please enter output file name :")
flip = raw_input("Please enter your flip pattern(ud/lr):")
a = open(inFileName,"r")
b = a.readlines()
l2d = []
for i in b:
l2d.append(i[:-2])
  
flippeds = ""
if (flip == "lr"):
fliplrs = ""
for j in range(len(l2d)):
fliplrs += l2d[j][::-1]+" "
flippeds = fliplrs
elif(flip == "ud"):
flipuds = ""
for j in range(len(l2d)-1,0,-1):
flipuds += l2d[j]+" "
flipuds += l2d[0]
flippeds = flipuds
  
aa = open(outFileName,"a+")
aa.write(flippeds)
aa.close()
a.close()

Output:

input.txt

#######
# 0 0 #
# v # %
# o # #
####### #
# #   
########
# # #   
# # #   
# # #   
% # #   
#####   
## ##   
## ##   
## ##   
000 000

output.txt

#######
# 0 0 #
% # v #
# # o #
# #######
# #   
########   
# # #   
# # #
# # #
# # %
#####   
## ##   
## ##
## ##   
000 000