Jump to content

Batch remove white BG around simple images


MarijaM

Recommended Posts

Hi. I am making mandala book with cats. I have them as PNG but the area around the cat is white. I have to open file, click anywhere outside with magic wand click delete and save, and this action I should perform +1000 times. Is there way to automate this? I tried in Photoshop with this photoshop BG removal but it leaves white between tail and body. 

I would appreciate any help. Thank you.

Windows 10
Latest paintNet

2_Cats (1).png

3_Cats (1).png

Link to comment
Share on other sites

Hi @MarijaM,

 

AFAIK, You can't do batch processing in Paint.NET. 

 

You can do it with ImageMagick though.

 

Steps:

Download & Install ImageMagick (My installation location is in C:\Program Files\ImageMagick-7.1.0-Q16-HDRI)

Make sure you've a backup copy of original images.

Save the code below into a .bat file and run it.

It'll ask you to enter some parameters.

 

@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" -fuzz %TOLERANCE%%% -fill transparent -draw "color %POINT% floodfill" "%OUTPUT_DIR%\%%~nxa"
@echo "Done!"
PAUSE

 

Parameters

Tolerance: Similar to magic wand tool's tolerance level.

ImageMagick Directory: ImageMagick installation directory

Point: Similar to magic wand tool's initialization point. ( Default value of 0,0 means top-left corner of the image )

Input Directory: Png files with backgrounds.

Output Directory: Png files without backgrounds will be saved into this directory.

  • Hugs 1
Link to comment
Share on other sites

On 10/18/2021 at 2:40 PM, otuncelli said:

Hi @MarijaM,

 

AFAIK, You can't do batch processing in Paint.NET. 

 

You can do it with ImageMagick though.

 

Steps:

Download & Install ImageMagick (My installation location is in C:\Program Files\ImageMagick-7.1.0-Q16-HDRI)

Make sure you've a backup copy of original images.

Save the code below into a .bat file and run it.

It'll ask you to enter some parameters.

 

@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" -fuzz %TOLERANCE%%% -fill transparent -draw "color %POINT% floodfill" "%OUTPUT_DIR%\%%~nxa"
@echo "Done!"
PAUSE

 

Parameters

Tolerance: Similar to magic wand tool's tolerance level.

ImageMagick Directory: ImageMagick installation directory

Point: Similar to magic wand tool's initialization point. ( Default value of 0,0 means top-left corner of the image )

Input Directory: Png files with backgrounds.

Output Directory: Png files without backgrounds will be saved into this directory.

Will this action crop them also or just delete the white around it? I need them to be cropped.

Link to comment
Share on other sites

1 minute ago, MarijaM said:

Will this action crop them also or just delete the white around it? I need them to be cropped.

 

-trim command should do the trick. Here is the updated 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" -fuzz %TOLERANCE%%% -fill transparent -draw "color %POINT% floodfill" -trim "%OUTPUT_DIR%\%%~nxa"
@echo "Done!"
PAUSE

 

  • Like 1
Link to comment
Share on other sites

I installed it after clicking the link above but it opens something IMG display, no commands or anything. I go to folder of installation and click on magick.exe nothing happens. 

Link to comment
Share on other sites

0 Create a desktop icon
0 Add application directory to your system path
0 Install FFmpeg
0 Associate supported file extensions with ImageMagick
0 Install legacy utilities (e.g. convert)
0 Install development headers and libraries for C and C++
0 Install PeriMagick for Strawberry Perl v5.20
0 Install ImageMagickObject OLE Control for VBscript, Visual Basic, and WSH

Link to comment
Share on other sites

I've to admit this is a bit technical since there is not much user interface. I'll try to explain as simple as I can.

 

After installing ImageMagick, you don't need to run anything in it. This program doesn't work that way. It has no user interface at all. Just note the location where its installed on your computer. In my case it was in C:\Program Files\ImageMagick-7.1.0-Q16-HDRI.

 

Then download this file anywhere on your computer and run it (Basically, this is the .bat file that contains the same code above): 

https://drive.google.com/file/d/1xKbX9CiTdBeUz53nECjyruruNSSiy8nM/view?usp=sharing

 

It'll ask you to enter 5 parameters. For the first 3 parameters I'm using the defaults. So I skipped those just by pressing enter key. If you installed ImageMagick into another directory than mine, you'll need to enter its path here. Then I enter input and output directories.

image.png.1cd4116e56c8002cf47d97f0f0724bdb.png

 

After that, It should start to generate images in output directory. Their backgrounds are removed and cropped.

cat1.png.69ef93602f26d30c3335de7507e4505d.png276670761_cat1-Copy.png.78d2ce284dc6da8e0f06d0cc165733fd.png1471189326_cat1-Copy(2).png.a4f0df1f31f5889e25e2f5ba082c5881.png829583855_cat1-Copy(2)-Copy.png.793af958ec8a5839cdc999bb132e878f.png

 

Edited by otuncelli
  • Like 1
Link to comment
Share on other sites

It worked on some images but on images where white around is not connected, it did nothing. I think I need to enlarge images by px so the white BG is connected all around. 

image.png.b97615391250717bd2101c0e8792f742.png

Link to comment
Share on other sites

Thank you so MUCH!!! This is amazing. Can the same program be used to resize all images to 3000×3000px or something like that? 

I am making clipping mask in shape of animal over set of mandala files (first test was on cats). I need the input layers for masking all to be same dimensions. Resolution doesn't matter (pixelization due to stretch or enlargement) because after all this is done I will perform Image trace and turn all in to vector. Since it is all black and white image tracing works perfectly. 

Link to comment
Share on other sites

8 hours ago, MarijaM said:

Thank you so MUCH!!! This is amazing. Can the same program be used to resize all images to 3000×3000px or something like that? 

I am making clipping mask in shape of animal over set of mandala files (first test was on cats). I need the input layers for masking all to be same dimensions. Resolution doesn't matter (pixelization due to stretch or enlargement) because after all this is done I will perform Image trace and turn all in to vector. Since it is all black and white image tracing works perfectly. 

 

I'm happy it worked for your case 😊 Yes, it can resize using many many different techniques. There is interpolative resizing with different modes, scaling and adaptive-resizing options. I don't know which will work best with image tracing. The example code below using "box" filter. Also converts the resulting image to 300ppi.

 

I've also updated the link.

 

@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

 

8 hours ago, MarijaM said:

And just for reference, what you achieved in couple of lines this could not in 3 days of lines. Photoshop

 

Thanks to ImageMagick developers. Photoshop's batch processing is so limited compared to it, and it's there mainly for marketing. I'm glad this forum is not as strict as Photoshop's. So we can talk about other tools and suggest alternative ways to achive something.

  • Hugs 1
Link to comment
Share on other sites

5 hours ago, otuncelli said:

Thanks to ImageMagick developers. Photoshop's batch processing is so limited compared to it, and it's there mainly for marketing. I'm glad this forum is not as strict as Photoshop's. So we can talk about other tools and suggest alternative ways to achive something.

 

Thank you. Everything worked great, now I can continue with my book. I also asked on illustrator forum  how to Image trace all these photos so the interior stays white but outside is transparent and it seems that is not possible in batch, only manually. So I placed images in Powerpoint and use sharpen tool and the end result is good enough for printing. 

 

Link to comment
Share on other sites

On 10/20/2021 at 5:59 AM, otuncelli said:

Thanks to ImageMagick developers. Photoshop's batch processing is so limited compared to it, and it's there mainly for marketing. I'm glad this forum is not as strict as Photoshop's. So we can talk about other tools and suggest alternative ways to achive something.

@otuncelli sorry to bother you, but you did open a whole new universe to me with ImageMagick and now I am looking for ways to process these images in to SVG and once again people are mentioning ImageMagick and something called Potrace Batch auto trace black and white images .

Do you know something about this program and can it be used to get SVG that look the same as PNG (white interior, transparent around). I tried all kinds of settings in Illustrator and it gives me either semi white, no white or extra block around. 

I installed Potrace but it opens as run window so i guess once again I need come code to un inside.


 

iMAGE TRACING1.png

Link to comment
Share on other sites

I think I have a better idea. Do what @otuncelli did, but with resize applied and lower tolerance. Then, use the alpha channel to cover up all the way to the line and process it from there.

 

I don't know how to use ImageMagick, and I tried to do this in G'MIC. I will refer you to @otuncelli and discuss.pixls.us (if you go the g'mic route) forum for help on this

G'MIC Filter Developer

Link to comment
Share on other sites

I'm thinking of implementing something similar for my SVG File Type plugin for Paint.NET. It's currently load-only plugin. Raster to vector is a bit complex 😅

 

There is a --fillcolor option for potrace that you can use. (The codes below uses this)

 

I also optimized the procedure. These codes are single step. They should do what you want from start to finish.

 

This works but it generates squiggly lines due to upscaling algorithm. These codes are for .bat file. I also updated the link.

 

@echo off
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 POTRACEDIR="PoTrace Directory (Default: Current directory)" || SET "POTRACEDIR=%cd%"
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%% ^
-fill white ^
-draw "color %POINT% floodfill" ^
-trim ^
-filter box ^
-resize 1000x1000 ^
-type grayscale ^
pnm:- | %POTRACEDIR%\potrace.exe -b svg --fillcolor #ffffff -r 32 -s -o "%OUTPUT_DIR%\%%~na.svg")
@echo "Done!"
@echo off
PAUSE

 

This will perform AI upscaling before vectorization process. (If source image is so small) But it requires a good GPU and this program 

https://github.com/xinntao/Real-ESRGAN/releases/tag/v0.2.2.4. Also it takes more time to convert.

 

@echo off
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 RESRGAN="Real-ESRGAN Directory (Default: Current directory): " || SET "RESRGAN=%cd%"
SET /p POTRACEDIR="PoTrace Directory (Default: Current directory): " || SET "POTRACEDIR=%cd%"
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 ^
-trim ^
-type grayscale ^
png:"%OUTPUT_DIR%\out.png"
"%RESRGAN%\realesrgan-ncnn-vulkan.exe" -s 4 -n realesrgan-x4plus-anime -i "%OUTPUT_DIR%\out.png" -o "%OUTPUT_DIR%\upscaled.png"
"%MAGICKDIR%\magick.exe" convert "%OUTPUT_DIR%\upscaled.png" ^
-resize 800x800 ^
-type grayscale ^
pnm:- | "%POTRACEDIR%\potrace.exe" -b svg --fillcolor #ffffff -r 25.6 -s -o "%OUTPUT_DIR%\%%~na.svg"
del "%OUTPUT_DIR%\out.png" && del "%OUTPUT_DIR%\upscaled.png")
@echo "Done!"
@echo off
PAUSE

 

And there is an alternative tool for potrace named "autotrace" which supports centerline tracing. It might give you better result than potrace so you may completely skip AI upscaling step if you use it with correct parameters. But I don't know much details.

 

Here are the results: svgfiles.zip

 

Edited by otuncelli
did some improvements
  • Like 1
Link to comment
Share on other sites

This is a friendly reminder that this is not an ImageMagick support forum.

 

5 hours ago, otuncelli said:

I'm thinking of implementing something similar for my SVG File Type plugin for Paint.NET.

 

^^ this, or perhaps take the discussion to PM?

  • Like 2
Link to comment
Share on other sites

  • 4 weeks later...
On 10/22/2021 at 1:03 AM, otuncelli said:

 

Agreed @Ego Eram Reputo

 

@MarijaM You can reach me with PM.

Thank you, and sorry for going of the main topic paint net. I truly hoped it can be done inside because I love paintnet.

Sorry for delay in reply, 5 in my daughters class were positive and my daughter was sick for weeks. 

Now she is better and I am back at work.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...