使用python模块plotdigitizer抠取论文图片中的数据

技术背景

对于各行各业的研究人员来说,经常会面临这样的一个问题:有一篇不错的文章里面有很好的数据,但是这个数据在文章中仅以图片的形式出现。而假如我们希望可以从该图片中提取出数据,这样就可以用我们自己的形式重新来展现这些数据,还可以额外再附上自己优化后的数据。因此从论文图片中提取数据,是一个非常实际的需求。这里以前面写的量子退火的博客为例,博客中有这样的一张图片:

在这篇文章中,我们将介绍如何使用python从图片上把数据抠取出来。

plotdigitizer的安装

这里我们使用pip来安装python第三方库plotdigitizer,该库的主要功能就是可以自动化的从图片中提取出数据,我们可以使用腾讯的pip镜像源来加速我们的安装过程:

[dechin@dechin-manjaro plotdigitizer]$ python3 -m pip install -i https://mirrors.cloud.tencent.com/pypi/simple plotdigitizer
Looking in indexes: https://mirrors.cloud.tencent.com/pypi/simple
Collecting plotdigitizer
  Downloading https://mirrors.cloud.tencent.com/pypi/packages/89/bb/ff753093458c05ce3b52fd17527b6b0622ca096aadcf561c6316320ab793/plotdigitizer-0.1.3-py3-none-any.whl (20 kB)
Collecting loguru<0.6.0,>=0.5.3
  Downloading https://mirrors.cloud.tencent.com/pypi/packages/6d/48/0a7d5847e3de329f1d0134baf707b689700b53bd3066a5a8cfd94b3c9fc8/loguru-0.5.3-py3-none-any.whl (57 kB)
     |████████████████████████████████| 57 kB 521 kB/s 
Collecting opencv-python<5.0.0,>=4.5.1
  Downloading https://mirrors.cloud.tencent.com/pypi/packages/2a/9a/ff309b530ac1b029bfdb9af3a95eaff0f5f45f6a2dbe37b3454ae8412f4c/opencv_python-4.5.1.48-cp38-cp38-manylinux2014_x86_64.whl (50.4 MB)
     |████████████████████████████████| 50.4 MB 467 kB/s 
Collecting numpy<2.0.0,>=1.19.5
  Downloading https://mirrors.cloud.tencent.com/pypi/packages/c7/e6/dccac76b7e825915ffb906beeba5a953597b6cfe1fe686b5276e122cb07c/numpy-1.20.1-cp38-cp38-manylinux2010_x86_64.whl (15.4 MB)
     |████████████████████████████████| 15.4 MB 20.4 MB/s 
Collecting matplotlib<4.0.0,>=3.3.4
  Downloading https://mirrors.cloud.tencent.com/pypi/packages/ab/20/60cfe5d611ac86df07b7b1f9b9582f22f7eda5edbe2124ba85bdf3133822/matplotlib-3.3.4-cp38-cp38-manylinux1_x86_64.whl (11.6 MB)
     |████████████████████████████████| 11.6 MB 4.4 MB/s 
Requirement already satisfied: python-dateutil>=2.1 in /home/dechin/anaconda3/lib/python3.8/site-packages (from matplotlib<4.0.0,>=3.3.4->plotdigitizer) (2.8.1)
Requirement already satisfied: cycler>=0.10 in /home/dechin/anaconda3/lib/python3.8/site-packages (from matplotlib<4.0.0,>=3.3.4->plotdigitizer) (0.10.0)
Requirement already satisfied: pillow>=6.2.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from matplotlib<4.0.0,>=3.3.4->plotdigitizer) (8.0.1)
Requirement already satisfied: kiwisolver>=1.0.1 in /home/dechin/anaconda3/lib/python3.8/site-packages (from matplotlib<4.0.0,>=3.3.4->plotdigitizer) (1.3.0)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.3 in /home/dechin/anaconda3/lib/python3.8/site-packages (from matplotlib<4.0.0,>=3.3.4->plotdigitizer) (2.4.7)
Requirement already satisfied: six>=1.5 in /home/dechin/anaconda3/lib/python3.8/site-packages (from python-dateutil>=2.1->matplotlib<4.0.0,>=3.3.4->plotdigitizer) (1.15.0)
Installing collected packages: loguru, numpy, opencv-python, matplotlib, plotdigitizer
  Attempting uninstall: numpy
    Found existing installation: numpy 1.19.2
    Uninstalling numpy-1.19.2:
      Successfully uninstalled numpy-1.19.2
  Attempting uninstall: matplotlib
    Found existing installation: matplotlib 3.3.2
    Uninstalling matplotlib-3.3.2:
      Successfully uninstalled matplotlib-3.3.2
Successfully installed loguru-0.5.3 matplotlib-3.3.4 numpy-1.20.1 opencv-python-4.5.1.48 plotdigitizer-0.1.3

通过运行帮助指令,我们可以查看是否安装成功:

[dechin@dechin-manjaro plotdigitizer]$ plotdigitizer -h
usage: plotdigitizer [-h] --data-point DATA_POINT [--location LOCATION] [--plot PLOT] [--output OUTPUT]
                     [--preprocess] [--debug]
                     INPUT

Digitize image.

positional arguments:
  INPUT                 Input image file.

optional arguments:
  -h, --help            show this help message and exit
  --data-point DATA_POINT, -p DATA_POINT
                        Datapoints (min 3 required). You have to click on them later. At least 3 points
                        are recommended. e.g -p 0,0 -p 10,0 -p 0,1 Make sure that point are comma
                        separated without any space.
  --location LOCATION, -l LOCATION
                        Location of a points on figure in pixels (integer). These values should appear in
                        the same order as -p option. If not given, you will be asked to click on the
                        figure.
  --plot PLOT           Plot the final result. Requires matplotlib.
  --output OUTPUT, -o OUTPUT
                        Name of the output file else trajectory will be written to <INPUT>.traj.csv
  --preprocess          Preprocess the image. Useful with bad resolution images.
  --debug               Enable debug logger

执行指令与输出图片

先把需要抠取数据的图片放到当前目录下,然后运行如下指令:

plotdigitizer ./test1.png -p 0,-1 -p 20,0 -p 0,0.1 --plot output.png

该指令会将test1.png中的数据提取出来,可以使用-o存储为csv格式的数据表格。这里实际使用中我们发现,即使不用plot指令,也会在Manjaro Linux系统下不断的输出打印图片,只有通过kill -9的方式才能强行将进程杀死,有可能是开源库中存在的某个bug。这里展示一下用新的数据绘制出来的效果图:

执行结束后,该图片会被输出到临时文件夹tmp/plotdigitizer/下,但是注意前面产生的图片会被后来的临时文件所覆盖。

总结概要

这里我们仅仅是介绍和演示了plotdigitizer的基本使用方法,这样一个使用python制作的图像数据工具更加符合pythoner的使用习惯和逻辑。虽然实际使用过程中工具可能出现各种各样的问题,但是基本上是一个比较好的工具,值得推荐。

版权声明

本文首发链接为:https://www.cnblogs.com/dechinphy/p/plotdigitizer.html
作者ID:DechinPhy
更多原著文章请参考:https://www.cnblogs.com/dechinphy/

posted @ 2021-03-05 20:33  DECHIN  阅读(3178)  评论(0编辑  收藏  举报