You will write a program named flipper.py that “flips” the “pixels” of ascii-art
ID: 3919686 • Letter: Y
Question
You will write a program named flipper.pythat “flips” the “pixels” of ascii-art images. Ascii-art is an image that is created by assembling rows and columns or characters to resemble an image.
The first is an ascii picture of a person. This is saved in a file named person.txt
#######
# 0 0 #
# v # %
# O # #
####### #
# #
########
# # #
# # #
# # #
% #####
## ##
## ##
## ##
000 000
Notice that the image has the same exact number of characters on each row. This is important because rows in an image must all be of the same length for it to appear “square”. For the case of these files, this means padding the ends of the lines with space characters. Your program should work onanyascii image file, with any width and height), so do not just test with these two.
You will write a program that flips the characters in these images in various ways. Your program will take three inputs. The first should be the name of the file containing the image to flip. The second will be the name of the newfile to save the flipped image. The third will be either “lr” (left-right)or “ud” (up-down). You would use the input()function to get these values.
Select an image file: person.txt
Select an output file: person_lr.txt
Select a direction (lr, ud): lr
Your program must ensure that the third input is eitherlror ud. If it does not match either input, then print a warning message and then ask for the input again. It SHould do this until a correct input is provided.
Flipping person.txt in lr mode should result in the outfiles having these contents respectively:
#######
# 0 0 #
% # v #
# # O #
# #######
# #
########
# # #
# # #
# # #
##### %
## ##
## ##
## ##
000 000
#######
# 0 0 #
# v # %
# O # #
####### #
# #
########
# # #
# # #
# # #
% #####
## ##
## ##
## ##
000 000
Explanation / Answer
import sys
def flipUPDOWN(l):
l1 = []
i = len(l)-1
while i >= 0:
l1.append(l[i])
i = i -1
return l1
def flipLR(l):
lr = []
for i in range(len(l)):
l2 = l[i]
print(l2)
l3 = []
j = len(l2)-1
while j >= 0:
l3.append(l2[j])
j = j - 1
#print(l3)
lr.append(l3)
return lr
def main():
f1 = input("Select an image file: ")
f2 = input("Select an output file: ")
cmd = ""
while cmd == "":
cmd = input("Select a direction (lr,ud): ")
if cmd == "lr" or cmd == "ud":
break
else:
cmd = ""
try:
mf1 = open(f1,"r")
mf2 = open(f2,"w")
except IOError:
print("Could not open file! Please check input file ")
sys.exit()
l = []
for line in mf1:
line = line[:-1]
l.append(list(line))
l1 = []
print(l)
if cmd == "lr":
l1 = flipLR(l)
elif cmd == "ud":
l1 = flipUPDOWN(l)
if l1 != []:
for i in range(len(l1)):
st = ''.join(l1[i])
st = st + " "
mf2.write(st)
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.