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?
Recursive convert png to jpg
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Recursive convert png to jpg
Please always provide your IM version and platform!
You can use mogrify
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
You can use mogrify
Code: Select all
cd img/2017/11/2/43
mogrify -format jpg *.png
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
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
Do i need to use "cd img/2017/11/2/43" each and every time?
There are around 400 sub directories
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Recursive convert png to jpg
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
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>";
}
}
?>
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
E:\photos>C:/ImageMgaick/mogrify -path "img" -format jpg *.png
This one is also reporting "The system cannot find the path specified."
This one is also reporting "The system cannot find the path specified."
Re: Recursive convert png to jpg
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.
)
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.
)
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Recursive convert png to jpg
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.
"-path" is the location for the output files, not the inputs.
snibgo's IM pages: im.snibgo.com
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Recursive convert png to jpg
the directory used by -path must exist already, also.