The implementation method of Python to read picture attribute information

Source: Internet
Author: User
Tags print print python script
This article is to use the Python script to read the picture information, several instructions are as follows:

1. Error handling not implemented

2, not read all information, probably only GPS information, image resolution, picture pixels, equipment vendors, shooting equipment, etc.

3, simple changes should be able to achieve violence to modify the image of GPS information

4, but for itself without GPS information picture, the implementation is very complex, need to carefully calculate the offset of each descriptor

After the script runs, the read results are as follows

This is exactly what the Windows property viewer reads

The source code is as follows

#-*-Coding:utf-8-*-import Binascii class Parsemethod (object): @staticmethod def parse_default (f, Count, offset): p @staticmethod def parse_latitude (f, Count, offset): Old_pos = F.tell () F.seek (+ offset) latitude = [0, 0,0] for I in Xrange (count): Byte = F.read (4) numerator = Byte.encode (' hex ') byte = F.read (4) Deno Minator = Byte.encode (' hex ') latitude[i] = float (int (numerator, +))/int (denominator, +) print ' latitude:\t% .2f%.2f\ '%.2f\ "'% (Latitude[0], latitude[1], latitude[2]) F.seek (old_pos) @staticmethod def parse_longtitude (f , count, offset): Old_pos = F.tell () F.seek (+ offset) longtitude = [0,0,0] for I in Xrange (count): by Te = F.read (4) numerator = Byte.encode (' hex ') byte = F.read (4) denominator = Byte.encode (' hex ') long Titude[i] = float (int (numerator))/int (denominator, +) print ' longtitude:\t%.2f%.2f\ '%.2f\ '% (longtitude[0] , Longtitude[1], longtitUDE[2]) F.seek (old_pos) @staticmethod def parse_make (f, Count, offset): Old_pos = F.tell () F.seek (+ offset ) byte = F.read (count) a = Byte.encode (' hex ') print ' make:\t\t ' + Binascii.a2b_hex (a) F.seek (Old_pos) @stat ICMethod def Parse_model (f, Count, offset): Old_pos = F.tell () f.seek (+ + offset) byte = F.read (count) a = b Yte.encode (' hex ') print ' model:\t\t ' + Binascii.a2b_hex (a) F.seek (old_pos) @staticmethod def parse_datetime (  F, Count, offset): Old_pos = F.tell () F.seek (+ offset) byte = F.read (count) a = Byte.encode (' hex ') print ' Datetime:\t ' + Binascii.a2b_hex (a) F.seek (old_pos) # Rational data type, @staticmethod def parse_xresolution (f , count, offset): Old_pos = F.tell () f.seek (+ + offset) byte = F.read (4) numerator = Byte.encode (' hex ') b Yte = F.read (4) denominator = Byte.encode (' hex ') xre = Int (numerator, +)/int (denominator, +) print ' Xresolut Ion:\t ' + str (xre) + ' dPi ' F.seek (old_pos) @staticmethod def parse_yresolution (f, Count, offset): Old_pos = F.tell () F.seek (+ offs ET) byte = F.read (4) numerator = Byte.encode (' hex ') byte = F.read (4) denominator = Byte.encode (' hex ') xre = Int (numerator, +)/int (denominator, +) print ' yresolution:\t ' + str (xre) + ' dpi ' F.seek (old_pos) @staticmet Hod def parse_exif_ifd (f, Count, offset): Old_pos = F.tell () f.seek (+ + offset) byte = F.read (2) A = BYTE.E  Ncode (' hex ') exif_ifd_number = Int (A, +) for I in Xrange (exif_ifd_number): Byte = F.read (2) tag_id =       Byte.encode (' hex ') #print tag_id, byte = F.read (2) Type_n = Byte.encode (' hex ') #print Type_n, byte = F.read (4) Count = Byte.encode (' hex ') #print count, byte = F.read (4) Value_offset = Byte.encode (' hex ') #print value_offset value_offset = Int (value_offset, +) Exif_ifd_dict.get (tag_id, Parsemethod.par Se_default) (F, Count, VALue_offset) F.seek (old_pos) @staticmethod def parse_x_pixel (f, Count, value): print ' x pixels:\t ' + str (value) @staticmethod def parse_y_pixel (f, Count, value): print ' y pixels:\t ' + str (value) @staticmethod def parse_gps_i      FD (F, Count, offset): Old_pos = F.tell () F.seek (+ offset) byte = F.read (2) A = Byte.encode (' hex ')      Gps_ifd_number = Int (A, +) for I in Xrange (gps_ifd_number): Byte = F.read (2) tag_id = Byte.encode (' hex ')      #print tag_id, byte = F.read (2) Type_n = Byte.encode (' hex ') #print type_n, byte = F.read (4) Count = Byte.encode (' hex ') #print count, byte = F.read (4) Value_offset = Byte.encode (' hex ') #print VA Lue_offset count = Int (count, value_offset) = Int (value_offset, +) Gps_ifd_dict.get (tag_id, Parsemetho D.parse_default) (F, Count, Value_offset) F.seek (old_pos) ifd_dict = {' 010f ': parsemethod.parse_make, ' 0110 ': Pa Rsemethod.parse_model, ' 0132 ': parsemethod.parse_datetime, ' 011a ': parsemethod.parse_xresolution, ' 011b ': parsemethod.parse_yresolution, ' 8769 ': parsemethod.parse_exif_ifd, ' 8825 ': parsemethod.parse_gps_ifd} exif_ifd_dict = {' a002 ': parsemethod.parse _x_pixel, ' a003 ': parsemethod.parse_y_pixel} gps_ifd_dict = {' 0002 ': parsemethod.parse_latitude, ' 0004 ': ParseMet  Hod.parse_longtitude} with open (' image.jpg ', ' RB ') as F:byte = F.read (2) A = Byte.encode (' hex ') print ' SOI marker:\t ' + A byte = F.read (2) A = Byte.encode (' hex ') print ' APP1 marker:\t ' + a byte = F.read (2) A = Byte.encode (' hex ') pr int ' APP1 length:\t ' + str (int (A, 16)) + '. Dec ' byte = F.read (4) A = Byte.encode (' hex ') print ' identifier:\t ' + Binascii.a2b_hex (a) byte = F.read (2) A = byte. Encode (' hex ') print ' pad:\t\t ' + a print print ' Begin to print Header .... ' Print ' APP1 Body: ' byte = F.read (2) A = Byte.encode (' hex ') print ' byte order:\t ' + a byte = F.read (2) A = Byt E.encode (' hex ') print ' 42:\t\t ' + a byte = F.read (4) A = Byte.encode (' hex ') print ' 0th IFD offset:\t ' + a print ' Finish print Header ' print ' Begin to print 0th IFD ... ' Print #print ' total: ', byte = F.read (2) A = Byte.encode (' hex ') interoperability_ Number = Int (A, b) #print interoperability_number for I in Xrange (interoperability_number): Byte = F.read (2) TA  g_id = Byte.encode (' hex ') #print tag_id, byte = F.read (2) Type_n = Byte.encode (' hex ') #print Type_n, byte = F.read (4) Count = Byte.encode (' hex ') #print count, byte = F.read (4) Value_offset = Byte.encode (' hex ') # Print Value_offset count = Int (count, value_offset) = Int (value_offset, +) # Simulate switch ifd_dict.ge T (tag_id, Parsemethod.parse_default) (F, Count, Value_offset) print print ' Finish print 0th IFD .... '

Summarize

Using Python to read the image attribute information The realization method to this basically ended, everybody learned? I hope this article will bring some help to our study or work.

Related Article

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.