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)
python怎么读文件
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。我需要的是⼀个⽂件就能搞定读和写的库,了半天终于发现了,操作起来⼗分⽅便。废话少说,直接贴代码。我添加了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 = _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 = _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:
.geo
.interop
.interop
.exif.<tagname>
.exif.makernote.<tagname>
.thumbnail
img.flashpix.<...>
img.jfif.<tagname>
E.g:
try:
if.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.fd = fd
assert mode in ["rw", "ro"]
if not self.data is None:
self.parse_data(data)
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 = _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 = ExifType(2, "ascii", 1).id
SHORT = ExifType(3, "short", 2).id
LONG = ExifType(4, "long", 4).id
RATIONAL = ExifType(5, "rational", 8).id
UNDEFINED = ExifType(7, "undefined", 1).id
SLONG = ExifType(9, "slong", 4).id
SRATIONAL = ExifType(10, "srational", 8).id
def exif_type_size(exif_type):
"""Return the size of a type"""
return (exif_type).size
class Rational:
"""A simple fraction class. Python 2.6 could use the inbuilt Fraction class."""
def __init__(self, num, den):
"""Create a number fraction num/den."""
self.num = num
self.den = den
def __repr__(self):
"""Return a string representation of the fraction."""
return "%s / %s" % (self.num, self.den)
def as_tuple(self):
"""Return the fraction a numerator, denominator tuple."""
return (self.num, self.den)
class IfdData:
"""Base class for IFD"""
name = "Generic Ifd"
tags = {}
embedded_tags = {}
def special_handler(self, tag, data):
"""special_handler method can be over-ridden by subclasses
to specially handle the conversion of tags from raw format
into Python data types."""
pass
def ifd_handler(self, data):
"""ifd_handler method can be over-ridden by subclasses to
specially handle conversion of the Ifd as a whole into a
suitable python representation."""
pass
def extra_ifd_data(self, offset):
"""extra_ifd_data method can be over-ridden by subclasses
to specially handle conversion of the Python Ifd representation
back into a byte stream."""
return ""
def has_key(self, key):
return self[key] != None
def __setattr__(self, name, value):

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。