![]() |
|
| Daemon News Ezine | BSD News | BSD Mall | BSD Support Forum | BSD Advocacy | BSD Updates |
Trawling the Ports Collection: Manipulating graphics imagesGreg Lehey <grog@lemis.com>Last month we looked at some simple audio ports. We'll look at more of them in a later article in this series, but in order to avoid being too one-sided, I'll look in another ports category this month. Manipulating graphical images is an important part of using a desktop computer nowadays. Graphics-capable web browsers have been around for ten years, and in an earlier issue of this column we looked at graphics displays for documents. This month we'll look at using xv, one of the old graphics diehards, for processing images from digital cameras. Digital camera formatsModern digital cameras use a number of different memory formats and computer connections. The most popular ways of transferring data from the camera to the computer are with USB and with CompactFlash PCMCIA adaptors. Last time I looked, USB connections were a can of worms: each camera manufacturer seemed to have his own interface. I'm told that that has changed, but I still can't reliably connect my Nikon CoolPix 880 to my system. In addition, since my camera doesn't have a power supply, I'm using expensive battery power during the transfer. The alternative is to remove the CompactFlash card and insert it in a laptop in a PCMCIA adaptor. The PCCARD software should then create a disk device for the card. On my FreeBSD laptop, it becomes /dev/ad8 for some reason. Things look something like this: Jun 30 13:20:17 sydney /kernel: pccard: card inserted, slot 0 Jun 30 13:20:17 sydney /kernel: ata4 at port 0x240-0x247,0x24e irq 11 slot 0 on pccard0 Jun 30 13:20:17 sydney /kernel: ad8: 46MB <> [185/16/32] at ata4-master BIOSPIO After this, you can mount the flash card and treat it pretty much like any normal Microsoft disk. # mkdir /camera # mount -t msdos /dev/ad8s1 /camera # ls -RFC /camera/ DCIM/ NIKON001.DSC* /camera/DCIM: 100NIKON/ /camera/DCIM/100NIKON: DSCN1821.JPG* DSCN1822.JPG* INFO.TXT* # ls -l /camera/DCIM/100NIKON -rwxr-xr-x 1 root wheel 1103764 Jun 30 21:52 /camera/DCIM/100NIKON/DSCN1821.JPG -rwxr-xr-x 1 root wheel 1051861 Jun 30 21:52 /camera/DCIM/100NIKON/DSCN1822.JPG -rwxr-xr-x 1 root wheel 638 Jun 30 21:52 /camera/DCIM/100NIKON/INFO.TXT This is what you'd see with a Nikon CoolPix 880 camera. The photos are stored in JPEG format in the directory /DCIM/100NIKON. The modification timestamps are in UTC: the camera does keep time, but it doesn't understand time zones, so the times you see will probably not be the local time you took the photo. The file INFO.TXT contains information about the photos. To move the data around, use standard UNIX commands: # mkdir /home/grog/Photos/20020630 # mv /camera/DCIM/100NIKON/* /home/grog/Photos/20020630 # df /camera Filesystem 1048576-blocks Used Avail Capacity Mounted on /dev/ad8s1 45 2 43 5% /camera # chown grog /home/grog/Photos/20020630 # chown grog /home/grog/Photos/20020630/* # df /camera Filesystem 1048576-blocks Used Avail Capacity Mounted on /dev/ad8s1 45 0 45 1% /camera # umount /camera Unfortunately, we have to do this as root, since it involves mounting disks. This is an area which could do with improvement. As a result I need to issue a couple of chmod commands to change the ownership of the images to myself. Note also that I use the mv command, which automatically deletes the photos from the flash card after copying. It's worth being careful with this: it's quite easy to crash your laptop by removing the flash card when it's still mounted (note the umount command at the end). If you perform the mv and then crash the laptop by immediately removing the flash card, you may discover that you have lost your photos. I avoid this problem by copying to an NFS-mounted file system. xvNext, we need to run xv. Install from the Ports Collection as usual. Unlike most software in the Ports Collection, it's not free software: it's shareware, a concept which has almost died out. The reason xv hasn't changed its license is, well, it hasn't changed in the best part of ten years. The latest version I have is version 5.3.3, dated 29 December 1994. The license basically says ``you can use this program for non-commercial use, and contact us for commercial conditions.'' I sent a mail message to that address while writing this article, but as of right now I don't have a reply. xv runs under X, so you need to be running X before you can use it. You can run xv simply with the name, or you can supply a list of files for it to process. If you start it by itself, you see: ![]() In this case, though, we have a list of images we want to look at, so we do: $ cd Photos/20020630 $ xv *.JPG & Then we see the first photo (careful, remember that 1 MB size above?). We can move between the photos by pressing the space bar for the next photo and the backspace key for the previous photo. We can do better than that, though. If we press the right mouse button, we get the following display: ![]() We can then select the images with the mouse, or perform a number of conversions. Getting the image to a usable sizeThe image we're looking at has a resolution of 2048x1536 pixels, and it's somewhat over 1 MB in size. In all likelihood you can't display it on your monitor. But even if you can, it's still larger than necessary. xv can save with different levels of compression. By default, it recommends 75% compression. If you click on the Save button on the right, you get the following menu: ![]() Before we continue, it's important to understand an idiosyncrasy of xv: it automatically shrinks images to fit them on the screen. If you're running on a 1024x768 screen, that's the size the xv image will be. On the other hand, web browsers typically display in the original resolution unless told not to, accompanying it with scroll bars, so it could well be worth saving the image in original size.
Note the ![]() It's possible to turn both the Quality and Smoothing dials either by selecting the arrows, or by pressing the left mouse button on the indicator and dragging. In this case, we leave it at the default settings, press Ok and save the image. It doesn't really look any different, does it? It's not supposed to, of course. But this file is much smaller: $ ls -l DSCN1821.JPG yana-big.jpeg -rwxr-xr-x 1 grog wheel 1103764 Jun 30 21:52 DSCN1821.JPG -rw-r--r-- 1 grog lemis 317938 Jun 30 18:23 yana-big.jpeg The quality isn't as good, but you can hardly tell the difference. The only reason for keeping the original is if you want to do other processing, in which case it's good to have as good an original as possible. But this image is still enormous. How about shrinking it a bit?
You can change sizes in a number of ways: select some of the unintelligible
icons at the bottom of the controls menu, select the
You don't need to remember this list; you get it automatically if you press the
![]() When you shrink the image, most of it will disappear from the screen. As a result, the cursor may no longer be over the image, and you may end up typing into a different window. You can avoid this by positioning the cursor over the controls window. After shrinking, we're left with this image, which is only 10216 bytes in size. Rotating images
There are other things you may need to do, of course. The
next image was taken in portrait orientation.
xv doesn't know about that, so it displays it on its side. To rotate it,
use one of the rotate buttons ( Note that if the previous image filled the screen, the new one will be smaller, since xv doesn't go off the screen. You'll have to check the size before saving. CroppingOften it's not possible to get perfect framing of a photo, or for some reason you want only a part of the photo. The act of removing the superfluous parts of the photo is called cropping. Theoretically you could crop to any subset of the photo (putting it in the shape of a daemon, for example), but xv can only crop to a rectangular shape. For example, I took the two photos in question to point out to my daughter how she was holding the bassoon reed in her mouth. We don't need the entire background for that. Instead, we mark the area Of interest by putting the mouse pointer at one corner, pressing the left button, and dragging: ![]()
The box indicates the portion of the photo which is to be cropped. You can
change its position by clicking on one of the eight boxes on the corners and sides
and dragging. When you are happy, press the ![]()
At the same time, the Autocropping
You'll notice there's also a button The colour editorSometimes you get images that are of borderline quality. For example, consider the following image, which was produced by a very poor quality scanner: ![]() This photo is so dark that you could assume there is only one horse there. To correct this, hit the e key. The following menu appears: ![]() There are a number of things you can do here, but we're going to look primarily at the Intensity box at the bottom middle. You can play around with it by pulling on the line if you know exactly what you're doing, but in general it's better to change the gamma value. By default it's 1; higher values than 1 are brighter, lower values are darker. In this case, we position the cursor over the Intensity box, type g1.5 and see: ![]() At the same time, the image changes: ![]() That's still too dark. Trying again with 2, we see: ![]() Well, that's better, but it's still too dark. If we increase the gamma to 2.5, we get this: ![]() Well, it's lighter, but there's another problem here: the quality of the image is deteriorating rapidly. This is one of the problems with the JPEG format: it doesn't contain enough information for us to be able to recover the image any better. Somewhere at this point we need to decide that we can't make a silk purse out of a sow's ear, and decide which to use. Other uses of xvThis article has only touched on some aspects of xv; there are many other uses. I used it, for example, to create the illustrations for this article. You can also use it to convert image formats, and to smooth or otherwise enhance images. Its biggest problem is that it's interactive. If you want to convert 40 images to one-eighth size, you'll have to do each one individually. That's not the UNIX way: it should be possible to do it automatically, preferably controlled by rules in a Makefile. And yes, indeed, that is possible with ImageMagick, but we'll leave that for another day. |