2016-07-14 3 views
0

전화 응용 프로그램에서 이미지를 가져 오는 API를하고 있습니다. 이미지에는 EXIF ​​데이터가 포함되어 있으며이 이미지 중 일부는 방향 태그 (PIL thumbnail is rotating my image?에 따라)가 있습니다. 현재 저는 imagemagick/commandline에서의 morgify를 사용하여 문제를 해결합니다. 하지만 DRF 뷰에서 데이터를받은 후 PIL/Pillow를 사용하여 자동 방향 지정을 수행 할 수 있는지 (또는 이해할 수 있는지) 궁금합니다.Django rest framework - PIL로 이미지 자동 회전

--edit--

내가 http://www.django-rest-framework.org/api-guide/generic-views/

답변

0

확인에서 저장하고 삭제 후크를 사용해야처럼 보이는, 그래서 코드 (PIL thumbnail is rotating my image?에서 복사 자동 회전 코드)입니다 :

from PIL import Image, ExifTags 


def autorotate(path): 
    """ This function autorotates a picture """ 
    image = Image.open(path) 
    if hasattr(image, '_getexif'): # only present in JPEGs 
     orientation = None 
     for orientation in ExifTags.TAGS.keys(): 
      if ExifTags.TAGS[orientation] == 'Orientation': 
       break 
     e = image._getexif() # returns None if no EXIF data 
     if e is not None: 
      exif = dict(e.items()) 
      orientation = exif[orientation] 

      if orientation == 3: 
       image = image.transpose(Image.ROTATE_180) 
      elif orientation == 6: 
       image = image.transpose(Image.ROTATE_270) 
      elif orientation == 8: 
       image = image.transpose(Image.ROTATE_90) 
      image.save(path) 

class ReceivedDataList(generics.ListCreateAPIView): 
    queryset = User.objects.all() 
    serializer_class = UserSerializer 
    filter_backends = (filters.DjangoFilterBackend,) 
    filter_class = UserFilter 

    def perform_create(self, serializer): 
     instance = serializer.save() 
     autorotate(instance.photo.file.name) 
관련 문제