Episode: 1822 Title: HPR1822: Some tips on using ImageMagick Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr1822/hpr1822.mp3 Transcribed: 2025-10-18 09:43:20 --- This is HPR episode 1,822 entitled, some tips on using image magic. It is hosted by Dave Morris and is about 21 minutes long. The summary is image magic in an amazing toolkit for manipulating images. Here's how I use it. This episode of HPR is brought to you by an honesthost.com. Get 15% discount on all shared hosting with the offer code HPR15. That's HPR15. Better web hosting that's honest and fair at An Honesthost.com. Hello everyone this is Dave Morris. In the past few days I've been preparing an HPR show which has got a bunch of images in it. While I was doing this I was learning my way around various image manipulation tools. I thought I would do a quick show about what I discovered and what I was doing to do the various things with images. In particular I was using image magic which is a very clever package. So I thought I would just go over some of what I'd been doing. Image magic contains an enormous range of capabilities and you could somebody who was more adept at it than I am could spend the time doing a whole series of shows on its capabilities. I thought that I'd just give you a quick overview of what I've discovered just a bunch of tips which is why I've called this show some tips on using image magic. I really am the rawest amateur when it comes to this type of image manipulation and I've found most of what I needed by reading through the image magic documentation which I've pointed to in the links and there's a lot there you could spend ages reading through this stuff. It is really fascinating if you're interested in working with images and doing clever things with them. Anyway what I normally do when preparing a show is to get images by taking pictures and like what I did when I'm coffee where I took pictures of my mocha coffee maker and various things like that. So I used my digital camera to do this. Now digital cameras generate metadata that describes stuff about the camera and the picture or GPS if you've got that switched on. I like to strip that out before I upload the pictures. So the first thing I do is to to strip this out. I've found that it is possible to use image magic's convert command to do this but it's not a recommend a way of doing it. There's another thing called exif tool which is a script which is freely available. I've given you a link to it. I used that you would type in exif tool, space, hyphen or AWL equals space and then the name of a file or indeed any sort of wildcarded file specification like star.jpg and it will go through all of the files that match removing the exif data. It saves the original image by just simply appending a hyphen original to the file name. So if for any reason you need to go back to these you want to keep one with the copy with the exif in you can easily do that. Then one of the things I do after that is to crop the image. Usually I've taken a picture which has got a lot of surrounding stuff around it so it's quite like to just crop it in some way to get that sort of rubbish away, simplify the image. There are ways of doing this with the image magic but you need to know a lot about the geometry of the image and really I found that I couldn't get into doing that very well. So in the past I've used GIMP to do it but I've always found it just a tiny bit odd to use so I recently listened to people talking about Criter or Criter because some people call it. I find that the crop tool in there is really good so strongly recommend that. It's really easy. You just bring up the picture in Criter and then click on the crop tool and just draw a box around it and bam hit the crop button and there you go. Save it over the top of the original if you want to. Now digital camera files are usually enormous so in the context of an HPR show it's unlikely you're going to want to upload all of a large image so what I normally do is to reduce the image size in order to make this a little bit easier and normally just put all the images I want for a show into an images sub directory. First of all I create a directory for the show itself so I keep all the show notes and all that sort of thing in that directory and then I make a sub directory of that which I call images and then I run image magic to walk through that list of files and convert them, resize them, shrink them down. So in general the convert command which is one of the commonest commands within image magic you give it the name of the original picture then you put hyphen resize resize I normally put 640 that's the number of pixels wide that you want the result to be image magic will work out the other dimensions the height in other words so the aspect ratio is not changed then you give it the name of the final file so it's my example is convert space big pic dot jpg hyphen resize space 640 space small pic dot png now the output file can be a different format to the input file and image magic will spot that you've done that and will do the necessary format change so I've given in the notes that there's a long sort of long notes in this case I've given there just the the sort of single command line that I would type in to do all the the conversions in one go what my camera generates is files with numerical names with a dot jpg on the end it's a it's not a very advanced camera so it just generates jpegs and the jpg is in capitals so there's a four loop a bash four loop that I've shown which walks through all the files in the images directory and then runs the convert command on on each one I won't I won't go through that in detail you you should be able to get what it what it does I've given a short explanation in the notes as to what it's actually doing as well as reducing an image to a specific width in pixels you can also reduce it by a percentage so after hyphen resize you can put something like 50% if you want to and then that will reduce the image by 50% keeping the aspect ratio the same the whole business of resizing is quite complicated I haven't completely got my head around it because the aspect ratio is an issue that you need to be fully aware of you can mangle images by by doing it wrong so if you do what I've suggested then you you won't get too many trouble into too much trouble one thing I sometimes do if there's lots of pictures is I make some thumbnails the idea being that the thumbnails will then be placed in the notes and you'll be able to see oh there's some pictures there and they'll be formatted in some way maybe one per paragraph or something or in a block of images and then when you click on them you see the full image so the way I'd sometimes do that will have done that in the past when I've done it it was not many times but is I would create a directory which I call thumbs to hold the the thumbnails and the assumption is that you want the the image magic command to drop the thumbnails into this thumbs directory there's a command called magnify which does this and in general you would type magnify space hyphen format space png if you're using png files there's different formats obviously you won't be working with minus path space thumbs that's the path I want to drop the files into minus thumbnail that's telling it to generate thumbnails and you follow that in I followed this anyway with 100 times 100 x100 which says you want 100 by 100 thumbnails then you would give it the name of an image or indeed a list of images again you can use a wildcarded file name in this case and it will go and just walk through them all and do them but I wrote myself a script to do this which I've included in the notes that's because when I've got a bunch of images I usually create a file which contains their names and I call it manifest and do this every time because I've got very scripts that rely on that being the case and my script walks through that file the manifest file and just operates makes them now from each one so some cases I've got images that I'm not really happy with I'm not sure I want to include so I would exclude them from the manifest even though I've still got them around in my show directory so it's useful just to be able to separate them out anyway you can look at the script it's very simple it just runs the magnifying command in a loop and I mentioned here that you can use magnify to just convert the whole lot in one go another thing I've done recently is to mess around with the thumbnails a bit after I've created them and I tried in in a recent show to add a watermark to each of the thumbnails just I put a number I wanted to put a watermark number on the front of each one and also to add a border just to make them look a little bit nicer basically I'm messing around with this stuff as you probably gathered but learning along the way so to do that I put a found out some commands to help me and put them into a script which I've included and what this script does it's another sort of thing where it's looping through the manifest file and for each of the thumbnails it's making a backup copy of the original so that if anything goes wrong then we're okay and then it runs the convert command and this is quite a complicated convert basically I want to talk talk about I want to read it out to you but but I'll try and describe it briefly and there's a convert command in this script which takes the the thumbnail and it takes the old name which I've renamed the thumbnail to a name with a ridge in the name to mark that it's the original file without being modified so I would take that as the input to the convert and then define the font and the size of the font that I want to use for the watermark and then draw on the center of the image a watermark made up of black and white text overlayed containing the number and the loop is incrementing a number as it goes beginning and then after doing this watermark addition it adds a border and that's another another instance of the convert command which is this sort of general workhorse of image magic it seems to take the the image that I've just added a watermark to then minus shave s h a v e space one times one one x one that says take one pixel off the edge and then put in its place a border a black border so it's saying hyphen border color space black hyphen border space one so that's a one pixel border has been added to the to the image without changing its size so and then it finishes off with the name of the file again so the convert is taking the file and writing back over the top of it with the the border since we've got the original still it doesn't really matter then the script increments the index and carries on so that's quite nice another thing I tried in a recent show was to add captions to images you can tell I've maybe been reading through some of the image magic stuff to see what I can do and messing around with it so I discovered it's possible to take an image and add a text caption to it so the next section is about this subject so what I did was I made a file from my manifest file actually and it contains a list of all the the files I want to work on but after each file name is a comma and then the text of the caption so I've given you the script that I've used and it goes through a loop this is a while loop in bash and the argument of the while loop is a read command reads into a variable called l now the reason I'm using read is because I'm reading text out of a file one line at a time and the text contains spaces if you use the technique I used before where you you read stuff in a fall loop then the fact that the the spaces on each line will cause it to be cause the line to be broken up into a bunch of arguments so it basically doesn't work if you do that so I'm using read in a loop and for each line that I get I split it up into two pieces one the bit before the comma and the bit after the comma and you'll see after the while there's f equals and an expression and then c equals an expression f file name c for for caption then I do another thing where I save the original file making changing the name of the existing file to the same name with a ridge or i g dot png on the end of it and then I use convert to take the original file do the action to it and then write the file name that we had in the start so we end up with the list of all the files effectively having been changed but with the backup the convert command takes the the input file as a first argument this time we've got minus background space and I chose carkey k h a k i as as a color just because I'd seen it in in the documentation basically could have been anything then I say a label colon and then in quotes dollar c that's the the caption that I pulled out of the file minus gravity center gravity space which says that I want the text to be centered and then minus append which puts it on the bottom of the of the image and then the last argument is the the output file name the while loop ends obviously you you'll be aware that the syntax is while something or other while expression do and then the final thing is done but after the done if you put a less than sign which means read from and then the name of the file that you you've got your captions in the mind's called captions then it that that's how you you read the file the I've given an example of what my captions file actually looks like so now it is an optically clever or elegant but and it's also not very robust you could easily make this thing break but it is done the job and I'll probably develop a bit more in the future and you're welcome to to do your interest to develop yourself however you see fit the final thing I wanted to mention was when I was preparing this particular show with images in it I wanted to join a couple of images together so I'd taken two photographs and I wanted to shrink them both and then stick them side by side to make an image of the sort of size that I was generating for the for the show from the other pictures so that wasn't quite so straightforward I couldn't easily find an answer to this I found a stack overflow question being answered which gave me a rather complicated convert command which I've included in the notes I don't understand it to be honest I've not managed to work out exactly what it's doing but it does quite a nice job sticks the two images together and there's hardly any gap between them and no border around it and I think it's actually quite nice but perhaps a better method if you can work out how to use it is to use the image magic montage command and I've experimented through that a bit you would you would type something like montage followed by minus background space and then a hex number for a for a color I just used in quotes hash and then six zeros meaning black could have actually put black there I think I didn't do that I don't know why I didn't do that anyway then there's a geometry option followed by a space and in the case I've given you here I've given a geometry of one times one and then back slash less than sign and then plus one plus one this this is similar to the sort of standard geometry specification you see in x windows and other type of graphical specifications what it's actually has a special meaning one times one with a less than after you have to put a slash in front of the less than because it's interpreted by the shell what that means that's a sort of special sequence which says to image magic give me the best fit you can actually fiddle around with the sizes of the images that make up the montage and I haven't completely understood how that works I have to admit so I just went for the easy way the plus one plus one bit says how much space to put around the images because effectively the images are treated as tiles which are stuck together in in an image and this effectively controls the border between them the border that's actually produced it's as if they're stuck on a background and we've already defined the color of the background as black so that's how it comes together have a look at the documentation it probably explains a lot better than I've done anyway you follow all of that with the names of the images that you want to concatenate together and it can be any number really I think I'm not sure what the limits are but in my case it's only two and the final argument to the montage command is the name of the file you want it to write to in my case of my arguments are left.png, right.png and then they're to produce an image called montage.png so it it works quite nicely I actually get quite a large image as a result but then I run the convert resize on that to get the the actual size that I that I want so have a look at the image magic documentation if you're interested in in using montage or indeed have a look at that stack overflow article that I've linked to I've found that montage is a bit more straightforward so I probably recommend that so anyway I've given you links to the various bits and pieces the sub elements of image magic that I've been using so I hope you find that that useful and if you if you do let me know okay thanks bye you've been listening to hecka public radio at hecka public radio dot org we are a community podcast network that releases shows every weekday Monday through Friday today's show like all our shows was contributed by an hbr listener like yourself if you ever thought of recording a podcast then click on our contribute link to find out how easy it really is hecka public radio was founded by the digital dog pound and the infonomicum computer club and it's part of the binary revolution at binrev.com if you have comments on today's show please email the host directly leave a comment on the website or record a follow-up episode yourself unless otherwise status today's show is released on the creative commons attribution share a light 3.0 license