I'm attempting to rotate an image in python by copying each individual pixel into a new blank image, (I know, probably not the best way to do it but this is what I have the theoretical knowledge basis for). The original image is 360 pixels wide and 480 pixels tall. I'm getting errors throughout my code so I've pasted both relevant functions. The only time I dont get an error is when my range for y is less than three.
def rotate(img):
height = int(img.getHeight())
width = int(img.getWidth())
turn = image.EmptyImage(height, width)
xs = 0
ys = 0
for y in range(height):
for x in range(width):
px = img.getPixel(x, y)
r = px.getRed()
g = px.getGreen()
b = px.getBlue()
turnpx = image.Pixel(r, g, b)
turn.setPixel(x+(width-xs), y, turnpx)
xs+=1
return turn
def main():
img6 = image.Image("images//arch.gif")
img7 = rotate(img6)
if img7:
win7 = image.ImageWin(img7.getWidth(), img7.getHeight(), "Win7")
img7.draw(win7)
main()
The error messages I'm receiving are:
Traceback (most recent call last):
File "C:\programs\test.py", line 28, in <module>
main()
File "C:\programs\test.py", line 23, in main
img7 = rotate(img6)
File "C:\programs\test.py", line 17, in rotate
turn.setPixel(x+(width-xs), y, turnpx)
File "C:\programs\image.py", line 467, in setPILPixel
self.im.putpixel((x,y),pixel.getColorTuple())
File "C:\programs\anaconda3\Lib\site-packages\PIL\Image.py", line 2136, in putpixel
return self.im.putpixel(xy, value)
IndexError: image index out of range



EmptyImage(height, width)is correct, and notEmptyImage(width, height); (2) if you print the values ofx+(width-xs)in the loop, are they what you expect?importstatements, it certainly is not.pxintor,g,band later use it to createturnpx- you get exacltyturnpx = pxflip(),mirror(),rotate()?