python读取和写入EXIF信息

什么是EXIF信息呢?
百度百科:Exif是一种图象文件格式,它的数据存储与JPEG格式是完全相同的。实际上Exif格式就是在JPEG格式头部插入了数码照片的信息,包括拍摄时的光圈、快门、白平衡、ISO、焦距、日期时间等各种和拍摄条件以及相机品牌、型号、色彩编码、拍摄时录制的声音以及全球定位系统(GPS)、缩略图等。所有的JPEG文件以字符串“0xFFD8”开头,并以字符串“0xFFD9”结束。文件头中有一系列“0xFF??”格式的字符串,称为“标识”,用来标记JPEG文件的信息段。“0xFFD8”表示图像信息开始,“0xFFD9”表示图像信息结束,这两个标识后面没有信息,而其它标识紧跟一些信息字符。0xFFE0 — 0xFFEF之间的标识符称为“应用标记”,没有被常规JPEG文件利用,Exif正是利用这些信息串记录拍摄信息的。
逛摄影论坛时经常会看到,照片的底部包含很多其他信息,如:曝光度,光圈,焦距,快门,机身等等,这些信息就是EXIF信息,摄影爱好者可以参考这些信息提高自己的摄影技术。本文主要涉及的是如何把信息隐藏到图片中,比如一个电影地址。

首先实现一个最简单的方式,把信息直接添加到图片的头部或者尾部,直接添加到头部由于破坏了图片的数据,所以头部会出现一块黑色的区域比较明显,所以别人一下子就看出来了,效果最差。添加到尾部只是简单的增加了图片的大小,图片的数据区域并没有改变,所以如果信息量不是很大,基本是看不出来的,缺点是传到其他网站时容易被裁剪掉。下面的代码实现了把种子隐藏到图片尾部的1024字节区域。

import sys  
 
def add_info(origin_file, data_file, output_file):  
    container = open(origin_file, "rb").read()  
    data = open(data_file, "rb").read()  
    f = open(output_file, "wb")  
    f.write(container)  
    if len(data) <= 1024:  
        data = '%s%s' %(data,' '*(1024 - len(data)))  
    else:  
        raise Exception("flag data too long")  
 
    f.write(data)  
    f.close()  
 
def read_info(filename):  
    container = open(filename,"r").read()  
    print container[len(container) - 1024:len(container)].rstrip()  
 
if "__main__" == __name__:  
    try:  
        if len(sys.argv) == 4:  
            add_info(sys.argv[1], sys.argv[2], sys.argv[3])  
            read_info(sys.argv[3])  
        else :    
            print "arguments error" 
    except Exception,err :  
        print err
2. 接下来这种方式是把信息写到exif信息中,操作起来比较麻烦,也存在被裁剪的风险。但比上面风险要小很多,一般的网站不会清除图片的exif信息。网上有很多读取EXIF信息的demo,但是写入EXIF信息的比较少,很多人推荐使用pyexif2,但是这个源码安装和配置相当麻烦,直接pass。我需要的是一个文件就能搞定读和写的库,找了半天终于发现了 pexif ,操作起来十分方便。废话少说,直接贴代码。我添加了set_copyright和read_copyright函数,把电影地址信息添加到Copyright这个标识上,并尝试读取出来。这样就可以非常方便的实现在后台上传图片的时候把电影信息添加到图片里了。
#coding=utf-8  
""" 
pexif is a module which allows you to view and modify meta-data in 
JPEG/JFIF/EXIF files. 
 
The main way to use this is to create an instance of the JpegFile class. 
This should be done using one of the static factory methods fromFile, 
fromString or fromFd. 
 
After manipulating the object you can then write it out using one of the 
writeFile, writeString or writeFd methods. 
 
The get_exif() method on JpegFile returns the ExifSegment if one exists. 
 
Example: 
 
jpeg = pexif.JpegFile.fromFile("foo.jpg") 
exif = jpeg.get_exif() 
.... 
jpeg.writeFile("new.jpg") 
 
For photos that don't currently have an exef segment you can specify 
an argument which will create the exef segment if it doesn't exist. 
 
Example: 
 
jpeg = pexif.JpegFile.fromFile("foo.jpg") 
exif = jpeg.get_exif(create=True) 
.... 
jpeg.writeFile("new.jpg") 
 
The JpegFile class handles file that are formatted in something 
approach the JPEG specification (ISO/IEC 10918-1) Annex B 'Compressed 
Data Formats', and JFIF and EXIF standard. 
 
a JPEG file is made of a series of segments followed by the image 
data. In particular it should look something like: 
 
[ SOI | <arbitrary segments> | SOS | image data | EOI ] 
 
So, the library expects a Start-of-Image marker, followed 
by an arbitrary number of segment (assuming that a segment 
has the format: 
 
[ <0xFF> <segment-id> <size-byte0> <size-byte1> <data> ] 
 
and that there are no gaps between segments. 
 
The last segment must be the Start-of-Scan header, and the library 
assumes that following Start-of-Scan comes the image data, finally 
followed by the End-of-Image marker. 
 
This is probably not sufficient to handle arbitrary files conforming 
to the JPEG specs, but it should handle files that conform to 
JFIF or EXIF, as well as files that conform to neither but 
have both JFIF and EXIF application segment (which is the majority 
of files in existence!).  
 
When writing out files all segment will be written out in the order 
in which they were read. Any 'unknown' segment will be written out 
as is. Note: This may or may not corrupt the data. If the segment 
format relies on absolute references then this library may still 
corrupt that segment! 
 
Can have a JpegFile in two modes: Read Only and Read Write. 
 
Read Only mode: trying to access missing elements will result in 
an AttributeError. 
 
Read Write mode: trying to access missing elements will automatically 
create them. 
 
E.g:  
 
img.exif.primary.<tagname> 
             .geo 
             .interop 
             .exif.<tagname> 
             .exif.makernote.<tagname> 
 
        .thumbnail 
img.flashpix.<...> 
img.jfif.<tagname> 
img.xmp 
 
E.g:  
 
try: 
 print img.exif.tiff.exif.FocalLength 
except AttributeError: 
 print "No Focal Length data" 
 
""" 
 
import StringIO  
import sys  
from struct import unpack, pack  
 
MAX_HEADER_SIZE = 64 * 1024 
DELIM = 0xff 
EOI = 0xd9 
SOI_MARKER = chr(DELIM) + '\xd8' 
EOI_MARKER = chr(DELIM) + '\xd9' 
 
EXIF_OFFSET = 0x8769 
GPSIFD = 0x8825 
 
TIFF_OFFSET = 6 
TIFF_TAG = 0x2a 
 
DEBUG = 0 
 
def debug(*debug_string):  
    """Used for print style debugging. Enable by setting the global 
    DEBUG to 1.""" 
    if DEBUG:  
        for each in debug_string:  
            print each,  
        print 
 
class DefaultSegment:  
    """DefaultSegment represents a particluar segment of a JPEG file. 
    This class is instantiated by JpegFile when parsing Jpeg files 
    and is not intended to be used directly by the programmer. This 
    base class is used as a default which doesn't know about the internal 
    structure of the segment. Other classes subclass this to provide 
    extra information about a particular segment. 
    """ 
 
    def __init__(self, marker, fd, data, mode):  
        """The constructor for DefaultSegment takes the marker which 
        identifies the segments, a file object which is currently positioned 
        at the end of the segment. This allows any subclasses to potentially 
        extract extra data from the stream. Data contains the contents of the 
        segment.""" 
        self.marker = marker  
        self.data = data  
        self.mode = mode  
        self.fd = fd  
        assert mode in ["rw", "ro"]  
        if not self.data is None:  
            self.parse_data(data)  
 
    class InvalidSegment(Exception):  
        """This exception may be raised by sub-classes in cases when they 
        can't correctly identify the segment.""" 
        pass 
 
    def write(self, fd):  
        """This method is called by JpegFile when writing out the file. It 
        must write out any data in the segment. This shouldn't in general be 
        overloaded by subclasses, they should instead override the get_data() 
        method.""" 
        fd.write('\xff')  
        fd.write(pack('B', self.marker))  
        data = self.get_data()  
        fd.write(pack('>H', len(data) + 2))  
        fd.write(data)  
 
    def get_data(self):  
        """This method is called by write to generate the data for this segment. 
        It should be overloaded by subclasses.""" 
        return self.data  
 
    def parse_data(self, data):  
        """This method is called be init to parse any data for the segment. It 
        should be overloaded by subclasses rather than overloading __init__""" 
        pass 
 
    def dump(self, fd):  
        """This is called by JpegFile.dump() to output a human readable 
        representation of the segment. Subclasses should overload this to provide 
        extra information.""" 
        print >> fd, " Section: [%5s] Size: %6d" % \  
              (jpeg_markers[self.marker][0], len(self.data))  
 
class StartOfScanSegment(DefaultSegment):  
    """The StartOfScan segment needs to be treated specially as the actual 
    image data directly follows this segment, and that data is not included 
    in the size as reported in the segment header. This instances of this class 
    are created by JpegFile and it should not be subclassed. 
    """ 
    def __init__(self, marker, fd, data, mode):  
        DefaultSegment.__init__(self, marker, fd, data, mode)  
 
        # For SOS we also pull out the actual data  
        img_data = fd.read()  
        # -2 accounts for the EOI marker at the end of the file  
        self.img_data = img_data[:-2]  
        fd.seek(-2, 1)  
 
    def write(self, fd):  
        """Write segment data to a given file object""" 
        DefaultSegment.write(self, fd)  
        fd.write(self.img_data)  
 
    def dump(self, fd):  
        """Dump as ascii readable data to a given file object""" 
        print >> fd, " Section: [  SOS] Size: %6d Image data size: %6d" % \  
              (len(self.data), len(self.img_data))  
 
class ExifType:  
    """The ExifType class encapsulates the data types used 
    in the Exif spec. These should really be called TIFF types 
    probably. This could be replaced by named tuples in python 2.6.""" 
    lookup = {}  
 
    def __init__(self, type_id, name, size):  
        """Create an ExifType with a given name, size and type_id""" 
        self.id = type_id  
        self.name = name  
        self.size = size  
        ExifType.lookup[type_id] = self 
 
BYTE = ExifType(1, "byte", 1).id 
ASCII = E
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值