Bulk Background Removal

Programming for Potatoes

 

Programming for Potatoes

Who needs a brain the size of a planet when you’ve got a Potato-Brain ?

Introduction

It is often useful to have the ability to remove the background from an image. The usual impetus is the desire to change the background in some creative manner or another. Deep Art Effects allows background removal using the standard GUI but what if you want to remove the backgrounds of many, possibly thousands, of images? Yes, you can do it with the GUI, but it’s a click-athon that will wear both your finger and your patience. Applying a little DAE CLI magic to the process via a script makes all this a painless process (although it may take quite some time if you are doing this for a seriously large collection of images).

Mona Transparent

 

A Small Addition

You need to make one small alteration to the PHP you installed last time (if you didn’t get that far yet then go do it now along with the DAE CLI setup and come back here to continue).

Owing to the way this script is written we need to add the semi-intelligent ability to check if a file is an image or not. To do this browse to your PHP directory and open php.ini in a normal text editor like NotePad++ (also mentioned in the last article).

Now search for (Ctrl F usually) the text “fileinfo” and you’ll find a line that says …

;extension=fileinfo

Delete the semi-colon from the start of that line so it now just says “extension=fileinfo”. Save your altered file and you’re all ready to go.

The Script

Now you need to download and extract the remove_background.php script. As with the make_art.php script last time this new script wants to be in the same directory as the Deep Art Effects CLI to make it work.

All you need now is some images to remove the backgrounds of. If you’ve created some AI art with makeart.php and they’re in art then we can convert those to try it out.

\php\php remove_background.php art art_no_background

This will remove the background from all the images in art and save them in  a new folder called art_no_background.

If you used AI Images and look at the results you’ll notice that you get some human-looking images and a lot of blanks. This is owing to the way that Deep Art Effects works. At present it tries to find humans in an image, so background removal is actually more correctly non-human removal. It would be possible to identify other types of feature in an image other than humans but at the moment all we get is humans (but then again it’s probably what you wanted to do in the first place)

Removed

Bulk Removing Background

The remove_background script was developed as a generalised solution to processing DAE CLI images and as such it will work not only on the folder you tell it to but also any sub-folders of your chosen source folder. The output images are saved in an identical forlder structure within the outpot folder you requested on the command line. As transparency (which is necessary for background removal) isn’t possible with the JPEG file format all images in the output folder are saved as PNGs (with transparency) regardless of the source image file type.

It should be noted that as the script duplicates files and saves them as PNGs it will require much more space for the duplicates than the originals.

Another feature of the remove_background script is that it is re-startable. This was done in such a way that if you want to process thousands of files you can stop an existing run (with Ctrl C) and the script will simply stop. If you then want to continue just enter the same command and it will skip all the files it’s already processed and continue with the next file that would have been processed if you hadn’t stopped it running in the first place.

As the script needs to find out how many files it needs to process and have the ability to resume lenghty runs if you run the script specifying a source folder that contains really large numbers of files there will be a bit of a delay while it looks at all the files (it’s only noticable over about 5k files in tests)

Where Next?

As stated above the remove_background script is capable of doing a lot more than just removing backgrounds. You will find a function called perform_command (line 86) that can be adapted to do other things such as, for example, apply a style to a file. Because remove_background works on folders this would allow you to style entire folders for example.

Humans

As mentioned above the remove background option of Deep Art Effects looks for humans. This results in a lot of blank images so ideally it’d be a good idea to be able to ascertain how much background was removed – this way you’d be able to create buckets of images with various amounts of human in them. This is, of course, the topic of the next article 🙂

 

Leave a comment