Recursive convert png to jpg

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
agriz
Posts: 237
Joined: 2011-10-01T02:21:30-07:00
Authentication code: 8675308

Recursive convert png to jpg

Post by agriz » 2017-11-29T11:17:51-07:00

the folder structure is img/year/month/date/random_no

img/2017/11/1/100
img/2017/11/1/574
img/2017/11/1/435
img/2017/11/2/43
..

Inside that last folder, i have few pictures in png. It seems jpg sizes are lesser than png. I dont have any image with transparent pixels.
How do i convert all the files from png to jpg without changing the original name and path?

User avatar
fmw42
Posts: 22464
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Recursive convert png to jpg

Post by fmw42 » 2017-11-29T11:32:43-07:00

Please always provide your IM version and platform!

You can use mogrify

Code: Select all

cd img/2017/11/2/43
mogrify -format jpg *.png
But then you will have two sets -- one with png and one with jpg.

I would suggest that you create a new directory to hold the converted files. Then once it works, you can delete the old directory and rename the new one.

See -path option for mogrify at http://www.imagemagick.org/Usage/basics/#mogrify.

Alternately, you could write a script loop over each image the directory and use

Code: Select all

convert image.png image.jpg
rm -f image.png

agriz
Posts: 237
Joined: 2011-10-01T02:21:30-07:00
Authentication code: 8675308

Re: Recursive convert png to jpg

Post by agriz » 2017-11-29T12:17:54-07:00

Version: ImageMagick 6.9.3-7 Q16 x64 2016-03-06 http://www.imagemagick.org

Do i need to use "cd img/2017/11/2/43" each and every time?
There are around 400 sub directories

User avatar
fmw42
Posts: 22464
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Recursive convert png to jpg

Post by fmw42 » 2017-11-29T13:34:35-07:00

Yes, mogrify only works on one directory at a time. So, you will need to write a script loop over a list of directories and call mogrify for each directory.

agriz
Posts: 237
Joined: 2011-10-01T02:21:30-07:00
Authentication code: 8675308

Re: Recursive convert png to jpg

Post by agriz » 2017-11-30T06:34:38-07:00

Code: Select all

<?php

$convert = "C:/ImageMgaick/mogrify.exe";
$location = "img/2017/11/23";



for($i = 1; $i <= 10; $i++) {
	
	if(is_dir($location."/".$i)) {
	
		echo "Directory : ".$i;
		echo "<br/>";
		echo "Full Directory :".$location.'/'.$i;
		echo "<br />"	;
	
		exec($convert." -path ".$location.'/'.$i." -format jpg *.png 2>&1", $output, $return_var);
		echo "<pre>";
			print_r($output);
			print_r($return_var);
		echo "</pre>";
	
	}
}
?>
There is no directory from 1 to 9.
Directory : 10
Full Directory :img/2017/11/23/10

Array
(
[0] => The system cannot find the path specified.
)
1

I tried the following code in windows pc.

agriz
Posts: 237
Joined: 2011-10-01T02:21:30-07:00
Authentication code: 8675308

Re: Recursive convert png to jpg

Post by agriz » 2017-11-30T07:16:03-07:00

E:\photos>C:/ImageMgaick/mogrify -path "img" -format jpg *.png

This one is also reporting "The system cannot find the path specified."

agriz
Posts: 237
Joined: 2011-10-01T02:21:30-07:00
Authentication code: 8675308

Re: Recursive convert png to jpg

Post by agriz » 2017-11-30T07:30:06-07:00

There was a mistake in my coding.
I wrongly typed "ImageMagick" Path in command.

Now i am getting,
E:/photos>C:/ImageMagick/mogrify.exe -path "img/2017/11/23/10" -quality 60 -format jpg *.png
Array
(
[0] => mogrify.exe: unable to open image `*.png': Invalid argument @ error/blob.c/OpenBlob/2702.
[1] => mogrify.exe: unable to open file `*.png' @ error/png.c/ReadPNGImage/3913.
)

snibgo
Posts: 9595
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Recursive convert png to jpg

Post by snibgo » 2017-11-30T07:45:06-07:00

That's the message you get if you have no *.png files in that directory.

"-path" is the location for the output files, not the inputs.
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 22464
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Recursive convert png to jpg

Post by fmw42 » 2017-11-30T10:42:59-07:00

the directory used by -path must exist already, also.

Post Reply