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 πŸ™‚

 

Automating AI Art Creation

Programming for Potatoes

Programming for Potatoes

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

Introduction

AI ArtWhile Deep Art Effects is a superb tool for manipulating and creating interesting images it is frustrating that so many mouse clicks are required to get things done. At the behest of the community Deep Art Effects noticed what we were asking and released a command line version. The command line version of Deep Art Effects has almost all the functionality of it’s GUI parent but allows for mouse-free operation at the cost of having to type your request at the command prompt.

As you can now process an image from the command line its obvious that if you have some basic programming skills then you can automate repetative tasks with relative ease. One problem with this solution, of course, is that not everyone has the necessary coding skills to do what they want. Here we present some basic scripts that will automate some of the more laborious tasks a DAE user is likely to desire but doesn’t know how to achieve without guidance.

To keep things as simple as possible we’re going to use PHP as a scripting language. PHP has a long track record and is one of the most popular methods of creating Websites. This website uses WordPress and WordPress is written in PHP. One thing that isn’t immediately obvious about PHP is that is is not just capable of controlling all types of Websites, it is equally capable of being used as a general purpose scripting language much like many other languages. PHP also has the advantage that it works on all platforms and is relatively small weighing in at a few tens of Mb and doesn’t need huge external libraries to provide much of its functionality – when you do need that extra bit of functionality most of the useful ones are provided in the original download as optional extras that are not enabled by default.

AI Art

For our first project we’re going to automate the creation of large quantities of AI generated Images condensing all that typing that would be required on the command line into one controlling script that runs DAE as often as we want outputting a new unique image every few seconds. This project has the additional advantage that there is no confuguration required (some future projects may require extra stuff but we’ll worry about that when we get to it – it’s actually also very easy if/when we need to do it)

So, to get all this running you need the following

  1. A registered*** version of Deep Art Effects GUI version (note – this link pays for me writing this tutorial if you click it then buy a DAE license – so click it!)
  2. A copy of Deep Art Effects CLI version (both are needed – I don’t get any cash for this link though…)
  3. A copy of PHP (choose one of the Zip versions, unless you have a definite reason not to I suggest the Thread Safe option – the second one)
  4. A copy of my makeart.php script (it’s also zipped as browsers can get wary of you deliberately downloading a PHP file)

*** You can try this all out without a registered version of Deep Art Effects but the results will look like this…

Unregistered Sample Image

The trial version watermarks all images…

Deep Art Effects

AI ArtUnless you already have Deep Art Effects installed you’ll need to go and Download the DAE GUI and install it. If you already have a LICENSE then you’re all set for the next step otherwise you’ll need to go and BUY ONE

Once you’ve installed and licensed Deep Art Effects you now need to go and get a copy of the Deep Art Effects CLI, the link for the download is under the main Windows download link where it says as “(BETA) Command Line Tool Download”. Just download this file and save it somewhere safe as we’ll need it later.

Finally also download makeart.zip which contains makeart.php as we’ll need that when we get to extract the CLI as well.

PHP

Next we need to obtain and extract PHP so go to the PHP download site and grab yourself a copy of the Zipped Windows 64 bit Thread Safe version (unless you know you need the Non-thread safe). Save this somewhere safe as well and we’re all ready to get everything set up.

To start with create a directory in C:\ called php (so you’ve got a C:\php directory) then use explorer to copy or move the PHP zip file you just downloaded into it. Now use your any ZIP extraction tool such as WinRar to extract the contents of the zip into c:\php like this…

Extract PHP

You should now have a load of files and a few directories in c:\php. If, for some reason, you’ve just got one directory then move the contents of that directory into c:\php.

We now just want to rename the file php.ini-development to php.ini

Rename to php.ini

Once you’ve renamed the file shown above so it’s called php.ini you’re all done – PHP is installed and will work perfectly.

Wasn’t that easy?

Lets make some Art

For the final step of this process we need to extract the Deep Art Effects CLI you downloaded earlier and the makeart script into the same directory. Now that PHP is installed somewhere easy to get at the exact location of the Deep Art Effects CLI and the controlling script makeart.php don’t matter that much so just extract them both into a directory of your choosing.

Make some directory, I’m choosing c:\work\dae, and copy the DAE CLI and makeart zips both there and extract them

Step 3

Now you need to open a command prompt (PowerShell window) in the directory that now has DeepArtEffectsCli.exe and makeart.php – an easy way to do this is by holding down the shift key then left-clicking on the directory you’ve put everything in

Step 4

Now you’ve got a command prompt just type in the following line and the magic will happen…

\php\php makeart.php art ai 1 100

And this will happen…

Step 5

 

My own run of the above took exactly six minutes. The estimation shown starts of very rough and gradually gets more and more accurate as time proceeds. The stats are present to give you an idea of how many images you can create in a given timescale and also to show you how long the current command you’re running has left to completion.

The above command is asking the script to create a directory called “art” then use the prefix “ai” and create images in it starting at number 1 and making 100 in total.

You could add another 100 to the same directory by issuing this command

\php\php makeart.php art ai 101 100

i.e. – do the same thing but start at 101 as we’ve already done 1 to 100

You could then try

\php\php makeart.php art ai 301 1800

But that will take a while πŸ™‚

Whatever you do you’ll end up with a large number of random AI generated images in your destination folder like this…

Step 5

Yours will, of course, be completely different but also faintly reminiscent of the above image.

The Source Code

If you open makeart.php in a standard text editor such as NotePad++ (not word or anything like that though – that’ll break things) you can see exactly what’s going on in the code. I’ve written a lot of comments to guide you through what’s going on. The code should be easy enough to follow even if you don’t know PHP (almost all programming languages make sense if you know at least one or two others)

The PHP Manual should explain most of the stuff in the code if you’re coming from some other coding tool.

The most important part is line 117 where I do this…

 // Construct the command line
 $cmd = ".\\" . $dae . " generate -name artwork -output " . $outpath . "/" . $fnprefix . "-" . sprintf("%06d", $i) . "." . $ftype;

This line creates the command line that you’d type if you weren’t doing this by code. The variable $dae just holds the name of the DAE CLI (as I tend to rename mine to dae.exe) so the above line makes $cmd equal to….

.\DeepArtEffectsCli.exe generate -name artwork -output art/ai-000001.jpg

… if $i is one, if it were 123 then the filename after -output would be art/ai-000123.jpg

By reading the CLI help output and modifying my code you can therefore make your own versions of this script which do other things like remove the background from an image – or lots of them all at once.

Somewhat predictably after that comment – Next time – removing backgrounds from a lot of images without clicking