使用Python而不使用PIL(包括缩略图)在EXIF中指定方向旋转图像 [英] Rotating an image with orientation specified in EXIF using Python without PIL including the thumbnail

查看:888
本文介绍了使用Python而不使用PIL(包括缩略图)在EXIF中指定方向旋转图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:


  • 我正在将iPhone中的图像连同EXIF信息发送到我的Pyhon套接字服务器。 / li>
  • 我需要根据拍摄图像时的实际方向正确定位图像。我知道IOS总是将图像保存为横向左侧,并将实际方向添加为EXIF字段(EXIF.Image.Orientation)。

  • 我正在读取EXIF字段以查看实际方向。然后我使用wxpython将图像旋转到正确的方向。

我使用pyexiv2进行EXIF操作。

I am using pyexiv2 for EXIF manipulation.

问题:EXIF信息包括使用wxpython旋转图像时丢失的缩略图。

Issue: The EXIF information incluiding the thumbnails lost while rotating the image using wxpython.

我做了什么:我之前正在阅读EXIF旋转图像。我重置了EXIF中的方向字段。然后我会在轮换后把它放回去。

What I did: I am reading the EXIF before rotating the image. I reset the orientation field in the EXIF. Then I am putting it back after rotation.

问题:

EXIF内的缩略图没有旋转。因此,图像和缩略图具有不同的方向。

The thumbnail inside the EXIF is not rotated. So, the image and thumbnail have different orientations.

问题?

是否有除PIL以外的任何模块旋转图像保留其EXIF信息?

Is there any module other than PIL to rotate an image keeping its EXIF info?

是否有单独的EXIF字段用于缩略图方向?

Is there a separate EXIF field for thumbnail orientation?

是否有我可以单独旋转缩略图吗?

Is there a way I can just rotate the Thumbnail alone?

感谢您的帮助......

Thanks for your help...

推荐答案

我找到了一个解决方案......检查出来...... http://www.atoztoa.com/2012/12/rotate-images-along-with-thumbnails-in.html

I have found a solution... check this out... http://www.atoztoa.com/2012/12/rotate-images-along-with-thumbnails-in.html

'''
Rotate Image
'''
import pyexiv2
import wx
import cStringIO
import os

def rotateImage(infile, device):
    try:
        # Read Metadata from the image
        metadata = pyexiv2.metadata.ImageMetadata(infile)
        metadata.read();

        # Let's get the orientation
        orientation = metadata.__getitem__("Exif.Image.Orientation")
        orientation = int(str(orientation).split("=")[1][1:-1])

        # Extract thumbnail
        thumb = metadata.exif_thumbnail

        angle = 0

        # Check the orientation field in EXIF and rotate image accordingly
        if device == "iPhone" or device == "iPad":
            # Landscape Left : Do nothing
            if orientation == ORIENTATION_NORMAL:
                angle = 0
            # Portrait Normal : Rotate Right
            elif orientation == ORIENTATION_LEFT:
                angle = -90
            # Landscape Right : Rotate Right Twice
            elif orientation == ORIENTATION_DOWN:
                angle = 180
            # Portrait Upside Down : Rotate Left
            elif orientation == ORIENTATION_RIGHT:
                angle = 90

            # Resetting Exif field to normal
            print "Resetting exif..."
            orientation = 1
            metadata.__setitem__("Exif.Image.Orientation", orientation)

        # Rotate
        if angle != 0:
            # Just rotating the image based on the angle
            print "Rotating image..."
            angle = math.radians(angle)
            img = wx.Image(infile, wx.BITMAP_TYPE_ANY)
            img_centre = wx.Point( img.GetWidth()/2, img.GetHeight()/2 )
            img = img.Rotate( angle, img_centre, True )
            img.SaveFile(infile, wx.BITMAP_TYPE_JPEG)

            # Create a stream out of the thumbnail and rotate it using wx
            # Save the rotated image to a temporary file
            print "Rotating thumbnail..."
            t = wx.EmptyImage(100, 100)
            thumbStream = cStringIO.StringIO(thumb.data)
            t.LoadStream(thumbStream, wx.BITMAP_TYPE_ANY)
            t_centre = wx.Point( t.GetWidth()/2, t.GetHeight()/2 )
            t = t.Rotate( angle, t_centre, True )
            t.SaveFile(infile + ".jpg", wx.BITMAP_TYPE_JPEG)
            thumbStream.close()

            # Read the rotated thumbnail and put it back in the rotated image
            thumb.data = open(infile + ".jpg", "rb").read();
            # Remove temporary file
            os.remove(infile + ".jpg")

        # Write back metadata
        metadata.write();

    except Exception, e:
        print "Error rotating image... : " + str(e)

这篇关于使用Python而不使用PIL(包括缩略图)在EXIF中指定方向旋转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆