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

Hello, I was wondering if it was possible to write a function in python or jytho

ID: 3672707 • Letter: H

Question

Hello,

I was wondering if it was possible to write a function in python or jython that would allow me to mirror any picture across it's diagonal line for e.g. mirroring across the points (0,0) and (width,height). The image can be any size and can be rectanglar shape. I can do it if it is a square shape with this code but I cannot do a perfect mirror diagonally with rectangle pictures and it skips a section. Here is my code:

def mirror1():
picture=makePicture(pickAFile())
width=getWidth(picture)
height=getHeight(picture)
#needed this if statement so it mirrors from a square shape
if height>getWidth(picture):
height=getWidth(picture)

for x in range(0,width):
for y in range(0,height):
   oldPixel=getPixel(picture,x,y)
   newPixel=getPixel(picture,y,x)
   color=getColor(newPixel)
   setColor(oldPixel,color)
show(picture)

As mentioned earlier it needs to be able to mirror any size image across its diagonal perfect and not be restricted to squares. Any help would be greatly appreciated. Thanks.

Explanation / Answer

See the below python code which will help to you update or modify or use the below code will update the mirror any picture across it's diagonal line

def diagMirrorPicture(picture):

height = getHeight(picture)
width = getWidth(picture)

if (height != width):
printNow("Error: The input image is not squared. Found [" +
str(width) + " x " + str(height) + "] !")
return None

newPicture = makeEmptyPicture(width, height)

mirrorPt = 0
for x in range(0, width, 1):
for y in range(mirrorPt, height, 1):
sourcePixel = getPixel(picture, x, y)
color = getColor(sourcePixel)

# Copy bottom-left as is
targetPixel = getPixel(newPicture, x, y)
setColor(targetPixel, color)

# Mirror bottom-left to top right
targetPixel = getPixel(newPicture, y, x)
# ^^^^ (simply invert x and y)
setColor(targetPixel, color)

# Here we shift the mirror point
mirrorPt += 1

return newPicture


file = pickAFile()
picture = makePicture(file)

picture = diagMirrorPicture(picture)

if (picture):
writePictureTo(picture, "Path")
show(picture)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote