How to resize an image through the terminal? [duplicate]

I have Ubuntu 10.04. I have converted image through terminal using the following command:

convert myfigure.png myfigure.jpg

But I want to resize the height and width of the converted image. Is there any way to do this?


Same command, with an extra option:

convert myfigure.png -resize 200x100 myfigure.jpg

or

convert -resize 50% myfigure.png myfigure.jpg

To resize multiple files, you can try the following command (as suggested by @test30)

find . -maxdepth 1 -iname "*.jpg" | xargs -L1 -I{} convert -resize 30% "{}" _resized/"{}"

If you want CLI only:

sudo apt-get install imagemagick
mogrify -resize 320x240 Image.png 
mogrify -resize 50% Image.png
mogrify -resize 320x240 *.jpg

If you wanna try GUI:

Install nautilus-image-converter

sudo apt-get install nautilus-image-converter

It adds two context menu items in nautlius so you can right click and choose "Resize Image".(The other is "Rotate Image").

You can do a whole directory of images in one go if you like and you don't even have to open up an application to do so.


Since Ubuntu ships with Python, you can also use a Python script to achieve this with a little more control over what happens - see this stackoverflow question for example scripts. Those examples use just the standard library.

Script #1

import os, sys
import Image

size = 128, 128

for infile in sys.argv[1:]:
    outfile = os.path.splitext(infile)[0] + ".thumbnail"
    if infile != outfile:
        try:
            im = Image.open(infile)
            im.thumbnail(size, Image.ANTIALIAS)
            im.save(outfile, "JPEG")
        except IOError:
            print "cannot create thumbnail for '%s'" % infile

And another example where you only have to specify the width (as the width variable):

Script #2

from PIL import Image
import sys

filename = sys.argv[1:]
basewidth = 300
img = Image.open(filename)
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
img.save(filename) 

Now, how to do this through the terminal...

sudo nano resizescript.py

Paste one of those blocks of code into the text editor. Ctrl+x to exit (say yes to save changes).

To use Script #1:

python resizescript.py yourfilenamehere.jpg

To use Script #2:

python resizescript.py yourfilenamehere.jpg

You must be in the same directory as the picture files for both of these scripts. The first one shrinks the image to 128x128 pixels. The second script makes it 300 pixels wide and calculates the proportional height. This is more of a Python answer, but it is done all through the terminal technically.


imgp is a relatively new utility that does image resize and rotation. It has more features than nautilus-image-converter.

For example:

imgp -x 1366x768 *