
Python讀取圖片屬性信息的實(shí)現(xiàn)方法
這篇文章介紹了利用Python讀取圖片屬性信息的方法,讀取的內(nèi)容包括GPS 信息、圖片分辨率、圖片像素、設(shè)備商、拍攝設(shè)備等,有需要的朋友們可以參考借鑒。
本文是利用Python腳本讀取圖片信息,有幾個(gè)說明如下:
1、沒有實(shí)現(xiàn)錯(cuò)誤處理
2、沒有讀取所有信息,大概只有 GPS 信息、圖片分辨率、圖片像素、設(shè)備商、拍攝設(shè)備等
3、簡(jiǎn)單修改后應(yīng)該能實(shí)現(xiàn)暴力修改圖片的 GPS 信息
4、但對(duì)于本身沒有 GPS 信息的圖片,實(shí)現(xiàn)則非常復(fù)雜,需要仔細(xì)計(jì)算每個(gè)描述符的偏移量
腳本運(yùn)行后,讀取結(jié)果如下
腳本讀取的信息
這里和 Windows 屬性查看器讀到的內(nèi)容完全一致
圖片信息2
源碼如下
# -*- 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....'
總結(jié)
利用Python讀取圖片屬性信息的實(shí)現(xiàn)方法到這就基本結(jié)束了,大家都學(xué)會(huì)了嗎?希望這篇文章對(duì)大家的學(xué)習(xí)或者工作帶來一定的幫助,
數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼
若不方便掃碼,搜微信號(hào):CDAshujufenxi
LSTM 模型輸入長(zhǎng)度選擇技巧:提升序列建模效能的關(guān)鍵? 在循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)家族中,長(zhǎng)短期記憶網(wǎng)絡(luò)(LSTM)憑借其解決長(zhǎng)序列 ...
2025-07-11CDA 數(shù)據(jù)分析師報(bào)考條件詳解與準(zhǔn)備指南? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代浪潮下,CDA 數(shù)據(jù)分析師認(rèn)證愈發(fā)受到矚目,成為眾多有志投身數(shù) ...
2025-07-11數(shù)據(jù)透視表中兩列相乘合計(jì)的實(shí)用指南? 在數(shù)據(jù)分析的日常工作中,數(shù)據(jù)透視表憑借其強(qiáng)大的數(shù)據(jù)匯總和分析功能,成為了 Excel 用戶 ...
2025-07-11尊敬的考生: 您好! 我們誠(chéng)摯通知您,CDA Level I和 Level II考試大綱將于 2025年7月25日 實(shí)施重大更新。 此次更新旨在確保認(rèn) ...
2025-07-10BI 大數(shù)據(jù)分析師:連接數(shù)據(jù)與業(yè)務(wù)的價(jià)值轉(zhuǎn)化者? ? 在大數(shù)據(jù)與商業(yè)智能(Business Intelligence,簡(jiǎn)稱 BI)深度融合的時(shí)代,BI ...
2025-07-10SQL 在預(yù)測(cè)分析中的應(yīng)用:從數(shù)據(jù)查詢到趨勢(shì)預(yù)判? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代,預(yù)測(cè)分析作為挖掘數(shù)據(jù)潛在價(jià)值的核心手段,正被廣泛 ...
2025-07-10數(shù)據(jù)查詢結(jié)束后:分析師的收尾工作與價(jià)值深化? ? 在數(shù)據(jù)分析的全流程中,“query end”(查詢結(jié)束)并非工作的終點(diǎn),而是將數(shù) ...
2025-07-10CDA 數(shù)據(jù)分析師考試:從報(bào)考到取證的全攻略? 在數(shù)字經(jīng)濟(jì)蓬勃發(fā)展的今天,數(shù)據(jù)分析師已成為各行業(yè)爭(zhēng)搶的核心人才,而 CDA(Certi ...
2025-07-09【CDA干貨】單樣本趨勢(shì)性檢驗(yàn):捕捉數(shù)據(jù)背后的時(shí)間軌跡? 在數(shù)據(jù)分析的版圖中,單樣本趨勢(shì)性檢驗(yàn)如同一位耐心的偵探,專注于從單 ...
2025-07-09year_month數(shù)據(jù)類型:時(shí)間維度的精準(zhǔn)切片? ? 在數(shù)據(jù)的世界里,時(shí)間是最不可或缺的維度之一,而year_month數(shù)據(jù)類型就像一把精準(zhǔn) ...
2025-07-09CDA 備考干貨:Python 在數(shù)據(jù)分析中的核心應(yīng)用與實(shí)戰(zhàn)技巧? ? 在 CDA 數(shù)據(jù)分析師認(rèn)證考試中,Python 作為數(shù)據(jù)處理與分析的核心 ...
2025-07-08SPSS 中的 Mann-Kendall 檢驗(yàn):數(shù)據(jù)趨勢(shì)與突變分析的有力工具? ? ? 在數(shù)據(jù)分析的廣袤領(lǐng)域中,準(zhǔn)確捕捉數(shù)據(jù)的趨勢(shì)變化以及識(shí)別 ...
2025-07-08備戰(zhàn) CDA 數(shù)據(jù)分析師考試:需要多久?如何規(guī)劃? CDA(Certified Data Analyst)數(shù)據(jù)分析師認(rèn)證作為國(guó)內(nèi)權(quán)威的數(shù)據(jù)分析能力認(rèn)證 ...
2025-07-08LSTM 輸出不確定的成因、影響與應(yīng)對(duì)策略? 長(zhǎng)短期記憶網(wǎng)絡(luò)(LSTM)作為循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的一種變體,憑借獨(dú)特的門控機(jī)制,在 ...
2025-07-07統(tǒng)計(jì)學(xué)方法在市場(chǎng)調(diào)研數(shù)據(jù)中的深度應(yīng)用? 市場(chǎng)調(diào)研是企業(yè)洞察市場(chǎng)動(dòng)態(tài)、了解消費(fèi)者需求的重要途徑,而統(tǒng)計(jì)學(xué)方法則是市場(chǎng)調(diào)研數(shù) ...
2025-07-07CDA數(shù)據(jù)分析師證書考試全攻略? 在數(shù)字化浪潮席卷全球的當(dāng)下,數(shù)據(jù)已成為企業(yè)決策、行業(yè)發(fā)展的核心驅(qū)動(dòng)力,數(shù)據(jù)分析師也因此成為 ...
2025-07-07剖析 CDA 數(shù)據(jù)分析師考試題型:解鎖高效備考與答題策略? CDA(Certified Data Analyst)數(shù)據(jù)分析師考試作為衡量數(shù)據(jù)專業(yè)能力的 ...
2025-07-04SQL Server 字符串截取轉(zhuǎn)日期:解鎖數(shù)據(jù)處理的關(guān)鍵技能? 在數(shù)據(jù)處理與分析工作中,數(shù)據(jù)格式的規(guī)范性是保證后續(xù)分析準(zhǔn)確性的基礎(chǔ) ...
2025-07-04CDA 數(shù)據(jù)分析師視角:從數(shù)據(jù)迷霧中探尋商業(yè)真相? 在數(shù)字化浪潮席卷全球的今天,數(shù)據(jù)已成為企業(yè)決策的核心驅(qū)動(dòng)力,CDA(Certifie ...
2025-07-04CDA 數(shù)據(jù)分析師:開啟數(shù)據(jù)職業(yè)發(fā)展新征程? ? 在數(shù)據(jù)成為核心生產(chǎn)要素的今天,數(shù)據(jù)分析師的職業(yè)價(jià)值愈發(fā)凸顯。CDA(Certified D ...
2025-07-03