Page 1 of 1

Recursive convert png to jpg

Posted: 2017-11-29T11:17:51-07:00
by agriz
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?

Re: Recursive convert png to jpg

Posted: 2017-11-29T11:32:43-07:00
by fmw42
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

Re: Recursive convert png to jpg

Posted: 2017-11-29T12:17:54-07:00
by agriz
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

Re: Recursive convert png to jpg

Posted: 2017-11-29T13:34:35-07:00
by fmw42
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.

Re: Recursive convert png to jpg

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

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.

Re: Recursive convert png to jpg

Posted: 2017-11-30T07:16:03-07:00
by agriz
E:\photos>C:/ImageMgaick/mogrify -path "img" -format jpg *.png

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

Re: Recursive convert png to jpg

Posted: 2017-11-30T07:30:06-07:00
by agriz
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.
)

Re: Recursive convert png to jpg

Posted: 2017-11-30T07:45:06-07:00
by snibgo
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.

Re: Recursive convert png to jpg

Posted: 2017-11-30T10:42:59-07:00
by fmw42
the directory used by -path must exist already, also.