【问题标题】:Using PIL to auto rotate picture taken with cell phone and accelorometer使用OIL自动旋转手机和加速度计拍摄的照片
【发布时间】:2012-08-21 10:43:49
【问题描述】:

我在一个网络应用程序中使用 Django + PIL + Amazon boto。用户发送图片,webapp 显示它。大多数情况下,人们发送从他们的手机拍摄的照片。有时,图像以错误的方向显示。有没有办法使用 PIL 或 Django 的 ImageField 从图像中获取元信息并使用它将图像旋转到正确的方向?

【问题讨论】:

标签: python django python-imaging-library django-models


【解决方案1】:

我使用django-imagekit 处理图像,然后使用imagekit.processors.Transpose

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

class UserProfile(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}
  )

【讨论】:

    【解决方案2】:

    试试这个来获取 EXIF 信息。注意:_getexif() 方法属于 JPEG 插件。它不会存在于其他类型的图像中。

    import Image
    from PIL.ExifTags import TAGS
    
    im = Image.open('a-jpeg-file.jpg')
    exifdict = im._getexif()
    if len(exifdict):
        for k in exifdict.keys():
            if k in TAGS.keys():
                print TAGS[k], exifdict[k]
            else:
                print k, exifdict[k]
    

    对于我在硬盘上找到的随机图像,这产生了:

    ExifVersion 0221
    ComponentsConfiguration 
    ApertureValue (4312, 1707)
    DateTimeOriginal 2012:07:19 17:33:37
    DateTimeDigitized 2012:07:19 17:33:37
    41989 35
    FlashPixVersion 0100
    MeteringMode 5
    Flash 32
    FocalLength (107, 25)
    41986 0
    Make Apple
    Model iPad
    Orientation 1
    YCbCrPositioning 1
    SubjectLocation (1295, 967, 699, 696)
    SensingMethod 2
    XResolution (72, 1)
    YResolution (72, 1)
    ExposureTime (1, 60)
    ExposureProgram 2
    ColorSpace 1
    41990 0
    ISOSpeedRatings 80
    ResolutionUnit 2
    41987 0
    FNumber (12, 5)
    Software 5.1.1
    DateTime 2012:07:19 17:33:37
    41994 0
    ExifImageWidth 2592
    ExifImageHeight 1936
    ExifOffset 188
    

    这是您想要的 Orientation 值。可以找到它的含义,例如在exif orientation page

    原始 exif 数据可作为来自 Image.info['exif'] 的字符串获得。 可以使用rotate() 方法完成旋转。

    除了更改原始数据之外,我不知道使用 PIL 更改 EXIF 数据的方法。

    【讨论】:

      猜你喜欢
      • 2012-11-06
      • 1970-01-01
      • 2013-11-01
      • 2013-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-22
      • 1970-01-01
      相关资源
      最近更新 更多