Monday, December 15, 2014

Batch replacing color with transparency


I have a very large number of PNG images (too many to practically do one at a time) with a bright pink background and would like to replace the background color with transparency. If this is not possible or particularly difficult, replacing the specified color with white would be useful.


I have gimp but not photoshop and am not particularly comfortable with command line, so if a solution requires that please show each step. It'd be great to be able to do this, any help would be appreciated.



Answer



I know you said you're not comfortable with command line tools, but ImageMagick can do this:



Replace a Specific Color


convert balloon.gif -transparent blue balloon_trans.gif


enter image description here enter image description here enter image description here




Where balloon.gif is the source image, -transparent specifies that you want a transparent bg, blue is the color you want to replace, and balloon_trans.gif is the completed image.


This is under the assumption that your "bright pink" is not part of any of the images. For a little more intelligent background removal (floodfill), check out this tutorial: Masking Simple Backgrounds (floodfill)


This can get a little hairy and some of the options they specify there probably aren't required for what you're trying to do, so as @graphics man suggests you can add background to the command




If you can tell me what OS you're using and a sample image, I can help you with a script to automate this for many images




Specific to your situation:


First, install ImageMagick. You may need to restart after installation for your PATH variable to be updated (I did)


Once it's installed, fire up a command prompt (Start Menu, type cmd, hit enter)


You need to change directories to where the images are saved. For your situation, enter the following command:



cd C:\Users\Eric\Pictures\pngs\logos


Now we need to make a folder to save all the images in:


mkdir batch


And now, the command to convert all the images:


FOR %G IN (*.png) DO convert "%G" -transparent #FF00FF "batch\%G"


Note: With ImageMagick v7, use magick instead of convert, as so


FOR %G IN (*.png) DO magick "%G" -transparent #FF00FF "batch\%G"


If everything went smoothly, the newly saved images should be in the batch folder you created.


enter image description here enter image description here enter image description here


No comments:

Post a Comment

technique - How credible is wikipedia?

I understand that this question relates more to wikipedia than it does writing but... If I was going to use wikipedia for a source for a res...