GithubHelp home page GithubHelp logo

dilawar / plotdigitizer Goto Github PK

View Code? Open in Web Editor NEW
110.0 8.0 21.0 1.68 MB

A Python utility to digitize plots.

License: GNU General Public License v3.0

Python 98.11% Makefile 1.89%
digitization data-extraction python3 image-processing

plotdigitizer's Introduction

Python application PyPI version DOI

A Python3 command line utility to digitize plots in batch mode.

This utility is useful when you have a lot of similar plots such as EEG, ECG recordings. See examples below.

For one-off use cases, you will find web-based digitizer WebPlotDigitizer by Ankit Rohatagi much more easier to use.

Installation

$ python3 -m pip install plotdigitizer
$ plotdigitizer --help

Preparing image

Crop the image and leave only axis and trajectories. I use gthumb utility on Linux. You can also use imagemagick or gimp.

Following image is from MacFadden and Koshland, PNAS 1990 after trimming. One can also remove top and right axis.

Trimmed image

Run

plotdigitizer ./figures/trimmed.png -p 0,0 -p 10,0 -p 0,1

We need at least three points (-p option) to map axes onto the image. In the example above, these are 0,0 (where x-axis and y-axis intesect) , 10,0 (a point on x-axis) and 0,1 (a point on y-axis). To map these points on the image, you will be asked to click on these points on the image. Make sure to click in the same order and click on the points as precisely as you could. Any error in this step will propagate. If you don't have 0,0 in your image, you have to provide 4 points: 2 on x-axis and 2 on y-axis.

The data-points will be dumped to a csv file specified by --output /path/to/file.csv.

If --plot output.png is passed, a plot of the extracted data-points will be saved to output.png. This requires matplotlib. Very useful when debugging/testing.

Notice the error near the right y-axis.

Using in batch mode

You can pass the coordinates of points in the image at the command prompt. This allows to run in the batch mode without any need for the user to click on the image.

plotdigitizer ./figures/trimmed.png -p 0,0 -p 20,0 -p 0,1 -l 22,295 -l 142,295 -l 22,215 --plot output.png

How to find coordinates of axes points

In the example above, point 0,0 is mapped to coordinate 22,295 i.e., the data point 0,0 is on the 22nd row and 295th column of the image (assuming that bottom left of the image is first row, first column (0,0)). I have included an utility plotdigitizer-locate (script plotdigitizer/locate.py) which you can use to find the coordinates of points.

$ plotdigitizer-locate figures/trimmed.png

or, by directly using the script:

$ python3 plotdigitizer/locate.py figures/trimmed.png

This command opens the image in a simple window. You can click on a point and its coordinate will be written on the image itself. Note them down.

Examples

Base examples

plotdigitizer figures/graphs_1.png \
		-p 1,0 -p 6,0 -p 0,3 \
		-l 165,160 -l 599,160 -l 85,60 \
		--plot figures/graphs_1.result.png \
		--preprocess

original reconstructed

Light grids

plotdigitizer  figures/ECGImage.png \
		-p 1,0 -p 5,0 -p 0,1 \
        -l 290,337 -l 1306,338 -l 106,83 \
		--plot figures/ECGImage.result.png

original reconstructed

With grids

plotdigitizer  figures/graph_with_grid.png \
		-p 200,0 -p 1000,0 -p 0,50 \
        -l 269,69 -l 1789,69 -l 82,542 \
		--plot figures/graph_with_grid.result.png

original Image credit: Yang yi, Wang

reconstructed

Note that legend was not removed in the original figure and it has screwed up the detection below it.

Limitations

This application has following limitations:

  • Only b/w images are supported for now. Color images will be converted to grayscale upon reading.
  • Each plot should have only one trajectory.

Need help

Open an issue and please attach the sample plot.

Related Projects

  1. WebPlotDigitizer by Ankit Rohatagi is very versatile.

Notes

plotdigitizer's People

Contributors

dependabot[bot] avatar dilawar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

plotdigitizer's Issues

This plotdigitizer cannot work in some images

SBA_SAMARANG_S
I had getting out the line of the graph
The command I use is
plotdigitizer output/test.png -p 0,0 -p 150,0 -p 0,0 -p 0,7.5 -l 50,10 -l 100,10 -l 50,10 -l 50,160 --plot test.png

But I face this error
INFO:root:Extracting trajectories from output\test.png INFO:root:[(50, 10), (100, 10), (50, 10), (50, 160)] โ†’ origin 50, 10 INFO:root: 253.37610016420362 3.9020312040091025 Traceback (most recent call last): File "C:\Users\chunfang\anaconda3\envs\weather_nowcast\lib\runpy.py", line 194, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Users\chunfang\anaconda3\envs\weather_nowcast\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "C:\Users\chunfang\anaconda3\envs\weather_nowcast\Scripts\plotdigitizer.exe\__main__.py", line 7, in <module> File "C:\Users\chunfang\anaconda3\envs\weather_nowcast\lib\site-packages\plotdigitizer\plotdigitizer.py", line 323, in main run(args) File "C:\Users\chunfang\anaconda3\envs\weather_nowcast\lib\site-packages\plotdigitizer\plotdigitizer.py", line 264, in run traj = process_image(img_) File "C:\Users\chunfang\anaconda3\envs\weather_nowcast\lib\site-packages\plotdigitizer\plotdigitizer.py", line 217, in process_image trajcolor = params_["timeseries_colors"][0] IndexError: list index out of range

Can I know what error is it? Thanks

UnicodeEncodeError: 'charmap' codec can't encode character '\u2192'

Hi,

I came through an error that seems to be Unicode related.
I removed from the image whatever is not the axis and the numbers to avoid confusion, but still happens.
Here is the error below.

Thank you in advance,

--- Logging error in Loguru Handler #2 ---
Record was: {'elapsed': datetime.timedelta(microseconds=16049), 'exception': None, 'extra': {}, 'file': (name='plotdigitizer.py', path='C:\\Users\\user\\anaconda3\\envs\\myenv\\lib\\site-packages\\plotdigitizer\\plotdigitizer.py'), 'function': 'run', 'level': (name='DEBUG', no=10, icon='\U0001f41e'), 'line': 249, 'message': "data points ['200,0', '200,100', '0,1000'] \u2192 location on image []", 'module': 'plotdigitizer', 'name': 'plotdigitizer.plotdigitizer', 'process': (id=23404, name='MainProcess'), 'thread': (id=12196, name='MainThread'), 'time': datetime(2022, 7, 4, 11, 23, 40, 699096, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200), 'W. Europe Summer Time'))}
Traceback (most recent call last):
  File "C:\Users\user\anaconda3\envs\myenv\lib\site-packages\loguru\_handler.py", line 177, in emit
    self._sink.write(str_record)
  File "C:\Users\user\anaconda3\envs\myenv\lib\site-packages\loguru\_file_sink.py", line 176, in write
    self._file.write(message)
  File "C:\Users\user\anaconda3\envs\myenv\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2192' in position 118: character maps to <undefined>
--- End of logging error ---
2022-07-04 11:23:40.707 | WARNING  | plotdigitizer.plotdigitizer:run:252 - Either the location of data-points are not specified or their numbers don't match with given datapoints. Asking user...
--- Logging error in Loguru Handler #2 ---
Record was: {'elapsed': datetime.timedelta(seconds=13, microseconds=679939), 'exception': None, 'extra': {}, 'file': (name='plotdigitizer.py', path='C:\\Users\\user\\anaconda3\\envs\\myenv\\lib\\site-packages\\plotdigitizer\\plotdigitizer.py'), 'function': 'transform_axis', 'level': (name='INFO', no=20, icon='\u2139\ufe0f'), 'line': 151, 'message': '[(83, 64), (83, 440), (784, 63)] \u2192 origin 83, 63', 'module': 'plotdigitizer', 'name': 'plotdigitizer.plotdigitizer', 'process': (id=23404, name='MainProcess'), 'thread': (id=12196, name='MainThread'), 'time': datetime(2022, 7, 4, 11, 23, 54, 362986, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200), 'W. Europe Summer Time'))}
Traceback (most recent call last):
  File "C:\Users\user\anaconda3\envs\myenv\lib\site-packages\loguru\_handler.py", line 177, in emit
    self._sink.write(str_record)
  File "C:\Users\user\anaconda3\envs\myenv\lib\site-packages\loguru\_file_sink.py", line 176, in write
    self._file.write(message)
  File "C:\Users\user\anaconda3\envs\myenv\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2192' in position 119: character maps to <undefined>
--- End of logging error ---
Traceback (most recent call last):
  File "C:\Users\user\anaconda3\envs\myenv\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\user\anaconda3\envs\myenv\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\user\anaconda3\envs\myenv\Scripts\plotdigitizer.exe\__main__.py", line 7, in <module>
  File "C:\Users\user\anaconda3\envs\myenv\lib\site-packages\plotdigitizer\plotdigitizer.py", line 334, in main
    run(args)
  File "C:\Users\user\anaconda3\envs\myenv\lib\site-packages\plotdigitizer\plotdigitizer.py", line 269, in run
    traj = process_image(img_)
  File "C:\Users\user\anaconda3\envs\myenv\lib\site-packages\plotdigitizer\plotdigitizer.py", line 221, in process_image
    trajcolor = params_["timeseries_colors"][0]
IndexError: list index out of range

Can run application from PowerShell

Hello dilawar,

Thank you for your great work.
I am searching for a good application for Plot Digitalization and I was testing your application today.
I tried your examples as well as my own Plots. But in both cases I get the following error Message:

image

I also had a look at the Source Code but I don't understand what is the problem

Multiple lines in same plot

Kudos to you .. This works like a charm .. it will be also helpful if something can be done for multiple lines in a same plot as well ..

Background of the image

is there any way to allow this script to identify the image with a dark or transparent background?
I have tried to change the background by adding values to all pixels, but the color image convert to greyscale again and shows the error: I computed that background is 'dark' which is unacceptable to me
0314_006_3-PEEPreduced-from_10to5
.

ImportError: cannot import name 'PlotDigitizer' from 'plotdigitizer' (C:\Users\lenovo\anaconda3\lib\site-packages\plotdigitizer\__init__.py)

hello,

I have installed plotdigitizer in jupyter notebook. When I tried importing using "from plotdigitizer import PlotDigitizer" it's giving below error:
"
ImportError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_14264\1414868341.py in
----> 1 from plotdigitizer import PlotDigitizer

ImportError: cannot import name 'PlotDigitizer' from 'plotdigitizer' (C:\Users\lenovo\anaconda3\lib\site-packages\plotdigitizer_init_.py)

"

I have tried opening " init.py" , unfortunately, it's an empty file.

Could you please help me? I want to extract data from a bar graph. But plotdigitizer is not working.

Dark background not allowed

It seems dark background is also not allowed.. Unfortunately, that's unavoidable for me

nikhil@starwars:~/HPC_aneurysm/post_process_utils$ plotdigitizer UNN.JPG -p 0,0 -p 5,0 -p 0,50
2022-10-23 20:33:21.225 | WARNING  | plotdigitizer.plotdigitizer:run:252 - Either the location of data-points are not specified or their numbers don't match with given datapoints. Asking user...
2022-10-23 20:33:43.258 | ERROR    | plotdigitizer.plotdigitizer:_find_trajectory_colors:187 - I computed that background is 'dark' which is unacceptable to me.

logger doesn't support utf-8, resulting in error in Windows Command Promp

When I was trying to explore this tool in windows command prompt, it gives me error for Unicode Not Supported.
After digging inside the code, I found that it requires to add utf-8 additionally in logger argument, which will add support for user to run from windows command prompt.

More details regarding issue can be found here..
Delgan/loguru#124

Please let me know if I can create Pull Request for this small fix. I have made the changes in local.

CICD: Fix the travis deployment

Hi!

The build is reported as failing, but when I have checked the Travis pipeline one can see that it works correctly until the end.

The reason to report as broken is this line in your deployment script defined in .travis.yml:

sh -e /etc/init.d/xvfb start

What is the need behind this line?

Cheers

No module name cv2

After installing as per the instructions when I try to run the either of the commands
plotdigitizer --help
plotdigitizer ./figures/trimmed.png -p 0,0 -p 10,0 -p 0,1

the following error shows up, how do I get by this error

Traceback (most recent call last):
File "/home/harsh/.local/bin/plotdigitizer", line 7, in
from PlotDigitizer.plotdigitizer import main
File "/home/harsh/.local/lib/python3.6/site-packages/PlotDigitizer/plotdigitizer.py", line 13, in
import cv2
ModuleNotFoundError: No module named 'cv2'

transparent img

If my image has a transparent or dark background, is there any way to allow the script to identify this image?
I have tried to change the background color by adding values to all pixels, but it still does not work as the color image converts to grayscale, then shows an error: I computed that background is 'dark' which is unacceptable to me.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.