Page 1 of 2
How to remove black background.
Posted: 2013-01-30T14:33:58-07:00
by pat.vachon
I am trying to remove the background from these receipts I've taken pics of for ProOnGo. I've tried the following with no success.
convert *.jpg -level 10%,100%,1 -trim Resaved.jpg
Before image.
http://www.lucascontracting.ca/2013-01-30%20144035.jpg
After image.
http://www.lucascontracting.ca/Resaved-0.jpg
Re: How to remove black background.
Posted: 2013-01-30T15:09:06-07:00
by snibgo
A large fuzz does the trick.
Code: Select all
convert "2013-01-30 144035.jpg" -fuzz 60% -trim t.jpg
Re: How to remove black background.
Posted: 2013-01-30T22:05:01-07:00
by pat.vachon
That helps but it seems I still have some refinements to make. I only just started using this yesterday so I'm really just tinkering like crazy. Any advice on this set of files.
http://lucascontracting.ca/2013-01-30.rar
I currently have the following command line.
convert *.jpg -level 30%,90%,1 -despeckle -fuzz 40% -trim Resaved.jpg
Increasing the fuzz passed 40% starts to overcrop.
Re: How to remove black background.
Posted: 2013-01-30T23:23:36-07:00
by snibgo
The exposure varies across the images, so you don't know what levels are best. Instead, I'd use "-contrast-stretch", eg:
Code: Select all
convert in.jpg -colorspace RGB -contrast-stretch 10%%,10%% out.png
And "+repage" might be useful.
Re: How to remove black background.
Posted: 2013-01-31T10:08:29-07:00
by snibgo
The camera exposure varies, according to the size of the receipt. It's better to use manual control, where the camera has it, so backgrounds and foregrounds are consistent. Of course, a scanner would be even better.
The "black" background has light smudges, needing some heavy manipulation to remove. So I've adopted a two-tiered approach: create a mask, exactly black where the background is roughly black, and transparent elsewhere. Composite the mask over the original, then trim etc.
The (Windows 7) script, a single command, perfectly trims all the files in the rar, and I think does a decent job of ensuring the receipts are readable..
Code: Select all
"%IMG%convert" ^
%SRC% ^
( +clone ^
-despeckle ^
-blur 0x5 ^
-set colorspace RGB ^
-contrast-stretch 30%%,0%% ^
-fuzz 40%% -fill black -floodfill 0x0 Black ^
-fuzz 0 -fill None +transparent Black ^
) ^
-composite ^
-trim ^
+repage ^
-set colorspace RGB ^
-contrast-stretch 1%%,1%% ^
out\%2
Re: How to remove black background.
Posted: 2013-01-31T10:17:56-07:00
by pat.vachon
WOW!!!
K.
1. How do you make a script?
2. Not sure what to set on the camera. I'm using Cam4You Remote. Here is screenshot and help would be much appreciated.
http://screencast.com/t/4PysggEC
Re: How to remove black background.
Posted: 2013-01-31T10:58:07-07:00
by snibgo
Oh. Ah. Um.
A script is a text file that contains commands. How you create one depends on what type of computer you are using -- in particular the operating system. This isn't a scripting forum, and I wouldn't attempt to explain it. There many guides on the net.
Likewise, this isn't a camera forum. Read the manual. If it has a manual mode, that will give you more consistent results.
Re: How to remove black background.
Posted: 2013-01-31T11:53:43-07:00
by pat.vachon
What is "out\%2" at the end? Also, why the double percent signs?
I have not been able to do this in Win7 as one command line entry. I have tried to do it in two steps and get something close but not at all sharp.
http://screencast.com/t/40RTKfBjN1
convert "2013-01-30 144035.jpg" -despeckle -blur 0x5 -set colorspace RGB -contrast-stretch 30%%,0%% -fuzz 40%% -fill rgb(0,0,0) -floodfill 0x0 rgb(0,0,0) -fuzz 0 -fill None +transparent rgb(0,0,0) Resaved.jpg
convert "2013-01-30 144035.jpg" -composite Resaved-0.jpg -trim +repage -set colorspace RGB -contrast-stretch 1%%,1%% Resaved2.jpg
Re: How to remove black background.
Posted: 2013-01-31T12:15:07-07:00
by snibgo
Instead of out\%2, just put whatever directory and filename you want. If you are putting this in a Windows batch file, you need to double up the % signs. If you are typing at the command line, don't double them.
I showed it as one command. If you want it as two commands, here they are:
Code: Select all
"%IMG%convert" ^
%SRC% ^
-despeckle ^
-blur 0x5 ^
-set colorspace RGB ^
-contrast-stretch 30%%,0%% ^
-fuzz 40%% -fill black -floodfill 0x0 Black ^
-fuzz 0 -fill None +transparent Black ^
mask.png
"%IMG%convert" ^
%SRC% ^
mask.png ^
-composite ^
-trim ^
+repage ^
-set colorspace RGB ^
-contrast-stretch 1%%,1%% ^
out\%2
Your first command is okay, except that the mask file should be .PNG, not .JPG. If you use .JPG, the black may not be a nice smooth black, which would make the "-trim" fail. Your second command is somewhat wrong.
Re: How to remove black background.
Posted: 2013-01-31T12:35:22-07:00
by pat.vachon
YAHOO!!! Got it to work in one step.
http://screencast.com/t/PH7JQj622Ce
convert "2013-01-30 144035.jpg" ( +clone -despeckle -blur 0x5 -set colorspace RGB -contrast-stretch 30%,0% -fuzz 40% -fill rgb(0,0,0) -floodfill 0x0 rgb(0,0,0) -fuzz 0 -fill None +transparent rgb(0,0,0) ) -composite -trim +repage -set colorspace RGB -contrast-stretch 1%,1% Resaved2.jpg
How can I make this work all files in one directory and create a directory of the cropped images within it?
Re: How to remove black background.
Posted: 2013-01-31T13:21:21-07:00
by pat.vachon
Any ideas on how to make this work on all files in one directory and create a directory of the cropped images within it?
Re: How to remove black background.
Posted: 2013-01-31T13:39:17-07:00
by pat.vachon
Seems like I found the issue.
http://www.imagemagick.org/Usage/basics/#option_list
So is there a work for this?
Re: How to remove black background.
Posted: 2013-02-01T02:03:27-07:00
by snibgo
Any ideas on how to make this work on all files in one directory and create a directory of the cropped images within it?
http://www.imagemagick.org/Usage/basics/#mogrify
Re: How to remove black background.
Posted: 2013-02-01T07:58:39-07:00
by pat.vachon
I've tried mogrify. It doesn't like -composite as it takes the list of images to do what it does and spits our one image for the entire lot.
This works though.
FOR %%G IN (*.jpg) DO convert %%G ( +clone -despeckle -blur 0x5 -set colorspace RGB -contrast-stretch 30%%,0%% -fuzz 40%% -fill rgb(0,0,0) -floodfill 0x0 rgb(0,0,0) -fuzz 0 -fill None +transparent rgb(0,0,0) ) -composite -trim +repage -set colorspace RGB -contrast-stretch 1%%,1%% -rotate -90 -set filename:cfilename %%G cropped_%%[filename:cfilename]
Re: How to remove black background.
Posted: 2013-02-04T11:50:03-07:00
by pat.vachon
Well. I thought I was almost ready to put this into production but not quite. The composite is putting into play the blur from the first of the two images. This reduces the image quality significantly on some of the images.
FOR %%G IN (Orig_*.jpg) DO convert %%G ( +clone -despeckle -blur 0x1 -set colorspace RGB -contrast-stretch 30%%,0%% -fuzz 40%% -fill rgb(0,0,0) -floodfill 0x0 rgb(0,0,0) -fuzz 0 -fill None +transparent rgb(0,0,0) ) -composite -trim +repage -set colorspace RGB -contrast-stretch 1%%,1%% -set filename:cfilename %%G cropped2_%%[filename:cfilename]
How can I create a region to crop instead of cropping after -composite? Sort of like asking -trim for the coordinates, width and height without actually cropping.