EXIF data in a picture

Source: Internet
Author: User

The first two days a goddess in the Python group sent a picture, though it was just a landscape photo. But it suddenly occurred to me that before I read it in the <<python绝技>> book, the exif(exchange image file format) EXIF standard defines the standard for how to store pictures and audio files, including the author of the document, the time of the modification, or even the GPS location information that may contain the photo. (In fact, more than just pictures, documents, tables, audio, video have metadata similar to EXIF).

    fromimport Image    fromimport TAGS    img = Image.open(r"1.jpg")    forin img._getexif().items():        print(TAGS.get(tag,tag),value)
程序运行结果如下:
(' ImageWidth ',5312)    (' Imagelength ',2988)    (' Jpegifbytecount ',0)    (' whitebalance ',0)    (' usercomment ',u ' \ n ')    (' Meteringmode ',65535)    (' LightSource ',0)    (' Flash ',0)    (' FocalLength ', (480, -))    (' Orientation ',0)    (' Make ',u ' Samsung ')    (' Model ',u ' sm-n9100\x00 ')    (' DateTime ',u ' 2016:05:07 15:32:37 ')    (' Jpegifoffset ',258)    (' Exifoffset ',228)

But after my various Baidu and Google, and did not find PIL modify EXIF information operation, I hope to know how to use PIL operation Exif Crossing you leave a message below. For this I had to look for other libraries, such as Piexif (HTTPS://PYPI.PYTHON.ORG/PYPI/PIEXIF).
According to official documents, PIEXIF has only five functions, which is really simple to use. This article does not serve as a document interpretation of the work, directly to a complete self-written class.

ImportPiexif fromPiexifImportImageifd# 0th fromPiexifImportExififd# Exif fromPiexifImportGpsifd# GPS fromPiexifImportInteropifd#interop class Exif(dict):group_0th ="0th"group_1st ="1st"Group_exif ="Exif"Group_gps ="GPS"Group_interop ="Interop"Group_thumbnail ="thumbnail"_unkown_tag = {"Name":"Unkown","Type":"Unkown"} def __init__(self,path):Dict.__init__ (self) self.__name = Path Self.update (piexif.load (self.__name)) def pretty_dict(self):        "" " Print exif Info" ""Ret_dict = {} forGroup,group_valueinchSelf.items (): Ret_dict.setdefault (group,{})ifGroup_value is  not None  andGroup_value: forTag,valueinchGroup_value.items (): name = Piexif. Tags[group].get (Tag,self._unkown_tag) ["Name"] Ret_dict[group][name] = valuereturnRet_dict def as_byte(self):        "" For Raw write EXIF to Image "" "        returnPiexif.dump (self) def write(self,path=none):Piexif.insert (Self.as_byte (), PathorSelf.__name) def insert_exif(self,group,tags):        "" " Group is one of Group_0th,group_1st,group_exif, Group_gps,group_interop,group_thumbnail Tags reference class imageifd,exififd,gpsifd,interopifd "" "        ifIsinstance (tags,dict): forTag,valueinchTags.items (): self[group][tag] = valueelifIsinstance (Tags,list)orIsinstance (tags,tuple): forTag,valueinchTags:self[group][tag] = value def remove_exif(self,group,tag=none):        "" " Group is one of Group_0th,group_1st,group_exif, Group_gps,group_interop,group_thumbnail Tags reference class imageifd,exififd,gpsifd,interopifd "" "        ifTag is None: Self[group] = {}elifTaginchSelf[group]:delSelf[group][tag]

is a picture header with EXIF information added:

FF D8 is the identity header of a JPG file
FF E1 EXIF Head Start flag
00 29 according to the JPEG standard, 29 (note that this is hexadecimal) is the segment content and the segment length itself but does not include the segment ID and the length of the segment type
Prefix of EXIF content (this is fixed)
4D 4D xx 2A ... FF Content
As can be seen, the EXIF information is only filled in the JPG file of an FF E1 beginning of the comment segment, filled in the file header.

EXIF data in a picture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.