243

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?

karel
  • 110,292
  • 102
  • 269
  • 299
Md Kutubuddin Sardar
  • 2,571
  • 2
  • 14
  • 8

4 Answers4

343

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/"{}"
kenorb
  • 9,995
  • 2
  • 78
  • 90
Rinzwind
  • 293,910
  • 41
  • 570
  • 710
  • 16
    You can also use percentage, as in `convert -resize 50% myfigure.png myfigure.jpg` – January Mar 24 '13 at 09:48
  • @January excellent :) – Rinzwind Mar 24 '13 at 09:56
  • If I want to convert all images in a folder what's the best command then? does `convert -resize 50% *.JPG` work? tnx – Ehsan M. Kermani Sep 07 '14 at 19:30
  • 7
    resize all images in current dir by 50% `find -maxdepth 1 . -iname "*.jpg" | xargs -l -i convert -resize 50% {} /tmp/{}` src: https://www.perturb.org/display/632_ImageMagick_resize_images.html I added `maxdepth` :) – test30 Dec 10 '14 at 21:57
  • The character you have in the script, `×` is the ASCII character `0xD7` (multiplication sign); it needs to be an actual `x` for the code to work. See [_StackOverflow: Invalid argument for option '-resize' in shell script_](http://stackoverflow.com/q/26800248/617937) – IQAndreas Jan 18 '15 at 07:57
  • (I tried actually editing the answer to fix this, but the edit required at least 6 characters to be changed.) – IQAndreas Jan 18 '15 at 07:58
  • @IQAndreas thanks and edited :) (next time: try to add 5 spaces :D) – Rinzwind Jan 18 '15 at 10:11
  • Probably works on OSX if you're a developer (XCode &/or ImageMagick installed) – Seth Bro Jan 22 '15 at 09:28
  • 5
    Found that to do all the pictures the . needed to be before -maxdepth so it should be this: find . -maxdepth 1 -iname "*.JPG" | xargs -l -i convert -resize 25% {} email/{} – Andrew Stern Jul 09 '15 at 22:58
  • See also: https://guides.wp-bullet.com/batch-resize-images-using-linux-command-line-and-imagemagick/ – Gabriel Staples Jun 11 '20 at 07:47
  • Install `convert` with `sudo apt install imagemagick` – Boris Verkhovskiy Feb 10 '21 at 00:27
  • you can use a loop too to convert all images, for i in *.jpg; do convert $i -resize widthxheight $i; done; – ahmed galal May 16 '21 at 15:17
  • or `for f in *.jpg; do echo "Processing $f file.."; convert -resize 25% $f small$f; done` – Paloha Feb 16 '22 at 15:26
  • Well, this does not really work. The ratio of the picture remains the same. For example, converting 4x1 to 2x2 won't work. – foki Mar 14 '22 at 20:39
  • To make it work under my Ubuntu I had to change the order of the parameters for xarg like: `find . -maxdepth 1 -iname "*.png" | xargs -L1 -I{} convert "{}" -resize 30% ./small/"{}"` – Alex May 19 '22 at 11:51
62

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.

Maythux
  • 82,867
  • 54
  • 239
  • 271
  • 1
    Is there any more capable CLI software than this? It crashes and works on approx 80% images. – Luka Mar 29 '18 at 19:24
4

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 *
Eliah Kagan
  • 116,445
  • 54
  • 318
  • 493
Arun
  • 131
  • 3
3

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.

freeworld
  • 41
  • 5
  • please include example(s) here – Zanna Sep 10 '17 at 20:20
  • Please provide the example through the terminal as per question. – kenorb Sep 10 '17 at 21:10
  • Thanks, I assumed too much in my first answer. I have made the edits per question. – freeworld Sep 12 '17 at 00:05
  • Note that PIL is only available for Python 2; support for 3.x is planned "later" as per http://www.pythonware.com/products/pil/ – arp Jan 21 '18 at 16:13
  • For Python 3 there's Pillow - that's a fork of PIL. Confusingly enough it's in [Debian repositories](https://packages.debian.org/stretch/python3-pil) under `python3-pil` package. Aside from that, it's available via `pip3` package manager. – Sergiy Kolodyazhnyy Dec 02 '18 at 09:32