Page 1 of 1

Convert .png image, fill white background and keep original name

Posted: 2020-02-07T10:00:28-07:00
by sakis_s
Hi,
I'm trying to figure out how can i convert multiple photos at once from .png format to .jpg, fill white color background and keep the original filenames. All in one command.

I tried multiple things but couldn't make it work in one command.

This command fills white background perfectly but it's is for one photo, naming it manually:

Code: Select all

magick composite -compose Dst_Over -tile pattern:gray100 "C:\aa\1.png" "C:\aa\1.jpg"
The closest command i got was this one:

Code: Select all

convert *.png -set filename:base "%[basename]" "%[filename:base].jpg"
This converts all the photos and keeps same file names but i couldn't make it add white background when converting. I tried adding "-fill white" or "-background white" but nothing worked.

Can someone help me write the correct command?
Thank you in advance for your time.

Re: Convert .png image, fill white background and keep original name

Posted: 2020-02-07T10:24:55-07:00
by snibgo
sakis_s wrote:This command fills white background perfectly but it's is for one photo,...
I suggest you put that command inside a shell "for" loop, so that loops through all your files, executing "magick" for each one.

Re: Convert .png image, fill white background and keep original name

Posted: 2020-02-07T10:39:23-07:00
by fmw42
If you are trying to make transparency turn to white in your png to jpg conversion, then just use mogrify. It will convert a whole folder of files.

Lets say your images are in directory xx. First make a new directory, xxx. Now hange directories to xx. The run the command

Code: Select all

mogrify -path path_to/xxx -format jpg -background white -alpha background -alpha off *.png
where path_to is the path to directory xxx.

Alternately make a backup of your directory xx and leave out the -path path_to/xxx argument. Mogrify will then write over your input images with your output ones.


Or if you have enough RAM to hold all your images, you can do:

Code: Select all

convert *.png -set filename:base "%[basename]" -background white -alpha background -alpha off "%[filename:base].jpg"

Re: Convert .png image, fill white background and keep original name

Posted: 2020-02-07T11:21:47-07:00
by sakis_s
fmw42 wrote:
2020-02-07T10:39:23-07:00
If you are trying to make transparency turn to white in your png to jpg conversion, then just use mogrify. It will convert a whole folder of files.

Lets say your images are in directory xx. First make a new directory, xxx. Now hange directories to xx. The run the command

Code: Select all

mogrify -path path_to/xxx -format jpg -background white -alpha background -alpha off *.png
where path_to is the path to directory xxx.

Alternately make a backup of your directory xx and leave out the -path path_to/xxx argument. Mogrify will then write over your input images with your output ones.


Or if you have enough RAM to hold all your images, you can do:

Code: Select all

convert *.png -set filename:base "%[basename]" -background white -alpha background -alpha off "%[filename:base].jpg"
Thank you so much fmw42, that was really helpful!
One more thing. If i use command

Code: Select all

mogrify -path path_to/xxx -format jpg -background white -alpha background -alpha off *.png
what should i add to compress the output .jpg image to 50% ?

Re: Convert .png image, fill white background and keep original name

Posted: 2020-02-07T11:28:43-07:00
by fmw42
It depends upon what you mean by 50%. -quality 50 sets the compression. -define jpeg:extent sets a specific file size in kb (not percent)

See

https://imagemagick.org/script/command- ... hp#quality
https://imagemagick.org/Usage/formats/#jpg_write

Re: Convert .png image, fill white background and keep original name

Posted: 2020-02-07T12:18:35-07:00
by sakis_s
fmw42 wrote:
2020-02-07T11:28:43-07:00
It depends upon what you mean by 50%. -quality 50 sets the compression. -define jpeg:extent sets a specific file size in kb (not percent)

See

https://imagemagick.org/script/command- ... hp#quality
https://imagemagick.org/Usage/formats/#jpg_write
-quality 50 was exactly what i wanted. Thank you so much for your help! I really learnt a lot!