GithubHelp home page GithubHelp logo

ppmparser's Introduction

About PPM Parser

Python class for parsing PPM files.

Requirements:

  • numpy

Usage

Get the x/y/z/nx/ny/nz for a given u/v

with PPMParser(ppm_filename).open() as ppm:
    x, y, z, nx, ny, nz = ppm.get_3d_coords(1000, 2000)
    print(x, y, z, nx, ny, nz)

Note that this operation is very fast because PPMParser uses file seek operation to read the correct coordinates.

Traverse over ppm

with PPMParser(ppm_filename).open() as ppm:
    for imx, imy, x, y, z, nx, ny, nz in ppm.read_next_coords(skip_empty=True):
        print(x, y, z)

Using step

We often want to get only every n-th point in both x and y direction. Parameter step allows us to do this:

with PPMParser(ppm_filename, step=4).open() as ppm:
    for imx, imy, x, y, z, nx, ny, nz in ppm.read_next_coords(skip_empty=True):
        print(imx, imy)  # note that you need to multiply both with 4 to get the original coordinates

To avoid possible mistakes, we can use im_shape() like this:

with PPMParser(ppm_filename).open() as ppm:
    a = np.zeros(ppm.im_shape(), dtype=...)
    for imx, imy, x, y, z, nx, ny, nz in ppm.read_next_coords(skip_empty=True):
        a[imx, imy] = ...

Or we can let PPMParser allocate numpy array for us:

with PPMParser(ppm_filename).open() as ppm:
    a = ppm.im_zeros(dtype=...)
    for imx, imy, x, y, z, nx, ny, nz in ppm.read_next_coords(skip_empty=True):
        a[imx, imy] = ...

Utility: cropppm.py

Utility cropppm crops the input PPM file and writes the result to output PPM file. In the process stride can be applied. The output filename, if not specified, is constructed automatically.

Usage:

$ pip install pipenv
$ pipenv install
$ pipenv shell
<pipenv> $ python cropppm.py -i /path/to/input.ppm -o /path/to/output.ppm --stride 2 -roi 1000-2000,4000-5000

ppmparser's People

Contributors

kglspl avatar

Stargazers

Yakov Till avatar

Watchers

 avatar

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.