2016-07-14 7 views
0

私は電話アプリケーションから画像を取得するAPIをやっています。画像にはEXIFデータが含まれており、これらの画像には方向タグ(PIL thumbnail is rotating my image?)があります。現在、コマンドラインでimagemagick/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) 
関連する問題