[HELP] Autocrop batch on photo ID

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".

[HELP] Autocrop batch on photo ID

Postby newbie1 » 2012-05-29T07:39:27+00:00

Hi, i am a newbie on using imagemagick and i have a problem.

Lets say that i have many photo ID which i have to automatically crop out all the photo.

I had a camera standing by to take picture of the ID and then it will be saved to a folder. eg: C:\>Desktop\Input
The images are named : 1.jpg, 2.jpg, 3.jpg...
Then i had to autocrop all the ID and take out the photo of the person and save in to another folder. eg: C:\>Desktop\output

So how do i do it ??
newbie1
 
Posts: 31
Joined: 2012-05-29T07:31:08+00:00

Re: [HELP] Autocrop batch on photo ID

Postby Bonzo » 2012-05-29T08:48:06+00:00

Try this:

Code: Select all
::Turn off displaying the code on the screen
@echo off
:: Read all the jpg images from the directory, crop them and save to a different directory
for %%f in (%1\*.jpg) do ( convert "%%f" -gravity center -crop 200x200+0+0 "%2\%%~nf.jpg" )

This batch file was named resize.bat and saved in c: and called using "resize path\to\original\ path\to\save" without the c: in the path
Bonzo
 
Posts: 2233
Joined: 2006-05-20T08:08:19+00:00
Location: Cambridge, England

Re: [HELP] Autocrop batch on photo ID

Postby anthony » 2012-05-30T00:47:42+00:00

We will need a example of the input and what you want.

It really depends on just what 'surrounds' the area you want to crop to.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/
User avatar
anthony
 
Posts: 8732
Joined: 2004-05-31T19:27:03+00:00
Location: Brisbane, Australia

Re: [HELP] Autocrop batch on photo ID

Postby newbie1 » 2012-05-31T09:27:49+00:00

anthony wrote:We will need a example of the input and what you want.

It really depends on just what 'surrounds' the area you want to crop to.


This is a sample of the picture that i wanted to crop:
Image

I like to be able to crop out the person face which is on the left and also crop out the ID card which is on the right.

Lets say the photo came from C:\>Desktop\Input and i want the crop face and ID card to be saved in C:\>Desktop\output.

I also like to saved the name of the cropped photo as 1_1,1_2
As there will be alot of these photos, the original copy of the photo will be named 1.jpg, 2.jpg, 3.jpg...etc
And the cropped photo will be named as following 2_1, 2_2 and 3_1, 3_2

Thanks for Helping :)
newbie1
 
Posts: 31
Joined: 2012-05-29T07:31:08+00:00

Re: [HELP] Autocrop batch on photo ID

Postby fmw42 » 2012-05-31T10:00:27+00:00

Is this the actual image or just a diagram of what you have?

If the real image has picture and ID that are different color from the background, then if you were on Linux, Mac or Windows with Cygwin, you could try my bash unix script, multicrop, at the link below.

If your face and ID are on a background that is uniform and a different color, then you could make it binary, so that you have say black on white. Then use -scale to average the image down to one row. Then look for the totally white spaces to get the X crop coordinates and crop into two parts using -crop.

see
http://www.imagemagick.org/script/comma ... .php#scale
http://www.imagemagick.org/Usage/crop/#crop
User avatar
fmw42
 
Posts: 15028
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: [HELP] Autocrop batch on photo ID

Postby newbie1 » 2012-06-01T12:12:34+00:00

This is the actual image that i will be using:
Image
I'm using Windows.
I would want the output to be :
Image and Image
newbie1
 
Posts: 31
Joined: 2012-05-29T07:31:08+00:00

Re: [HELP] Autocrop batch on photo ID

Postby fmw42 » 2012-06-01T12:54:03+00:00

My multicrop script would handle that, but it requires Linux, Mac or Windows with Cygwin.

But you can do as I suggested above. Use -scale to average the image down to 1 row. Convert to txt format so you can view all the pixel values. Look for when the values are pure white and when they become other than white. You can even make a grayscale image first before averaging down to one row. Once you see where the transitions are, you can crop the image appropriately. Just pick any where in the middle of the white regions for the x coordinate and crop the image into two parts. For each part, use -fuzz XX% -trim to then get the two parts trimmed of all excess white.

see
http://www.imagemagick.org/script/comma ... u282#scale
http://www.imagemagick.org/Usage/crop/#crop
http://www.imagemagick.org/Usage/crop/#trim
http://www.imagemagick.org/Usage/files/#txt
User avatar
fmw42
 
Posts: 15028
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: [HELP] Autocrop batch on photo ID

Postby newbie1 » 2012-06-03T19:57:40+00:00

How do i use your multicrop script with cgywin and imagemagick?

and for your suggestions to -scale to average the image down to 1 row, how do i do that ?
i tried using -scale 1000% example.jpg and this only make the whole image bigger

sorry for all these question as im really new into this kind of programming
newbie1
 
Posts: 31
Joined: 2012-05-29T07:31:08+00:00

Re: [HELP] Autocrop batch on photo ID

Postby fmw42 » 2012-06-03T20:34:34+00:00

newbie1 wrote:How do i use your multicrop script with cgywin and imagemagick?

and for your suggestions to -scale to average the image down to 1 row, how do i do that ?
i tried using -scale 1000% example.jpg and this only make the whole image bigger

sorry for all these question as im really new into this kind of programming



I am not a windows user, so can only go by the fact that other users have installed cygwin and used my scripts.

See
http://www.imagemagick.org/Usage/windows/

With regard to -scale, you want to scale to 1 row which is -scale x1!. The width is left unchanged and height is made exactly 1, so you get one row. see http://www.imagemagick.org/script/comma ... p#geometry

Try this on your image (-fuzz is needed as you have jpg input):

NOTE: the following is unix. You may or may not have grep on windows (which just prints those lines containing white)??? But in windows you need to escape the % as %% and probably put double quotes around "x1!". Likewise the other commands further down. See the link above for further differences.

convert et61lk.jpg -fuzz 10% -trim -write et61lk_trim.png -scale x1! txt: | grep "white"
210,0: (255,255,255) #FFFFFF white
211,0: (255,255,255) #FFFFFF white
212,0: (255,255,255) #FFFFFF white
213,0: (255,255,255) #FFFFFF white
214,0: (255,255,255) #FFFFFF white
215,0: (255,255,255) #FFFFFF white
216,0: (255,255,255) #FFFFFF white
217,0: (255,255,255) #FFFFFF white
218,0: (255,255,255) #FFFFFF white
219,0: (255,255,255) #FFFFFF white
220,0: (255,255,255) #FFFFFF white
221,0: (255,255,255) #FFFFFF white
222,0: (255,255,255) #FFFFFF white
223,0: (255,255,255) #FFFFFF white
224,0: (255,255,255) #FFFFFF white
227,0: (255,255,255) #FFFFFF white

# trimmed image (et61lk_trim.png)
Image

# get trimmed size
convert et61lk_trim.png -format "%wx%h" info:
400x329

# crop and trim
convert et61lk_trim.png -crop 209x329+0+0 -fuzz 10% -trim +repage et61lk_left.png
convert et61lk_trim.png -crop $((400-227))x336+227+0 -fuzz 10% -trim +repage et61lk_right.png

Image

Image

Actually you don't have to be very specific about what point in the white range to use. Lets just pick any one value, say 215.

convert et61lk_trim.png -crop 215x329+0+0 -fuzz 10% -trim +repage et61lk_left.png
convert et61lk_trim.png -crop $((400-215))x336+215+0 -fuzz 10% -trim +repage et61lk_right.png

And because of the trim, you get the same result
User avatar
fmw42
 
Posts: 15028
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: [HELP] Autocrop batch on photo ID

Postby newbie1 » 2012-06-04T07:38:36+00:00

Thanks for all the information!

But there is a slight problem and im not sure is it because im using windows.

convert et61lk.jpg -fuzz 10% -trim -write et61lk_trim.png -scale x1! txt: | grep "white"
since there no grep on windows, i written it like this : convert et61lk.jpg -fuzz 10% -trim -write et61lk_trim.png -scale x1!
however there is a problem with convert so i change it to mogrify and it became :
mogrify et61lk.jpg -fuzz 10% -trim -write et61lk_trim.png -scale x1!
and it works sometime only. so is there a solution to this?

for this 2 command, it works great ! :D
convert et61lk_trim.png -crop 209x329+0+0 -fuzz 10% -trim +repage et61lk_left.png
convert et61lk_trim.png -crop $((400-227))x336+227+0 -fuzz 10% -trim +repage et61lk_right.png

and to batch cropped all the photo is the command written like this? :
mogrify *.jpg -fuzz 10% -trim -write *_trim.png -scale x1!

Thanks again! :)
newbie1
 
Posts: 31
Joined: 2012-05-29T07:31:08+00:00

Re: [HELP] Autocrop batch on photo ID

Postby fmw42 » 2012-06-04T10:25:35+00:00

Your problem is that on Windows, you did not escape the % to %% as I mentioned. See http://www.imagemagick.org/Usage/windows/

convert et61lk.jpg -fuzz 10% -trim -write et61lk_trim.png -scale x1! txt:

should be

convert et61lk.jpg -fuzz 10%% -trim -write et61lk_trim.png -scale "x1!" txt:

I am not positive about the quoting, but you can try with or without.

The above should write a list of every pixel to the terminal window. Then you have to filter out all the white pixels. That is why I used a pipe to grep "white". So you need to find or write something to filter the text information. So you can find the white region.
User avatar
fmw42
 
Posts: 15028
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: [HELP] Autocrop batch on photo ID

Postby fmw42 » 2012-06-04T14:58:48+00:00

Here is an alternate way to get the white pixels without grep. See http://www.imagemagick.org/Usage/transform/#fx and scroll down to the debugging section.

convert et61lk.jpg -white-threshold 99% -trim -write et61lk_trim.png -scale x1! -fx "r==1&&g==1&&b==1?debug(u):1 u" null:

et61lk_trim.png[216,0].red: u=1
et61lk_trim.png[216,0].green: u=1
et61lk_trim.png[216,0].blue: u=1
et61lk_trim.png[217,0].red: u=1
et61lk_trim.png[217,0].green: u=1
et61lk_trim.png[217,0].blue: u=1
et61lk_trim.png[218,0].red: u=1
et61lk_trim.png[218,0].green: u=1
et61lk_trim.png[218,0].blue: u=1
et61lk_trim.png[219,0].red: u=1
et61lk_trim.png[219,0].green: u=1
et61lk_trim.png[219,0].blue: u=1
et61lk_trim.png[220,0].red: u=1
et61lk_trim.png[220,0].green: u=1
et61lk_trim.png[220,0].blue: u=1
et61lk_trim.png[221,0].red: u=1
et61lk_trim.png[221,0].green: u=1
et61lk_trim.png[221,0].blue: u=1
et61lk_trim.png[222,0].red: u=1
et61lk_trim.png[222,0].green: u=1
et61lk_trim.png[222,0].blue: u=1
et61lk_trim.png[223,0].red: u=1
et61lk_trim.png[223,0].green: u=1
et61lk_trim.png[223,0].blue: u=1

For windows, this is probably

convert et61lk.jpg -white-threshold 99%% -trim -write et61lk_trim.png -scale "x1!" -fx "r==1&&g==1&&b==1?debug(u):1 u" null:

_________

P.S.
Here is an alternate way to get just the first white pixel, since any white pixel in the gap will do.

convert et61lk.jpg -fuzz 10% -trim -write et61lk_trim.png -scale x1! et61lk_trim_scale.png
compare -metric rmse -subimage-search et61lk_trim_scale.png \( -size 1x1 xc:white \) null:
0 (0) @ 212,0

The first two numbers 0 (0) mean that there is no difference between the best match and white. The numbers after the @ are the x,y coords.

The cropping is done the same, using these coordinates as in the earlier message.

see
http://www.imagemagick.org/script/compare.php
http://www.imagemagick.org/Usage/compare/


Anthony and Magick:

A useful enhancement to IM, though not urgent, might be:

To list the first and last min coordinates and the first and last max coordinates for each channel and over all in the verbose info and/or have new string formats for those.
User avatar
fmw42
 
Posts: 15028
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: [HELP] Autocrop batch on photo ID

Postby newbie1 » 2012-06-19T07:16:04+00:00

HI,
when i put this command in command prompt this error comes out.

convert 1.jpg -fuzz 10%% -trim -write 1_trim.png -scale x1!

convert.exe: option requires an argument `-scale' @ error/convert.c/ConvertImageCommand/2432.


However, there is still a correct output and i wonder is there a solution to remove this so called error?
newbie1
 
Posts: 31
Joined: 2012-05-29T07:31:08+00:00

Re: [HELP] Autocrop batch on photo ID

Postby fmw42 » 2012-06-19T10:18:04+00:00

newbie1 wrote:HI,
when i put this command in command prompt this error comes out.

convert 1.jpg -fuzz 10%% -trim -write 1_trim.png -scale x1!

convert.exe: option requires an argument `-scale' @ error/convert.c/ConvertImageCommand/2432.


However, there is still a correct output and i wonder is there a solution to remove this so called error?



You have left off the output and not put quotes around the x1! for windows otherwise, the ! gets it confused.

convert 1.jpg -fuzz 10%% -trim -write 1_trim.png -scale "x1!" txt:
User avatar
fmw42
 
Posts: 15028
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: [HELP] Autocrop batch on photo ID

Postby newbie1 » 2012-06-20T00:07:55+00:00

thanks, that solved the problem.

I had also make a window Dos batch file for it to automatically auto crop all the photo inside the folder:

Code: Select all
@echo off

cd C:\Users\user\Desktop\New\New folder

convert *.jpg -fuzz 10%% -trim -write %%02d_trim.png -scale "x1!" txt:

convert *_trim.png -crop 209x329+0+0 -fuzz 10%% -trim +repage %%02d_left.png

convert *_trim.png -crop 173x336+227+0 -fuzz 10%% -trim +repage %%02d_right.png

@pause


it works but i wanted to make it keep looping in case there additional photo being added to the folder or make it work when it senses there is additional photo being added to the folder.

I had read the examples but i still don't quite understand the concept of looping.
http://www.imagemagick.org/Usage/windows/#dos
newbie1
 
Posts: 31
Joined: 2012-05-29T07:31:08+00:00

Next

Return to Users

Who is online

Users browsing this forum: Bing [Bot], Google Adsense [Bot] and 18 guests