Hello World

From VipsWiki

Jump to: navigation, search

This page has code for some minimal programs in VIPS and nip. They don't do anything useful, but they are handy for checking that your install is working.

Contents

VIPS 7.x Hello World in C

This program loads an image and finds the average pixel value. It should work for pretty much any image.

#include <stdio.h>
#include <vips/vips.h>

int
main (int argc, char **argv)
{
  IMAGE *im;
  double avg;

  /* Start up VIPS. This will load translations, init the
   * threading system, init any libraries that VIPS uses and load any
   * plugins.
   */
  if (im_init_world (argv[0]))
    error_exit ("unable to start VIPS");

  if (argc != 2)
    error_exit ("usage: %s <filename>", g_get_prgname ());

  if (!(im = im_open (argv[1], "r")))
    error_exit ("unable to open");
  if (im_avg (im, &avg))
    error_exit ("unable to find avg");
  im_close (im);

  printf ("Hello World!\nPixel average of %s is %g\n", argv[1], avg);

  return (0);
}

Compile this program on Linux or OS X with:

gcc -Wall try5.c `pkg-config vips-7.18 --cflags --libs`

And run with:

./a.out ~/pics/fred.jpg

You should see:

Hello World!
Pixel average of /home/john/pics/fred.jpg is 67.0741

VIPS 7.x Hello World in C++

This program loads an image and finds the average pixel value. It should work for pretty much any image.

#include <stdio.h>
#include <vips/vips>

using namespace vips;

int
main (int argc, char **argv)
{
  try {
    if (argc != 2)
      throw (VError ("args: <filename>"));

    VImage fred (argv[1]);

    printf ("Hello World!\nPixel average of %s is %g\n", argv[1], fred.avg());
  }
  catch (const VError &e) {
    e.perror (argv[0]);
  }
            
  return (0);
}

Compile with something like:

g++ -Wall try.cc `pkg-config vipsCC-7.18 --cflags --libs`

And run with:

./a.out ~/pics/fred.jpg

You should see:

Hello World!
Pixel average of /home/john/pics/fred.jpg is 67.0741

VIPS 7.x Hello World in nip2

This is the same thing, but in nip2. This example uses the command-line mode from 7.12.

nip2 -e 'mean (Image_file "~/pics/fred.jpg")'

And you should see:

67.0741

(Image_file is the constructor for the Image class that makes an image from a filename, mean finds the pixel average.)

That example has the filename of the image to be processed embedded in the expression. You can supply the filename as a separate argument and pick it up like this:

nip2 -e 'mean (Image_file argv?1)' ~/pics/fred.jpg

(argv is a list of all the command-line arguments in the form ["script-name", "first-argument", ...], and ? is infix list index.)

To get exactly the same output as the C/C++ versions above you need the wordier:

nip2 -e '"Hello World!\nPixel average of " ++ argv?1 ++ " is " ++ print (mean (Image_file argv?1))' ~/pics/fred.jpg

(++ is infix list concatenation, print turns any expression into a string.)

VIPS 7.x Hello World in Python

import sys
from vipsCC import *

try:
  a = VImage.VImage (sys.argv[1])

  print 'Hello World!\n'
  print 'Pixel average of', sys.argv[1], 'is', a.avg ()
except VError.VError, e:
  e.perror (sys.argv[0])
Personal tools