File Operations

All of the file operations performed by the Gimp are handled by plug-ins. To support a new file format, all you have to do is write a “file load” procedure and a “file save” procedure, and register them with the Gimp. Each format’s load procedure follows a similar format, taking three arguments and returning an image. The run_mode specifies whether the Gimp should prompt the user for information (if RUN_INTERACTIVE) or simply perform the action with the supplied arguments (if RUN_NONINTERACTIVE). The filename and raw_filename[23] arguments should both contain the path and name of the file to load. The first five arguments of the save procedure for a file format are also generally standard: the run_mode ,the image to save, the drawable for the image, the filename to save it as, and the raw_filename. Most formats have additional arguments to the save procedure (file_png_save, for example, allows you to specify a compression level and whether the image is to be interlaced).

The gimp_temp_name function will return a unique filename in the user’s .gimp directory. Here’s an example of saving an image as a temporary GIF file:

# Save as a temporary GIF file
#
gimp_convert_indexed_palette($img, 1, 2, 0, 0);   # Use web palette
my $tmpfile = gimp_temp_name('gif');
file_gif_save(RUN_NONINTERACTIVE, $img, $drawable, 
              $tmpfile, $tmpfile, 1, 0, 100, 0);

There are actually many more file formats supported by the Gimp; only those specifically applicable to web graphics have been listed ...

Get Programming Web Graphics with Perl and GNU Softwar now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.