Below is a simple but not efficient method to rotate a PhotoImage
90 (right), 180 and 270 (left) degrees:
def rotate_image(img, dir):
w, h = img.width(), img.height()
if dir in ['left', 'right']:
newimg = PhotoImage(width=h, height=w)
else: # 180 degree
newimg = PhotoImage(width=w, height=h)
for x in range(w):
for y in range(h):
rgb = '#%02x%02x%02x' % img.get(x, y)
if dir == 'right': # 90 degrees
newimg.put(rgb, (h-y,x))
elif dir == 'left': # -90 or 270 degrees
newimg.put(rgb, (y,w-x))
else: # 180 degrees
newimg.put(rgb, (w-x,h-y))
return newimg
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…