Linux convert resize image

How to batch resize images in Ubuntu recursively within the terminal?

I have multiple images stored in a set of organized folders. I need to re-size those images to a specific percentage recursively from their parent directory. I am running Ubuntu 11.10 and i prefer learning how to do that directly from the terminal.

9 Answers 9

You could use imagemagick. For instance, for resizing all the JPG images under the current directory to 50% of their original size, you could do:

for f in `find . -name "*.jpg"` do convert $f -resize 50% $f.resized.jpg done 

The resulting files will have «.jpg» twice in their names. If that is an issue, you can check the following alternatives.

For traversing/finding the files to resize, you can use xargs too. Example:

find . -name "*.jpg" | xargs convert -resize 50% 

This will create copies of the images. If you just want to convert them in place, you can use:

find . -name "*.jpg" | xargs mogrify -resize 50% 

@CompilingCyborg I changed the answer so that it handles recursion in the subfolders of the current folder, looking for all jpg images.

@horsh thanks, I realize now that a way to deal with recursion was explicitly asked by the OP. I fixed by answer to include recursion.

Great script. If using mogrify take care to do: mogrify -resize 50% $f and not mogrify $f -resize 50%

Extending the answer from @betabandido

Incase there are spaces in filenames or folder names in which the images are, then one should use -print0 with find and -0 with xargs to avoid any parsing errors.

find . -name "*.jpg" -print0 | xargs -0 convert -resize 50% find . -name "*.jpg" -print0 | xargs -0 mogrify -resize 50% 
sudo apt install imagemagick sudo apt-get install nautilus-image-converter nautilus -q 

For resizing/rotating images in the current folder. You just install and then right click on an image or multiple ones and choose the size you want and that’s it. The nautilus -q is to stop nautilus. Just start nautilus again, and you’ll be able to use the image converter.

Hey! This should be the accepted answer. It’s so convenient to use it. Similar answer here: askubuntu.com/questions/1053081/…

I’m not sure why this is not upvoted more. I’m a programmer, so comfortable with the command line, but when I just want to resize some pictures, having a UI is way easier than looking up the imagemagick commands every time

Читайте также:  Smart board driver linux

It’s also works if you give the new resize resolution :

convert $f.jpg -size 1024x768 $f.resized.png 

You can use imagemagick tool for batch resize.

It will maintain the aspect ratio

$ convert dragon.gif -resize 64x64 resize_dragon.gif 

It will not maintain the aspect ratio

$ convert dragon.gif -resize 64x64\! exact_dragon.gif $ cat resize.sh #!/bin/bash for f in `find . -name "*.jpg"` do convert $f -resize 45x60\! $f.resize.jpg done 

It will resize the image to 45×60 without maintaining the aspect ratio in current directory.

I modified the code of the accepted answer a little bit to include png files and add a prefix instead of a postfix to the file name to make it easier to select all resized files at once.

for p in `find . -name "*.jpg" -o -name "*.png"` do d=$ f=$ b=$ e=$ convert $p -resize 33% $d/thumb.$b.$e done 

With a little modification it is possible to recreate the directory structure to a separate directory and have only resized files with the same directory structure. Another option is flattening the directory structure and having randomly generated file names and collect the path mapping to a CSV file by each thumb file with append.

there are a few answers like:

find . -name "*.jpg" | xargs convert -resize 50% 

this won’t work as it will expand the list like this: convert -resize 50% a.jpg b.jpg c.jpg which will resize a.jpg in c-0.jpg , b.jpg in c-1.jpg and let c.jpg untouched.

So you have to execute the resize command for each match, and give both input file name and output file name, with something like:

find . -name "*.jpg" | xargs -n 1 sh -c 'convert -resize 50% $0 $(echo $0 | sed 's/\.jpg/-th\.jpg/')' 

each match of find is individually passed by xargs -n 1 to the resize script: sh -c ‘convert -resize 50% $0 $(echo $0 | sed ‘s/\.jpg/-th\.jpg/’)’ . This script receives the file name in argument $0 , uses sed to make an output file name by substitution of the original .jpg suffix by a -th.jpg one. And it runs the convert command with those two file names.

Here is the version without xargs but find -exec :

find -name '*.jpg' -exec sh -c 'convert -resize 50% $0 $(echo $0 | sed 's/\.jpg/-th\.jpg/')' <> \; 

Источник

Command-line Basics: Resizing Images with ImageMagick

Command-line Basics: Resizing Images with ImageMagick

If you’ve ever done programmatic image manipulation, you have probably encountered the ImageMagick library or its major fork, GraphicsMagick. Essentially, ImageMagick is the most commonly-used program for resizing, converting, or otherwise manipulating images on the command line. It has libraries for integration into almost all popular programming languages , and you can use it directly with its included commands, mogrify and convert .

Читайте также:  Linux gcc kernel module

Prerequisites

Before you begin this guide, you should have a regular, non-root user with sudo privileges configured on your server. You can learn how to configure a regular user account by following our Initial server setup guide for Ubuntu 22.04.

Getting started

The ImageMagick library is very popular, but doesn’t usually come installed by default. However, it is widely available in package managers for all platforms. On Ubuntu 22.04, you can install it with apt .

First, update your package sources:

If you don’t already have a sample image handy to work with, you can download the header image from this tutorial using curl , and save it as image.jpg :

    curl https://images.prismic.io/digitalocean/0b619d51-a723-4748-997f-39ed5697a540_intro-to-cloud.jpg?auto=compress,format —output image.jpg

Resize to specific dimensions and keep aspect ratio

To resize an image to specific dimensions, use the convert command with an input file, the -resize parameter, your preferred dimensions, and an output filename:

This won’t actually resize the image to the exact dimensions specified. What it will do is resize the image to fit within those dimensions.

You can also append ^ to the dimensions to tell ImageMagick that you’d like to resize the image to fill the dimensions, potentially overlapping on one side.

One of the two dimensions (either width or height) will be scaled exactly, while the other will be scaled proportionately and may overlap:

Resize to specific dimensions and keep the aspect ratio

Sometimes you’ll need to not only resize an image, but also crop it so there’s nothing overlapping. A good use case for this is user avatars. You should avoid stretching or squashing a user’s profile picture, so cropping it to a square is an acceptable solution:

Note: In order to resize an image to arbitrary dimensions while ignoring the aspect ratio, you can append ! to the dimensions, like 100×100! . Most times, you will not want to do this, as it will stretch the image.

If needed, you can change the file extension of your output file, for example from .png to .jpg , and ImageMagick will convert your image to that format. You can use ImageMagick this way even without resizing or otherwise modifying an image. Refer to the ImageMagick documentation for a more complete list of options.

Resizing files in place

Thus far, we’ve been converting a file and saving it to a brand new file. While being a safer option, as it’s non-destructive to your original file, this is not always an ideal workflow.

If you need to edit a file in place, you can swap the convert command with mogrify . The mogrify command accepts an input file that will be modified in place.

Читайте также:  Linux исправить ошибки пакетов

Note: You are highly encouraged to make back ups of any images you are modifying in case you aren’t happy with the results.

Here’s the “ mogrify ed” versions of the previous commands:

Resizing multiple files

mogrify can also operate on an entire directory of files at once, unlike convert . In general, both ImageMagick commands use similar syntax, but convert only works with a single specified input and output.

To edit an entire directory of images and resize them all in the same way, you can pass mogrify a * wildcard:

Conclusion

Resizing images from the command-line is really just the tip of the iceberg. ImageMagick supports many additional options that allow you to optimize images, resample their colors, and convert them to other formats.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Источник

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

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.

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

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.

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):

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.

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

python resizescript.py yourfilenamehere.jpg 
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 128×128 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.

Источник

Оцените статью
Adblock
detector