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.

VIPS 8 Hello World in C

/* compile with
 *
 * gcc -Wall hello8.c `pkg-config vips --cflags --libs`
 */

#include <stdio.h>

#include <vips/vips.h>
 
int
main (int argc, char **argv)
{
  VipsImage *im;
  double avg;
 
  if (VIPS_INIT (argv[0]))
    vips_error_exit ("unable to start VIPS");

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

  if (!(im = vips_image_new_from_file (argv[1], NULL)))
    vips_error_exit ("unable to open");
  if (vips_avg (im, &avg, NULL))
    vips_error_exit ("unable to find avg");
  g_object_unref (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 --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 8.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/vips8>

using namespace vips;

int
main (int argc, char **argv)
{
  if (VIPS_INIT (argv[0])) 
    vips_error_exit (NULL);

  try 
  { 
    if (argc != 2)
      throw (VError ("args: <filename>"));

    VImage fred = VImage::new_from_file (argv[1]);

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

  return (0);
}

Compile with something like:

g++ -Wall try.cc `pkg-config vips-cpp --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 8.x Hello World in Python

This will work in Python2 and Python3.

import sys
from gi.repository import Vips

try:
    a = Vips.Image.new_from_file(sys.argv[1])

    print('Hello World!')
    print('Pixel average of %s is %d' %(sys.argv[1], a.avg()))
except Vips.Error as e:
    print(e)
    sys.exit(1)