Tim Evans Graphics Tips Page

Tim Evans Informal Home Page | Tim Evans Imperial College page

Graphics

More hints on postscript are below.

The arXiv has good help on figure sizes and on the easiest way to do bitmapping.

Which format to use?

For photos with colours use jpg format with uses a lossy compression method giving small files but acceptable image quality.

For images with sharp edges or fine detail use png or gif as both have lossless compression options, and will work with colour, grayscale or black and white images.

Both jpg JPEG and gif files can be wrapped into postscript files and the compression can be kept. ImageMagick and jpeg2ps can do this for you.

What software to use

On Windows to get a copy of the screen press alt+Print Screen key. Pressing cntrl+alt+Print Screen gives the active Window.

My preferred bitmap file viewer on Windows is Irfanview which also lets you do some basic manipulations. It does not let you save as postscript however. Many other programmes are around that will output as postscript but to date all I have found many need to be compiled. Irfanview does allow you to crop a picture (draw a box with the mouse then select Edit-> Crop), to resize a picture (Image -> resize resample) and save in png, jpeg, gif etc. The information option gives you all sort of details on the file, such as the dpi (dots per inch density of bitmap images) used.

For instance, one can look at the Image - resize/resample menu option and select 72dpi and a desired size (probably with Preserve Aspect Ratio selected if one wants to produce an image for a web page. Likewise dragging the cursor with the left mouse key held draws a white box and then the edit - crop selection selects a part of the image. Note the crop box size in pixels is shown at the top helping you to select an exact square if required.

To produce postscript from a bitmap I use the jpeg2ps command line converter. Use the -h option to ensure there are no percentage symbols to confuse things so type

jpeg2ps -h input.jpg > output.eps

ImageMagik

Note that ImageMagik is designed for bitmap files. For instance it will handle and convert eps files to, say, pdf, but it does so as a bitmap so that it is no longer looking nice when rescaled. I eventually found the self-installing files for the Windows version of ImageMagik and while its a command line programme, it can be used very simply. Warning: mogrify replaces existing files with files of same name so you must copy them first or use the convert command. convert needs a batch file to work on more than one while mogrify can process many files at once.

To convert all the files in one directory, say from all eps files to png (png is good for images with sharp edges, jpeg is good for photos) you move to the directory with the a copy of the files (to be on the safe side) and the you type

mogrify -format png *.eps
Here is an example where I resize the tiff to get the esp bounding box to work out
mogrify -format eps -resize 50% aegeain34.tif

Another example of the usefulness of ImageMagik is in producing tiff files (or other bitmap formats) in a standard resolution for a publisher. Starting with the vector files in eps format (produced from other packages like OpenOffice Draw, pajek, etc.) I converted them simply to 600dpi density as follows:

convert fig2.eps -display 600x600 fig2.tiff
convert -density 600 input.eps -flatten output.tiff
Note that with pdf files the density option must come before teh input file as there is no density specified in the input file. A useful example of a ImageMagik Windows batch file is given below. Note that I did not get this to work so used GIMP instead.

Finally here is how I converted a pdf file to a bitmap file

convert -density 1200 -trim original.pdf -resize 3600 -quality 100 bitmapversion.jpg
Note that the resize usually requires a percentage scaling or a width and height in pixels.

Resizing and Resampling

Here is how we can convert a file to be 2cm wide, same aspect ratio and at 96dpi (PC web format)

convert inputfile.png -resample 96x96 -resize 192 outputfile.png

PNG file transparency

Pixels in PNG files (and others) have the option to be transparent to a specified degree. For instance when saving figures from Microsoft office (e.g. right click on a diagram or group of drawing objects and the option is there) png files are produced which are transparent. When displaying in a browser, this can be a problem. IE treats this incorrectly, Firefox showing a black background (even when I changed the background colour in the html body command). The solution is to change the transparanecy of pixels and/or change those transparent pixels to a background colour.

I do this with ImageMagik convert command. two versions I tied were as follows:-

convert fig.png -alpha opaque fig1.png
convert fig.png -alpha off fig2.png
convert fig.png -background white -alpha background fig3.png
The first two produced a grey like background in the browser. The last switched all the transparent (previous balck appearing) pixels to white.

A useful ImageMagik Windows batch file is as follows

@echo off
echo Converts transparent pixels to white pixels in png files.
echo Output files as *whitebakgkgnd.png in output directory.
mkdir output
for %%f in (*.png) do ( convert "%%f"  -background white -alpha background "output\%%~nfwhitebkgnd.png" )
or in a similar way
@echo off
echo Converts pdf to jpg.
echo Output files as *d1200.jpg in output directory.
mkdir output
for %%f in (*.pdf) do ( convert -density 1200 -trim "%%f" -resize 3600 -quality 100 "output\%%~nfd1200.jpg" )

GIMP

This is the GNU image manipulation programme. It deals in pixel based images, not vector ones.

For eps manipulation you need ghostscript (I found mine in C:\Program Files (x86)\gs\gs8.60\bin\gswin32.exe) and you need to add an environment variable GS_PROG with this value (to do this right click on the computer icon, system properties, advanced tab and then the Environment Variables button). I then opened the eps file in GIMP specifying the dpi as the resolution. GIMP now converts this to a pixel based image and you can save or edit it from there.


Web Graphics

Size and DPI for Web Pages

Web browsers display images by pixels so the DPI of an image is nothing more than an instruction that used when a page is printed. Screen resolutions are usually 72 (Mac) or 96dpi (PC) so preparing at 72 dpi is the defacto web standard. Irfanview and Imagemagik can do this as resample and resize options.

Icons for Web Pages

The icon for a web site displayed by the url is called a Favicon or a web icon. Its 16x16 pixels. The modern way to include it is include the following line in the head section
<head>
...
<link rel="icon" type="image/png" href="http://example.com/myicon.png">
...
</head>

For problems with png (and other file types) coming out with a black background in a browser, you may have a problem with graphics file transparency so see answer above.

OCR

There are a few places you can use via the web for occasional OCR work. Try for instance list of available OCR.

Postscript

General Postscript Manipulation

There are numerous utilities to manipulate postscript in various ways. Some examples are below. The descriptions of these utilities you find on the web may be Unix based but the same utilities exist on most Windows machines. Look in the ghostscript directory, something like
C:\Program Files\gs\gs8.53\lib
or try cygwin which emulates Unix and has these commands. Postscript Figures The best way I have found to produce figures is to use the drawing product, draw, in openoffice or libreoffice, both of whioch are open source and multiplatform. Many other drawing packages will have similar capabilities. Draw the figure then export them in a vector format such as eps so they will rescale nicely. The fonts will not rescale but sometimes hand editing can do the trick or go back to the original drawing.

There are some tricks worth knowing for draw and eps export.

Postscript to PDF conversion

These are notes on postscript to PDF conversion. For other pdf issues see pdf notes below.

The web site www.ps2pdf.com/convert/convert.htm is good for occasional ps to pdf conversion.

For more frequent use use ps2pdf which you should find with your Ghostscript implementation. General use is

ps2pdf [options] input.[e]ps output.pdf 
In particular you should note that it is common for pdf export to assume you want a figure or sketch put on a whole page, while eps export can limit the size of the graphic file to be the actual extent of the picture. For instance in the draw component of LibreOffice/OpenOffice you select the part of the graphic you want to save (often this is the whole drawing), choose export, then select the tick box marked selection and if you select the eps option you get a boundingbox defined in the eps file which is set to be only as big as the selected parts of your drawing. If you do this in with pdf format selected for export it all seems to work but the pdf file produced is a full sized page. On the other hand export from the visone network package does produce both eps and pdf files which are only as big as the drawing. So how do we fix this. Well after much searching I found the following option is needed
ps2pdf -dEPSCrop squarewalk.eps squarewalk.pdf
Basically pdf files have things like cropbox or artbox which play the role of the eps boundingbox, you just need the software which sets these pdf boxes. Note that ghostview can set the bounding box of an eps file by hand or automatically, or you even edit the eps file by hand - the boundingbox section is pretty obvious at the start of the file.

Printing economy style

To print a postscript file out where two sheets are reduced and fitted onto one side of A4 and then printing is done on both sides (duplex), you need to produce a new file first.
  1. Use psnup as found in the ghostscript installation and convert filename.ps to a new file, say filenameeconomy.ps
    psnup -nup 2 filename.ps filenameeconomy.ps
  2. Now view and print the filenameeconomy.ps file as normal.

Postscript import for GIMP on Windows

You need to set the Windows environment variable GS_PROG to be C:\Program Files\gs\gs8.51\bin\gswin32.exe or whatever the Ghostscript executable happens to be.

PDF

For conversion from postscript to pdf see postscript notes above. However a useful batch file to convert many pdf files in one directory is as follows

@echo Converts %1 (pdf file) to %2 (eps file)
for %%F IN (*.pdf) DO "C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\pdf2ps" -eps %%F %%~nF.eps

For occasional use try the following

For more sophisticated editing you could try < a href="https://inkscape.org/">inkscape. I find this complicated and buggy but it can get the job done. It looks like a powerful product that will improve over time as it gets better and as I learn how to use it. PDF Toolkit

PDF Toolkit is best for anything more involved. A beautiful example of a programme that does just one job but does it thoroughly and well. It works on all platforms and there is lots of documentation on PDF toolkit and many examples using PDF toolkit. PDF Toolkit can split off pages from a pdf, rotate pages, combine pages from different files, decrypt or encrypt (useful if passing your new book on) etc. etc.

Other Topics

Setting up Windows print queues to manipulate postscript