Skip to main content
Marinapomorac
Inspiring
October 19, 2021
Question

Remove ALL white that is JUST in the BG of an BW PNG image

  • October 19, 2021
  • 2 replies
  • 9593 views

Hello.

 

I have +5000 black and white PNG images with black drawings (mandalas) on them that have square dimensions but the image it self is not squared.

I need batch action that will:
a) Select all white around using magic wand
b) Invert selection
c) Crop image to selection
d) Change dimensions to 3000×3000px
e) Save as PNG transparent with 300px (the original is 300px)

Problems I am facing: 
- if mandala in original photo is touching the edge the magic wand only selects one corner of white. I hoped I can make multiple magic wand clicks in to action but it is not working. 

- save as PNG for some strange reason changes resolution to 96px

Windows 10 PC
Photoshop 22.5.1.441 (others have bugs, artboard is not transparent even if both settings are checked)

I tried using these:

 

and

I do not understand this one:
With Photoshop, how to batch replace white background with transparent in the image

 

and how to convert from tiff to PNG at the end. Why export to PNG is not acceptable?


This topic has been closed for replies.

2 replies

JJMack
Community Expert
Community Expert
October 19, 2021

In your action you could set your preferences units to percentage.  Then use the magic wand in the top left corner then hold the shift key and use the magic in the other corners to add to the selection. You could also use the wand in other edge area the you feel will never bet black.

JJMack
Marinapomorac
Inspiring
October 19, 2021

Thank you. Can you point me to the service store where I can hire someone to make this action for me start to end and that it works? I also need similar action to work in Illustrator Remove white from BW but keep the inside white SVG/AI
If I could hire someone to make these two actions that would help.
I do not use Photoshop on daily bases, just to batch process images. 

Legend
October 20, 2021

@JJMack @Lumigraphics 
Yes, I understand. As I said I do not use Photoshop to that extend I am familirazied with solving problems in actions. I created actions and I constantly get errors in select, delete, make or save as steps.
@JJMack Thank you for the action, I will test it and report back.

I asked same question in paint.net forum and I got this so if anyone in the future come to this post with same problem/need/question and there is still no working action for Photoshop:


a) Install ImageMagick C:\Program Files\ImageMagick-7.1.0-Q16-HDRI
b) Copy and save in Notepad with BAT extension this code:
@echo off
SET /p TOLERANCE="Tolerance (Default: 20): " || SET "TOLERANCE=20"
SET /p MAGICKDIR="ImageMagick Directory (Default: C:\Program Files\ImageMagick-7.1.0-Q16-HDRI): " || SET "MAGICKDIR=C:\Program Files\ImageMagick-7.1.0-Q16-HDRI"
SET /p POINT="Pick Color Point (Default: 0,0): " || SET "POINT=0,0"
SET /p INPUT_DIR="Input Directory: "
SET /p OUTPUT_DIR="Output Directory: "
@echo on
FOR %%a IN (%INPUT_DIR%\*.png) DO "%MAGICKDIR%\magick.exe" convert "%%~a" -gravity center -extent 105%%x105%% -fuzz %TOLERANCE%%% -fill transparent -draw "color %POINT% floodfill" -trim "%OUTPUT_DIR%\%%~nxa"
@echo "Done!"
PAUSE

c) Run the code

Images are cropped and transparent.

Second thing, resizing:
CODE FOR RESIZING (in this sample resizing is 3000×3000 and 300dpi)
@echo off
SET /p TOLERANCE="Tolerance (Default: 20): " || SET "TOLERANCE=20"
SET /p MAGICKDIR="ImageMagick Directory (Default: C:\Program Files\ImageMagick-7.1.0-Q16-HDRI): " || SET "MAGICKDIR=C:\Program Files\ImageMagick-7.1.0-Q16-HDRI"
SET /p POINT="Pick Color Point (Default: 0,0): " || SET "POINT=0,0"
SET /p INPUT_DIR="Input Directory: "
SET /p OUTPUT_DIR="Output Directory: "
@echo on
FOR %%a IN (%INPUT_DIR%\*.png) DO "%MAGICKDIR%\magick.exe" convert "%%~a" ^
-background white ^
-gravity center ^
-extent 105%%x105%% ^
-fuzz %TOLERANCE%%% ^
-fill transparent ^
-draw "color %POINT% floodfill" ^
-trim ^
-units PixelsPerInch ^
-density 300 ^
-filter box ^
-resize 3000x3000 ^
-background transparent ^
-gravity center ^
-extent 3000x3000 ^
"%OUTPUT_DIR%\%%~nxa"
@echo "Done!"
@echo off
PAUSE

 

I was shocked to see that free program and couple of lines of code can do this on 300 photos under a minute. 

 


ImageMagick is widely used behind the scenes for websites and commercial graphics processing. It can't do everything Photoshop can do but is definitely powerful for batch processing.

Stephen Marsh
Community Expert
Community Expert
October 19, 2021

@Marinapomorac wrote:


Problems I am facing: 
- if mandala in original photo is touching the edge the magic wand only selects one corner of white. I hoped I can make multiple magic wand clicks in to action but it is not working. 


Start recording the action with the following first:

 

1) Press Ctrl D (Default foreground/background colours of black/white)

2) Image > Canvas Size, add say 20px to the width/height with relative checked to expand the canvas before making selection

 

Now you should be able to remove the background with a single magic wand click in the upper left and crop to selection (double check the toolbar options for tolerance, contiguous on etc).

.

Marinapomorac
Inspiring
October 19, 2021

So you are saying that I should first treat all images with enlargment then use the BG removal action?

So I opened Photoshop and some random file then click on New action, record.

Then:
CTRL + D
X  - exchages swatches

Image - canvas size relative 20 20
Save
Close

That is action #1

Action 2 is:
Set selection
Inverse

Crop

Save

 

The first one worked, second one I get "The command "Select" is not currently available."

 

Marinapomorac
Inspiring
October 19, 2021

No need to exchange swatches if the background is white, I removed that from my post, sorry...


No pixels are selected.

 

 

I did something wrong, it added transparent 20px around.