2017-05-08 69 views
0

我將一些圖像加載到Django模型中。在Django模板中旋轉的圖像

當我通過Django模板顯示這些圖像時,會旋轉人像圖像。如果我通過點擊它們顯示的鏈接,通過Django Admin查看這些相同的圖像,就像我期望的那樣。

這是我的看法加載圖片:

def upload_profile(request, user_id): 
    variables = {} 
    if request.POST: 
     user_form = UserForm(request.POST, instance=User.objects.get(id=request.user.id)) 
     myuser_form = MyUserForm(request.POST, request.FILES, instance=MyUser.objects.get(user__id=request.user.id)) 

     if user_form.is_valid() and myuser_form.is_valid(): 
      user_form.save() 
      myuser = myuser_form.save() 
      myuser.photo = apply_orientation(myuser.photo) 
      myuser.save() 

      return game_selection(request) 

    variables['user_form'] = user_form 
    variables['myuser_form'] = myuser_form 

    return render(request, 'esc/profile.html', variables) 

而且這是我正在申請轉動:

def flip_horizontal(im): return im.transpose(Image.FLIP_LEFT_RIGHT) 
def flip_vertical(im): return im.transpose(Image.FLIP_TOP_BOTTOM) 
def rotate_180(im): return im.transpose(Image.ROTATE_180) 
def rotate_90(im): return im.transpose(Image.ROTATE_90) 
def rotate_270(im): return im.transpose(Image.ROTATE_270) 
def transpose(im): return rotate_90(flip_horizontal(im)) 
def transverse(im): return rotate_90(flip_vertical(im)) 
orientation_funcs = [None, 
       lambda x: x, 
       flip_horizontal, 
       rotate_180, 
       flip_vertical, 
       transpose, 
       rotate_270, 
       transverse, 
       rotate_90 
       ] 

def apply_orientation(im): 
    """ 
    Extract the oritentation EXIF tag from the image, which should be a PIL Image instance, 
    and if there is an orientation tag that would rotate the image, apply that rotation to 
    the Image instance given to do an in-place rotation. 

    :param Image im: Image instance to inspect 
    :return: A possibly transposed image instance 
    """ 

    try: 
     kOrientationEXIFTag = 0x0112 
     if hasattr(im, '_getexif'): # only present in JPEGs 
      e = im._getexif()  # returns None if no EXIF data 
      if e is not None: 
       #log.info('EXIF data found: %r', e) 
       orientation = e[kOrientationEXIFTag] 
       f = orientation_funcs[orientation] 
       return f(im) 
    except: 
     # We'd be here with an invalid orientation value or some random error? 
     pass # log.exception("Error applying EXIF Orientation tag") 
    return im 

我的代碼似乎從來沒有過的情況if hasattr(im, '_getexif'):

****編輯****

所以它沒有通過bec無論我加載什麼圖像,都沒有'_getexif'。

如果我添加print getattr(im, '_getexif')我碰到下面的錯誤,所有的圖像

「ImageFieldFile」對象有「_getexif」

+0

試試這個一旦if(im。'_ getexif') – Exprator

+0

這給出了一個無效的語法錯誤 – HenryM

+0

你試過getattr(object,name)嗎?並嘗試打印(IM),看看圖像是否通過 – Exprator

回答

0

所以我放棄了上面的所有代碼,取而代之的是無屬性以下

from imagekit.models import ImageSpecField 
from imagekit.processors import ResizeToFill, Transpose, SmartResize 

class MyUser(models.Model): 
    avatar = models.ImageField(upload_to='upload/avatars', max_length=255, blank=True, null=True) 
    avatar_thumbnail = ImageSpecField(
    source='avatar', 
    processors = [Transpose(),SmartResize(200, 200)], 
    format = 'JPEG', 
    options = {'quality': 75} 
) 

它的工作原理