7

Suppose I have a image named 1.png which is currently

500px : height
1000px : width

I want to resize it to:

50px : height
100px : width

It must be output in PNG format, not JPG. An example would be highly appreciated.

Braiam
  • 35,991
  • Your best bet is Imagemagick – r004 Jun 26 '14 at 11:47
  • If the idea is to create thumbnails see http://www.imagemagick.org/Usage/thumbnails/ and the various thumbnailer applications (see apt-cache search thumbnailer on a Debian-based system for instance). – Stéphane Chazelas Jun 26 '14 at 12:30

4 Answers4

17

I'd use convert or mogrify from the ImageMagick suite.

$ convert -resize 100x50 1.png 2.png

# or #

$ mogrify -resize 100x50 1.png

convert takes a separate output filename; creating a separate file.
mogrify doesn't take a separate output filename; modifying the file in place

voices
  • 1,272
choroba
  • 47,233
  • If you don't know the original size of the photo, but just want to downsize it by 50% in each dimension, how can you do that? – BallpointBen Mar 30 '20 at 14:57
  • 1
    @BallpointBen: It understands percents: convert -resize 50%x50% 1.png 2.png, or just convert -resize 50% 1.png 2.png. – choroba Mar 30 '20 at 15:07
6

The answers you have gotten so far will work in this particular case because your source and target images have the same aspect ratio. However, if you want to change to an arbitrary size, they will fail:

$ file foo.png 
foo.png: PNG image data, 1000 x 500, 8-bit/color RGB, non-interlaced
$ convert -resize 100x50 foo.png bar.png
$ file bar.png 
bar.png: PNG image data, 100 x 50, 8-bit colormap, non-interlaced

As you can see above, the simple convert works fine when not changing the image's proportions. But what if you want to change them?

$ convert -resize 200x50 foo.png bar.png
$ file bar.png 
bar.png: PNG image data, 100 x 50, 8-bit colormap, non-interlaced

So, when changing the proportions, the command above fails. In order to force convert to change an image this way, you need to add a ! to the end of the geometry specification (and, since ! is a special character to many shells, you need to escape it as \!):

$ convert -resize 200x50\! foo.png bar.png
$ file bar.png 
bar.png: PNG image data, 200 x 50, 8-bit colormap, non-interlaced
terdon
  • 242,166
3

Use Imagemagick for this.

Read the man page for correct use but it should work by passing parameters, something like

convert 1.png -resize 50x100 1-resized.png

terdon
  • 242,166
Marko
  • 161
  • At least on Debian, the man pages for ImageMagick tools are useless (except at pointing out where to find the real information like file:///usr/share/doc/imagemagick/www/convert.html or http://www.imagemagick.org/script/convert.php (though that latter one might be for a different version of ImageMagick)). – Stéphane Chazelas Jun 26 '14 at 12:15
  • The geometry argument is actually (in it's simplest form) "widthxheight", so it should be -resize 100x50 – Popup Jun 26 '14 at 12:50
2

An alternative to ImageMagick is the venerable netpbm:

pngtopnm input.png | pnmscale -reduce 10 | pnmtopng > output.png