Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands tagged ImageMagick from sorted by
Terminal - Commands tagged ImageMagick - 61 results
mogrify -resize 852x480 ./*.png
2013-12-24 12:42:06
User: evilsoup
Tags: ImageMagick
0

Requires ImageMagick to be installed; mogrify is the lesser-known sibling to convert -- it overwrites your original images, but allows you to work on batches of files without resorting to a loop.

convert image.jpg -resize 1x1 txt: | tail -1 | awk '{gsub(/[,\)]/," "); print $3+$4+$5}'
2013-12-11 09:13:00
User: sucotronic
Functions: awk tail
1

You can get the mean value for the colours in an image. Then you can determine, in general, how dark or bright is the image and run some other actions based on that. I'll recommend to readjust the brightness of the images using +sigmoidal-contrast option of imagemagick convert command.

for fil in *.JPG; do datepath="$(identify -verbose $fil | grep DateTimeOri | awk '{print $2"_"$3 }' | sed s%:%_%g)"; mv -v $fil $datepath.jpg; done
2013-08-02 01:42:04
Functions: mv
0

Requires ImageMagick.

Extracts date taken from image and renames it properly.

Based on StackOverflow answer.

mogrify -resize SIZE_IN_PIXELS *.jpg
2013-07-05 14:14:04
User: o0110o
-1

Batch resize all images to a width of 'X' pixels while maintaing the aspect ratio.

This makes uses of ImageMagick to make life easier.

convert infile.png \( +clone -channel GB -evaluate set 0 +channel \) +append -region 50%x100% -channel R -evaluate set 0 +channel outfile.png
2013-04-30 20:46:02
User: bugmenot
Functions: set
0

Converts red-cyan 3D anaglyphs to side-by-side format viewable by crossing your eyes. Useful for people who don't have 3D glasses or prefer not to use one.

convert {1..12}.png MyMultiPageFile.pdf
2013-03-26 01:30:38
User: lpanebr
0

Creates a PDF from multiple images. One page per image.

If you want a specific arbitrary order you can use {1,3,5,10,12}

* you may use jpg, tif etc

** if you do use jpg images you might want to add "-compress Zip" as suggested below to prevent from having the images from being re-compressed.

for file in `ls *.png`;do convert $file -resize 65% new_$file; done
import -quality 90 screenshot.png
2013-02-07 01:38:59
User: TuxLyn
1

Now try this. Ones you see small cross arrow, double click on any window you like to make a screenshot "selectively".

import -window root -quality 98 screenshot.png
mogrify -crop <width>x<height>+<X-offset>+<Y-offset> *.png
mogrify -transparent white image*.png
2013-01-23 16:58:24
User: michelsberg
2

mogrify can be used like convert. The difference is that mogrify overwrites files:

http://www.imagemagick.org/www/mogrify.html

Of course, other source colors can be used as well.

for file in *.jpg; do identify -format '%f %b %Q %w %h' $file; done
2012-11-16 10:06:35
User: phattmatt
Functions: file
0

Runs the identify command (from ImageMagick) on each jpg file in the current directory and returns image details according to the format parameter. The example here returns:

Filename FileSize Compression Width Height

More information about the available format options can be found here: http://www.imagemagick.org/script/escape.php

I usually redirect the output to a text file using "> listofdetails.txt" at the end. Spreadsheet magic can then be applied.

convert -size 100x100 xc:grey nopic100_100.jpg
2012-06-15 11:23:02
User: theduke
0

Quickly create a new image with a certain size and background color.

convert image1 image2 -resize '400x300!' MIFF:- | compare -metric AE -fuzz '10%' - null:
2012-04-17 11:25:34
User: dooblem
2

Outputs the number of different pixels.

2 params to increase tolerance:

* thumbnails size

* fuzz, the color distance tolerance

See http://en.positon.org/post/Compare-/-diff-between-two-images for more details.

identify -format "%[fx:w]x%[fx:h]" logo:
2012-02-19 08:29:56
User: dratini0
1

You can use the -format switch to get the size of the image. Replace "logo:" with your image.

identify image.jpg |grep -o "[[:digit:]]*x[[:digit:]]*" |tail -1
mogrify -colorspace Gray -rotate '-90>' -resize 600x800 -dither FloydSteinberg -colors 16 -format png *
2012-02-01 17:16:52
User: yoyoyojomo
0
optipng *.png

http://www.mobileread.com/forums/showpost.php?p=147076&postcount=7

-dither FloydSteinberg produces a more uniform dither than the default.

for fn in xkcd*.png xkcd*.jpg; do echo $fn; read xw xh <<<$(identify -format '%w %h' $fn); nn="$(echo $fn | sed 's/xkcd-\([^-]\+\)-.*/\1/')"; wget -q -O xkcd-${nn}.json http://xkcd.com/$nn/info.0.json; tt="$(sed 's/.*"title": "\([^"]\+\)",.*/\1/' ...
2012-01-06 20:26:11
User: fpunktk
Functions: echo read wget
-2

full command:

for fn in xkcd*.png xkcd*.jpg; do; echo $fn; read xw xh <<<$(identify -format '%w %h' $fn); nn="$(echo $fn | sed 's/xkcd-\([0-9]\+\)-.*/\1/')"; wget -q -O xkcd-${nn}.json http://xkcd.com/$nn/info.0.json; tt="$(sed 's/.*"title": "\([^"]*\)", .*/\1/' xkcd-${nn}.json)"; at="$(sed 's/.*alt": "\(.*\)", .*/\1/' xkcd-${nn}.json)"; convert -background white -fill black -font /usr/share/fonts/truetype/freefont/FreeSansBold.ttf -pointsize 26 -size ${xw}x -gravity Center caption:"$tt" tt.png; convert -background '#FFF9BD' -border 1x1 -bordercolor black -fill black -font /usr/share/fonts/truetype/freefont/FreeSans.ttf -pointsize 16 -size $(($xw - 2))x -gravity Center caption:"$at" at.png; th=$(identify -format '%h' tt.png); ah=$(identify -format '%h' at.png); convert -size ${xw}x$(($xh+$th+$ah+5)) "xc:white" tt.png -geometry +0+0 -composite $fn -geometry +0+$th -composite at.png -geometry +0+$(($th+$xh+5)) -composite ${fn%\.*}_cmp.png; echo -e "$fn $nn $xw $xh $th $ah \n$tt \n$at\n"; done

this assumes that all comics are saved as xkcd-[number]-[title].{png|jpg}.

it will then download the title and alt-text, create pictures from them, and put everything together in a new png-file.

it's not perfect, but it worked for nearly all my comics.

it uses the xkcd-json-interface.

though it's poorly written, it doesn't completely break on http://xkcd.com/859/

awk 'BEGIN{FS=":"; print "digraph{"}{split($4, a, ","); for (i in a) printf "\"%s\" [shape=box]\n\"%s\" -> \"%s\"\n", $1, a[i], $1}END{print "}"}' /etc/group|display
2011-12-04 01:56:44
Functions: awk printf
19

Parses /etc/group to "dot" format and pases it to "display" (imagemagick) to show a usefull diagram of users and groups (don't show empty groups).

lsmod | awk 'BEGIN{print "digraph{"}{split($4, a, ","); for (i in a) print $1, "->", a[i]}END{print "}"}'|display
2011-12-04 01:41:23
Functions: awk lsmod
15

parse "lsmod" output to "dot" format and pass it to "display". Without perl!

read -s PASS; echo $PASS | convert sensitive.jpg -encipher - -depth 8 png24:hidden.png
2011-11-25 18:15:23
User: kev
Functions: echo read
Tags: ImageMagick
0

Do not use JPEG, GIF, or any other 'lossy' image encoding with Encryption

convert -resize '1024x600^' image.jpg small-image.jpg
2011-10-14 22:34:14
User: aguslr
Tags: ImageMagick
13

This command will resize an image (keeping the aspect ratio) to a specific resolution, meaning the resulting image will never be smaller than this resolution.

For example, if we have a 2048x1000 image, the output would be 1229x600, not 1024x600 or 1024x500. Same thing for the height, if the image is 2000x1200, the output would be 1024x614.

find . -type f -name '*.jpg' -exec convert -quality 75 {} {} \;
2011-09-23 13:51:18
User: bobby_tables
Functions: find
0

Requires ImageMagick to be installed.

This command was stolen from @climagic on Twitter.

Probably a duplicate of command below, but this command uses slightly higher quality.

http://www.commandlinefu.com/commands/view/707/compress-images-using-convert-imagemagick-in-a-bulk

mogrify -resize 600x800! *.jpg
2011-09-09 18:19:59
User: totti
0

- Backup data before reszie as it over write original

-To preserve aspect ratio remove !

convert -background none -pointsize 55 label:"`whoami`" me.png
2011-09-07 17:06:38
User: totti
1

Create transparent image to use as icon, watermark for other images, etc.