Linux convert all images with

How can I convert JPG images recursively using find?

Essentially what I want to do is search the working directory recursively, then use the paths given to resize the images. For example, find all *.jpg files, resize them to 300×300 and rename to whatever.jpg. Should I be doing something along the lines of $(find | grep *.jpg) to get the paths? When I do that, the output is directories not enclosed in quotation marks, meaning that I would have to insert them before it would be useful, right?

2 Answers 2

Lets say, I need everything inside my nested folder/another/folder/*.jpg to be in *.png

find . -name «*.jpg» -print0|xargs -I<> -0 mogrify -format png <>

&& with a bit of explaination:

find . -name *.jpeg — to find all the jpeg’s inside the nested folders.
-print0 — to print desired filename withouth andy nasty surprises (eg: filenames space seperated)
xargs -I <> -0 — to process file one by one with mogrify

and lastly those <> are just dummy file name for result from find.

You can use something like this with GNU find:

find . -iname \*jpg -exec /your/image/conversion/script.sh <> + 

This will be safer in terms of quoting, and spawn fewer processes. As long as your script can handle the length of the argument list, this solution should be the most efficient option.

If you need to handle really long file lists, you may have to pay the price and spawn more processes. You can modify find to handle each file separately. For example:

find . -iname \*jpg -exec /your/image/conversion/script.sh <> \; 

Источник

Читайте также:  Programs that will run on linux

Bulk converting images from one format to another?

I want to convert a batch of images, nearly 100, from jpg to png format. How can I do this without renaming them, but instead actually converting the format?

Use the convert command. No, really. But you will want to rename them from something.jpg to something.png.

I wasn’t finished editing 😉 First, use the convert command on a single .jpg file and see if it works. Then, write a script that loops over the .jpg files and converts them to .png files.

You not mentioned how can I use the convert command on a single .jpg file. And I don’t know how to write a script that loops over the .jpg files.

4 Answers 4

mogrify -format png /path/*.jpg 

This will convert all the .jpg files into .png files and saves the converted files in the same directory.

This will moves all the .png files(converted) to the pic directory which resides on the Desktop.

If you want to keep the orientation of your image you have to add -auto-orient to the command. You can find out why here. The mogrify command which keeps the orientation would look like this:

mogrify -auto-orient -format png /path/*.jpg 

I also tried this command after changing the directory: mogrify -format png .jpg This also works nicely.

Using ImageMagick.

First install imagemagick:

sudo apt-get install imagemagick 

Try converting just one image at first:

convert image.jpg image.png 

You also need to split it into chunks that will fit to avoid hitting the limit of how much you can put on a command line. This should work better:

find -name '*.jpg' -print0 | xargs -0 -r mogrify -format png 

The -print0 and -0 are used to handle spaces in filenames and the -r means don’t run mogrify if there’s nothing to do.

Читайте также:  Hp pavilion g6 драйвера linux

EDIT 2 Switched png and jpg as per @Glutanimate’s comment.

EDIT 3 Changed png to jpg in last suggestion.

Источник

ImageMagick

Most operations will use the convert command. To convert an image into another format, you essentially rename your image with the desired file extension.

convert image1.png image1.jpg 

To resize images, use the -resize option.

convert image1.png -resize 200×100 image1.png 

Note that when using -resize , ImageMagick will preserve the image’s aspect ratio and fit it into an image with the specified dimensions. To force an image to a particular size, append an ! to the dimensions.

convert image1.png -resize 200×100! image1.png 

Rotate images with the -rotate option, using degrees. The following command would rotate an image 90 degrees.

convert image1.jpg -rotate 90 image1-rotated.jpg 

Since this is a command-line tool, you can take advantage of Bash and perform bulk operations. The following command would take all PNG files in the current directory, rotate them, and save a new copy of each with “-rotated” added to the beginning of each file name.

for file in *.png; do convert $file -rotate 90 rotated-$file; done 

During any given operation, if the name of your output image is the same as the input image, the operation will overwrite the original image. Otherwise a new image will be created.

You can find more complete documentation here on the ImageMagick website.

Limitations

  • Convert an entire PDF file to a bunch images
  • Extract an image from a Windows .ico file

I am unsure if these operations are possible.

  • Convert images to DPX, EXR, GIF, JPEG, JPEG-2000, PDF, PhotoCD, PNG, Postscript, SVG, TIFF, and other formats

I am unsure if all those file formats are supported by ImageMagick

Читайте также:  Alt linux ssh root

Источник

How can I convert a series of images to a PDF from the command line on Linux? [closed]

I have a scanning server I wrote in CGI and Bash. I want to be able to convert a bunch of images (all in one folder) to a PDF from the command line. How can that be done?

Use img2pdf, not ImageMagick. ImageMagick decodes the JPEG, resulting in generation loss and is 10–100 times slower than img2pdf.

img2pdf $(find . -iname ‘*.jpg’ | sort -V) -o ./document.pdf will give you document.pdf containing all images with jpg or JPG extension in the current dir — one image per page. document.pdf will have all images ordered as pages naturally ( -V option for sort ) so there is no need to add any leading zeros when numbering image files.

2 Answers 2

what if page*.png does not sort the images in the way you want ? e.g. page_1.png, page_2.png . page_10.png -> page_10 will appear before page_1

To sort the files, you can use: ls page*.png | sort -n | tr ‘\n’ ‘ ‘ | sed ‘s/$/\ mydoc.pdf/’ | xargs convert

FYI you almost never need to use ls for anything apart from displaying files. i.e. do not parse it’s output. find is a much more suitable tool. Here is an example convert $(find -maxdepth 1 -type f -name ‘page*.png’ | sort -n | paste -sd\ ) output.pdf . Keep in mind that the aforementioned command will not work if your pathnames contain spaces. The addition of characters that need to be escaped makes things a little more complicated.

This is simple and works very well, thank you! To avoid generating huge PDF files, use something like convert -compress jpeg -quality 85 *.png out.pdf

ImageMagick decodes the JPEG, resulting in generation loss. Use img2pdf instead; it’s also 10–100 times faster.

Источник

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