本文是利用Python腳本讀取圖片信息,有幾個說明如下:
1、沒有實現錯誤處理
2、沒有讀取所有信息,大概只有 GPS 信息、圖片分辨率、圖片像素、設備商、拍攝設備等
3、簡單修改后應該能實現暴力修改圖片的 GPS 信息
4、但對于本身沒有 GPS 信息的圖片,實現則非常復雜,需要仔細計算每個描述符的偏移量
腳本運行后,讀取結果如下
腳本讀取的信息
這里和 Windows 屬性查看器讀到的內容完全一致
圖片信息1
圖片信息2
源碼如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
# -*- 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( 12 + offset) latitude = [ 0 , 0 , 0 ] for i in xrange (count): byte = f.read( 4 ) numerator = byte.encode( 'hex' ) byte = f.read( 4 ) denominator = byte.encode( 'hex' ) latitude[i] = float ( int (numerator, 16 )) / int (denominator, 16 ) 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( 12 + 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, 16 )) / int (denominator, 16 ) 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( 12 + 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( 12 + offset) byte = f.read(count) a = byte.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( 12 + offset) byte = f.read(count) a = byte.encode( 'hex' ) print 'DateTime:\t' + binascii.a2b_hex(a) f.seek(old_pos) # rational data type, 05 @staticmethod def parse_xresolution(f, count, offset): old_pos = f.tell() f.seek( 12 + offset) byte = f.read( 4 ) numerator = byte.encode( 'hex' ) byte = f.read( 4 ) denominator = byte.encode( 'hex' ) xre = int (numerator, 16 ) / int (denominator, 16 ) print 'XResolution:\t' + str (xre) + ' dpi' f.seek(old_pos) @staticmethod def parse_yresolution(f, count, offset): old_pos = f.tell() f.seek( 12 + offset) byte = f.read( 4 ) numerator = byte.encode( 'hex' ) byte = f.read( 4 ) denominator = byte.encode( 'hex' ) xre = int (numerator, 16 ) / 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( 12 + offset) byte = f.read( 2 ) a = byte.encode( 'hex' ) exif_ifd_number = int (a, 16 ) 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, 16 ) EXIF_IFD_DICT.get(tag_id, ParseMethod.parse_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_ifd(f, count, offset): old_pos = f.tell() f.seek( 12 + offset) byte = f.read( 2 ) a = byte.encode( 'hex' ) gps_ifd_number = int (a, 16 ) 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 value_offset count = int (count, 16 ) value_offset = int (value_offset, 16 ) GPS_IFD_DICT.get(tag_id, ParseMethod.parse_default)(f, count, value_offset) f.seek(old_pos) IFD_dict = { '010f' : ParseMethod.parse_make , '0110' : ParseMethod.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' : 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, 16 ) #print interoperability_number for i in xrange (interoperability_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, 16 ) value_offset = int (value_offset, 16 ) # simulate switch IFD_dict.get(tag_id, ParseMethod.parse_default)(f, count, value_offset) print print 'Finish print 0th IFD....' |
總結
利用Python讀取圖片屬性信息的實現方法到這就基本結束了,大家都學會了嗎?希望這篇文章對大家的學習或者工作帶來一定的幫助,