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

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?".
Locked
sakis_s
Posts: 5
Joined: 2020-02-07T09:40:05-07:00
Authentication code: 1152

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

Post 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.

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

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

Post 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.
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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"

sakis_s
Posts: 5
Joined: 2020-02-07T09:40:05-07:00
Authentication code: 1152

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

Post 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% ?

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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

sakis_s
Posts: 5
Joined: 2020-02-07T09:40:05-07:00
Authentication code: 1152

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

Post 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!

Locked