The implementation method of the Python reading picture attribute information _python

Source: Internet
Author: User
Tags error handling print print

This article is about using Python scripts to read picture information, with several descriptions as follows:

1, did not implement error handling

2, did not read all the information, probably only GPS information, picture resolution, picture pixels, equipment manufacturers, shooting equipment, etc.

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

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

After the script runs, the read results are as follows


Information that the script reads

This is exactly the same as what Windows Property viewer reads.


Picture Information 1


Picture Information 2

Source code is as follows

#-*-coding:utf-8-*-Import Binascii class Parsemethod (object): @staticmethod def parse_default (f, Count, offset): Pass @staticmethod def parse_latitude (f, Count, offset): Old_pos = F.tell () F.seek (+ offset) lat Itude = [0,0,0] for I in Xrange (count): Byte = F.read (4) numerator = Byte.encode (' hex ') byte = F.re AD (4) denominator = Byte.encode (' hex ') latitude[i] = float (int (numerator))/int (denominator,) p Rint ' latitude:\t%.2f%.2f\ '%.2f\ '% (latitude[0], latitude[1], latitude[2]) F.seek (old_pos) @staticmethod D EF Parse_longtitude (F, Count, offset): Old_pos = F.tell () F.seek (+ offset) longtitude = [0,0,0] for I In Xrange (count): Byte = F.read (4) numerator = Byte.encode (' hex ') byte = F.read (4) denominator = Byte.encode (' hex ') longtitude[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_p OS = 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) @staticmethod def parse_model (f, Count, offset): Old_pos = F.tell () F.seek (1 2 + offset) byte = F.read (count) a = Byte.encode (' hex ') print ' model:\t\t ' + Binascii.a2b_hex (a) F.seek (OL D_pos) @staticmethod def parse_datetime (f, Count, offset): Old_pos = F.tell () F.seek (+ offset) by Te = F.read (count) a = Byte.encode (' hex ') print ' datetime:\t ' + Binascii.a2b_hex (a) F.seek (Old_pos) # ratio NAL data type, @staticmethod def parse_xresolution (f, Count, offset): Old_pos = F.tell () F.seek (+ offset byte = F.read (4) numerator = Byte.encode (' hex ') byte = F.read (4) denominator = Byte.encode (' hex ') x re = int (numerator, 16)/int (denominator) print ' xresolution:\t ' + str (xre) + ' dpi ' F.seek (old_pos) @staticmethod def parse_yr Esolution (F, Count, offset): Old_pos = F.tell () F.seek (+ offset) byte = F.read (4) numerator = Byte.enc

    Ode (' hex ') byte = F.read (4) denominator = Byte.encode (' hex ') xre = Int (numerator,)/int (denominator, 16) print ' yresolution:\t ' + str (xre) + ' dpi ' F.seek (old_pos) @staticmethod def parse_exif_ifd (f, count, offset) : Old_pos = F.tell () F.seek (+ offset) byte = F.read (2) A = Byte.encode (' 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 val Ue_offset Value_offset = Int (value_offset) exif_ifd_dict.get (tag_id, Parsemethod.parse_default) (F, Count, Value_offset) f.se 
  Ek (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_ifd (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 ') #pr int Value_offset count = Int (count) value_offset = Int (value_offset,) gps_ifd_dict.get (tag_id, P Arsemethod.parse_default(f, Count, Value_offset) F.seek (old_pos) ifd_dict = {' 010f ': parsemethod.parse_make, ' 0110 ': PARSEMETHOD.P Arse_model, ' 0132 ': parsemethod.parse_datetime, ' 011a ': parsemethod.parse_xresolution, ' 011b ': Parsemethod.par Se_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_lati Tude, ' 0004 ': Parsemethod.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 ') print ' 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 

  = Byte.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,) #print interoperability_number for I in Xrange (Interopera  Bility_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 count = Int (count,) value_offset = int


  (value_offset) # Simulate Switch ifd_dict.get (tag_id, Parsemethod.parse_default) (F, Count, Value_offset)Print print ' Finish print 0th IFD ... ' 

Summarize

The realization method of using Python to read the image attribute information to this is basically the end, we have learned? Hope this article for everyone's study or work to bring certain help,

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.