Python读取图片属性信息的实现方法


Posted in Python onSeptember 11, 2016

本文是利用Python脚本读取图片信息,有几个说明如下:

     1、没有实现错误处理

     2、没有读取所有信息,大概只有 GPS 信息、图片分辨率、图片像素、设备商、拍摄设备等

     3、简单修改后应该能实现暴力修改图片的 GPS 信息

     4、但对于本身没有 GPS 信息的图片,实现则非常复杂,需要仔细计算每个描述符的偏移量

脚本运行后,读取结果如下

Python读取图片属性信息的实现方法
脚本读取的信息

这里和 Windows 属性查看器读到的内容完全一致

Python读取图片属性信息的实现方法
图片信息1

Python读取图片属性信息的实现方法
图片信息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....'

总结

利用Python读取图片属性信息的实现方法到这就基本结束了,大家都学会了吗?希望这篇文章对大家的学习或者工作带来一定的帮助,

Python 相关文章推荐
使用cx_freeze把python打包exe示例
Jan 24 Python
python装饰器与递归算法详解
Feb 18 Python
python字符串连接方法分析
Apr 12 Python
python中实现精确的浮点数运算详解
Nov 02 Python
Python绘制正余弦函数图像的方法
Aug 28 Python
Python模块的加载讲解
Jan 15 Python
python 实现创建文件夹和创建日志文件的方法
Jul 07 Python
Python Gluon参数和模块命名操作教程
Dec 18 Python
详解如何在pyqt中通过OpenCV实现对窗口的透视变换
Sep 20 Python
python cookie反爬处理的实现
Nov 01 Python
python读取mnist数据集方法案例详解
Sep 04 Python
Pandas-DataFrame知识点汇总
Mar 16 Python
python开发环境PyScripter中文乱码问题解决方案
Sep 11 #Python
基于asyncio 异步协程框架实现收集B站直播弹幕
Sep 11 #Python
asyncio 的 coroutine对象 与 Future对象使用指南
Sep 11 #Python
Python中使用asyncio 封装文件读写
Sep 11 #Python
Python 如何访问外围作用域中的变量
Sep 11 #Python
Python优化技巧之利用ctypes提高执行速度
Sep 11 #Python
Python 中的with关键字使用详解
Sep 11 #Python
You might like
php实现的简单压缩英文字符串的代码
2008/04/24 PHP
PHP教程之PHP中shell脚本的使用方法分享
2012/02/23 PHP
CodeIgniter控制器之业务逻辑实例分析
2016/01/20 PHP
Laravel框架基于ajax实现二级联动功能示例
2019/01/17 PHP
Laravel框架学习笔记之批量更新数据功能
2019/05/30 PHP
PHPExcel实现的读取多工作表操作示例
2020/04/14 PHP
Highslide.js是一款基于js实现的网页中图片展示插件
2020/03/30 Javascript
javascrip客户端验证文件大小及文件类型并重置上传
2011/01/12 Javascript
日期处理的js库(迷你版)--自建js库总结
2011/11/21 Javascript
js实现图片从左往右渐变切换效果的方法
2015/02/06 Javascript
apply和call方法定义及apply和call方法的区别
2015/11/15 Javascript
Node.js中npm常用命令大全
2016/06/09 Javascript
JavaScript树的深度优先遍历和广度优先遍历算法示例
2018/07/30 Javascript
vue axios请求频繁时取消上一次请求的方法
2018/11/10 Javascript
NodeJS有难度的面试题(能答对几个)
2019/10/09 NodeJs
[03:27]最受玩家喜爱奖提名:PZH_Element 致玩家寄语
2016/12/20 DOTA
[33:23]Secret vs Serenity 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
pytyon 带有重复的全排列
2013/08/13 Python
python如何查看系统网络流量的信息
2016/09/12 Python
Python 异常处理的实例详解
2017/09/11 Python
python 读取鼠标点击坐标的实例
2018/12/29 Python
django数据关系一对多、多对多模型、自关联的建立
2019/07/24 Python
使用Python的Turtle绘制哆啦A梦实例
2019/11/21 Python
Python pandas 列转行操作详解(类似hive中explode方法)
2020/05/18 Python
python subprocess pipe 实时输出日志的操作
2020/12/05 Python
BONIA波尼亚新加坡官网:皮革手袋,鞋类和配件
2016/08/25 全球购物
党员2014两会学习心得体会
2014/03/17 职场文书
小学三年级学生评语
2014/04/22 职场文书
安全生产标语
2014/06/06 职场文书
先进单位事迹材料
2014/12/25 职场文书
2015年大学宣传部工作总结
2015/05/26 职场文书
致运动员赞词
2015/07/22 职场文书
2016年小学推普宣传周活动总结
2016/04/06 职场文书
如何利用js在两个html窗口间通信
2021/04/27 Javascript
python pygame入门教程
2021/06/01 Python
django中websocket的具体使用
2022/01/22 Python