2016-12-20 6 views
1

インポートされたイメージをtkinterキャンバス上で簡単に回転する方法はありますか?私はむしろPILモジュールを使用したくないですが、私は実行可能な選択肢を見つけることができません。 (便利な場合は、交差点を回って車の画像を回転させたい)PILを使わずにキャンバス上の画像を回転するには?

+2

tkinterはイメージを回転できません。次のような回転した車で画像を作成することができます。http://opengameart.org/sites/default/files/forum-attachments/20160613225504.jpgそしてPILなしで使用できます。 – furas

答えて

0

以下は、回転の簡単な方法ですが、PhotoImage(右)、180と270 :

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 
関連する問題