Gimptalk - Premier Gimp Community: Divide OR Crop Multiple Images from Single Scan - Gimptalk - Premier Gimp Community

Jump to content

  • 4 Pages +
  • 1
  • 2
  • 3
  • Last »
  • You cannot start a new topic
  • You cannot reply to this topic

Divide OR Crop Multiple Images from Single Scan

#1 User is offline   jfwiedmayer 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 20-December 08

Posted 20 December 2008 - 04:49 PM

Quote

In Adobe Photoshop Elements there is a function called "Divide scanned images" which i assume uses the whitespace between multiple images from a flatbed scan and separates each photo into a new image. does anyone know of a script that will do something similar in GIMP? I am getting my father a scanner for Xmas and wanted to find a relatively straightforward solution like Elements but the 130 for the software is out of my budget.

Here is the Elements process...
Posted Image


affordable and convenient Computer Repair Done Right - Flat Rate Pricing - Remote Assistance, Pickup/Delivery, On Site

http://www.phillyhelpdesk.com

Posted ImagePosted Image
0

#2 User is offline   RobA 

  • Member
  • PipPip
  • Group: Members
  • Posts: 721
  • Joined: 05-November 07

Posted 22 December 2008 - 08:26 PM

Try this script I threw together. However, it doesn't rotate the images if they are misaligned...

   See below for the improved script...


-Rob A>
Posted Image
Fantasy Cartography and Mapping by RobA
0

#3 User is offline   PhotoComix 

  • GT Senior Moderator
  • Group: Senior Moderators
  • Posts: 11,288
  • Joined: 13-June 05

Posted 23 December 2008 - 11:24 AM

cool :)
0

#4 User is offline   RobA 

  • Member
  • PipPip
  • Group: Members
  • Posts: 721
  • Joined: 05-November 07

Posted 23 December 2008 - 04:48 PM

OK -

Bit of an update. I tried this on a real scanner and the noise created a whole bunch of little images. I've now added a size threshold slider, and the script will call the deskew plugin http://www.cubewano....-deskew-plugin/ if it is installed.

See below for the latest version

Here is a scan I made with three photos on the platen:
Posted Image

and the three resulting images it extracted and straightened:
Posted Image
Posted Image
Posted Image

As a final note, the script used the colour at 0,0 as the background reference, so if your scanner has a shadow there it might be off.... I don't know a way to have a user specify a pick point in a script-fu :(

Feedback always appreciated!

-Rob A>
Posted Image
Fantasy Cartography and Mapping by RobA
0

#5 User is offline   mishra 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 01-January 09

Posted 01 January 2009 - 01:00 AM

Thanks, RobA.

I have successfully installed the plugin and the script.

The script seems to run, but is unable to extract multiple images. [In one instance, it seemed to enter an infinite loop.]

Do I need to set the initial sliders to something other than 10 and 100 ? Appreciate if you can provide an intro to the two parameters.

This will truly save me a ton of time if I can somehow get it to work in my envt.
0

#6 User is offline   RobA 

  • Member
  • PipPip
  • Group: Members
  • Posts: 721
  • Joined: 05-November 07

Posted 01 January 2009 - 08:51 AM

The 10 is the tolerance. You can test it by using the magic wand tool set to 10 and clicking at 0,0 to see what gets selected (Hopefully it is the whole background) but it will depend on the noise of the bg from your scanner.

If there are lots of speckles/noise it will iterate through all of them, so setting this higher is best.

The other parameter is the minimum size for an image. and smaller items will be ignored. Set it as large as posible, just smaller than your largest image.

I'm working on a better version, so hopefully this will help for now...

-Rob A>
Posted Image
Fantasy Cartography and Mapping by RobA
0

#7 User is offline   RobA 

  • Member
  • PipPip
  • Group: Members
  • Posts: 721
  • Joined: 05-November 07

Posted 13 January 2009 - 05:54 PM

One more update...

This now uses rectangular selections rather than the selection from the path (I could make this a toggle, I guess)...

Also, there is a new "Abort Limit" parameter to keep the plugin from going into a nasty loop if the parameters don't make a lot of sense.

See below for the latest version
Posted Image
Fantasy Cartography and Mapping by RobA
0

#8 User is offline   mishra 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 01-January 09

Posted 15 January 2009 - 01:27 AM

Rob,

I copied and pasted this in the script file, and re-started GIMP. I get the following error.

ERROR: NOT ENOUGH ARGUMENTS

Possibly, coz at the end of the script you have the Abort Limit parameter, but no input on the dialog box.

Rajnish
0

#9 User is offline   RobA 

  • Member
  • PipPip
  • Group: Members
  • Posts: 721
  • Joined: 05-November 07

Posted 15 January 2009 - 02:44 AM

mishra said:

Possibly, coz at the end of the script you have the Abort Limit parameter, but no input on the dialog box.

Rajnish


It is the last line!

Did you copy all of it?

Did you insert linefeeds because of the text editor you used?

-Rob A>
Posted Image
Fantasy Cartography and Mapping by RobA
0

#10 User is offline   mishra 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 01-January 09

Posted 15 January 2009 - 02:57 AM

My bad.

I simply re-copied and pasted and it is now working correctly.

Beginning to test it out. Will update you soon.
0

#11 User is offline   mishra 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 01-January 09

Posted 26 January 2009 - 08:23 PM

Rob,

Sorry, it took me longer than expected. But, I wanted to experiment exhaustively before commenting. I have now tested the script with (1) the photos on your post and (2) my own photos.

The photos from your post work perfectly. It seems like this could be the solution and we would want all the photos to work like that, including de-skew.

In case of my own photos, it is unable to extract two photos from a single collage. I have tried various settings on size [between 0 and 20] and selection [10 to 200] thresholds without success.

I started troubleshooting and noticed that with the fuzzy tool selection tool set to background [in my case, it is white or something close to it], and selection threshold set to zero, it is able to find the two picture on my scanned image. Albeit with some jagged edges, but I am assuming your script takes care of that.

Could you help me in understanding how best to accomplish it ?

Thanks,

Rajnish
0

#12 User is offline   RobA 

  • Member
  • PipPip
  • Group: Members
  • Posts: 721
  • Joined: 05-November 07

Posted 26 January 2009 - 10:21 PM

Could you post a full page scan sample somewhere? You can pm me with the details if you don't wish to post publicly.

There are a few factors that could mess it up, primarilly a shadow arounf the scanner frame, so that is what I would need to see and correct for.

-Rob A>
Posted Image
Fantasy Cartography and Mapping by RobA
0

#13 User is offline   RobA 

  • Member
  • PipPip
  • Group: Members
  • Posts: 721
  • Joined: 05-November 07

Posted 29 January 2009 - 01:30 PM

Here is an updated version that I have had an oportunity to test with a few moew scanned images (thanks Rajnish)

This simplifies the selection at the beginning to speed up the process and denoise the image more.

It also stops incorrect returns of the entire image due to bad paths.

Lastly, it provides X and Y offsets to allow compensate for scanner beds that introduce a shadow along the edges of a full page scan.

; DivideScannedImages.scm
; by Rob Antonishen
; http://ffaat.pointclark.net

; Version 1.3 (20090128)

; Description
;
; Locates each separate element and creates a new image from each.
; will call the deskew plugin http://www.cubewano.org/gimp-deskew-plugin/
; if it is installed on each image
;
; Changes:
; v1.1 - Added a size threshold slider, and it will call the deskew plugin if installed
; v1.2 - takes a rectangular selection bounding the path rather than a selection from the path itself, added an abort threshold incase the parameters are wonky
; v1.3 - simplifies the selection via feather/sharpen first to speed up the image analysis.
;        - fixed exporting the whole image as one.
;        - added sliders to pick a background offset.  This is useful if your scanner has a "shadow" around the edge of full scans,
;
; License:
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version. 
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; The GNU Public License is available at
; http://www.gnu.org/copyleft/gpl.html

(define (script_fu_DivideScannedImages img inLayer inThreshold inSize inLimit inX inY)

  (let*
    (
	  (width (car (gimp-image-width img)))
	  (height (car (gimp-image-height img)))
	  (newpath 0)
	  (strokes 0)
	  (tempVector 0)
	  (tempImage 0)
	  (tempLayer 0)
	  (bounds 0)
	  (count 0)
	  (numextracted 0)
   )
    ;  it begins here
    (gimp-context-push)
    (gimp-image-undo-disable img)
    
	;logging
	;(gimp-message-set-handler ERROR-CONSOLE)
    ;(gimp-message-set-handler CONSOLE)
    ;(gimp-message-set-handler MESSAGE-BOX)
	;or start GIMP wwith "gimp --console-messages" to spawn a console box
	;then use this:
	;(gimp-message "foobar") 

	;testing for functions defined
	;(if (defined? 'plug-in-shift) (gimp-message "It Exists") (gimp-message "Doesnt Exist"))

	;use 0,0 as the background colour
	(gimp-context-set-background (car (gimp-image-pick-color img inLayer inX inY FALSE FALSE 0)))
	
	; convert in inverted copy of the background selection to a path
	(gimp-fuzzy-select inLayer inX inY inThreshold CHANNEL-OP-REPLACE TRUE FALSE 0 TRUE)
    (gimp-selection-feather img (/ (min width height) 100))
	(gimp-selection-sharpen img)
	(gimp-selection-invert img)
	(plug-in-sel2path RUN-NONINTERACTIVE img inLayer)
	
	;break up the vectors
    (set! newpath (vector-ref (cadr (gimp-image-get-vectors img)) 0)) 
	
	(set! strokes (gimp-vectors-get-strokes newpath))
    (while (and (< count (car strokes)) (< numextracted inLimit))
	  (set! tempVector (gimp-vectors-new img "Temp"))
	  (gimp-image-add-vectors img (car tempVector) -1)
	  (gimp-vectors-stroke-new-from-points (car tempVector)
	    (list-ref (gimp-vectors-stroke-get-points newpath (vector-ref (cadr strokes) count)) 0)
	    (list-ref (gimp-vectors-stroke-get-points newpath (vector-ref (cadr strokes) count)) 1)
	    (list-ref (gimp-vectors-stroke-get-points newpath (vector-ref (cadr strokes) count)) 2)
	    (list-ref (gimp-vectors-stroke-get-points newpath (vector-ref (cadr strokes) count)) 3)
	  )
	  (gimp-vectors-to-selection (car tempVector) CHANNEL-OP-REPLACE TRUE FALSE 0 0)
	  
	  ;check for minimum size
	  (set! bounds (gimp-selection-bounds img))
	  (if (and (> (- (list-ref bounds 3) (list-ref bounds 1)) inSize) (> (- (list-ref bounds 4) (list-ref bounds 2)) inSize) ;min size slider
	           (< (- (list-ref bounds 3) (list-ref bounds 1)) width) (< (- (list-ref bounds 4) (list-ref bounds 2)) height)) ;max size image
	    (begin
		  (gimp-rect-select img (list-ref bounds 1) (list-ref bounds 2)
								(- (list-ref bounds 3) (list-ref bounds 1)) (- (list-ref bounds 4) (list-ref bounds 2))
								CHANNEL-OP-REPLACE FALSE 0 )
	      (gimp-edit-copy inLayer)
	      (set! tempImage (car (gimp-edit-paste-as-new)))
          (gimp-image-undo-disable tempImage)
	      (gimp-display-new tempImage)
	  
	      ;run deskew if it is installed
	      (if (defined? 'gimp-deskew-plugin) 
	        (begin
		      (set! tempLayer (car (gimp-image-get-active-layer tempImage)))
		      (gimp-layer-flatten tempLayer)
		      (gimp-deskew-plugin 0 tempImage tempLayer 0 0 0 0 0)
		      (gimp-image-resize-to-layers tempImage)
		      (gimp-layer-flatten tempLayer)
		      (plug-in-autocrop RUN-NONINTERACTIVE tempImage tempLayer)
		    )
	      )
		  (gimp-image-undo-enable tempImage)
		  (set! numextracted (+ numextracted 1))
	    )
      )		
	  (gimp-image-remove-vectors img (car tempVector))
	  (set! count (+ count 1))
	)

	;delete temp path
 	(gimp-image-remove-vectors img newpath)
	(gimp-selection-none img)
	
	;done
	(gimp-image-undo-enable img)
	(gimp-progress-end)
	(gimp-displays-flush)
	(gimp-context-pop)
  )
)

(script-fu-register "script_fu_DivideScannedImages"
        		    "/Filters/Divide Scanned Images..."
                    "Attempts to isolate each part of the image from the background and creates a new image from it"
                    "Rob Antonishen"
                    "Rob Antonishen"
                    "Dec 2008"
                    "RGB* GRAY*"
                    SF-IMAGE      "image"      0
                    SF-DRAWABLE   "drawable"   0
					SF-ADJUSTMENT "Selection Threshold"    (list 10 0 255 1 10 1 SF-SLIDER)
					SF-ADJUSTMENT "Size Threshold"         (list 100 0 2000 10 100 1 SF-SLIDER)		
					SF-ADJUSTMENT "Abort Limit"            (list 5 1 100 1 10 1 SF-SLIDER)							
					SF-ADJUSTMENT "Background Sample X Offset"    (list 5 1 100 1 10 1 SF-SLIDER)							
					SF-ADJUSTMENT "Background Sample Y Offset"    (list 5 1 100 1 10 1 SF-SLIDER)							
)


-Rob A>
Posted Image
Fantasy Cartography and Mapping by RobA
0

#14 User is offline   madthad 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 05-April 09

Posted 05 April 2009 - 06:32 PM

I am using the plugin "Divide Scanned Images" and is working well. My thanks to the author as it saved me some time, but now I am still wasting time doing the following:

If I place 4 photos on my fb scanner, scan them, then run the plugin, it correctly separates the scan into four images.

The problem is I now have to file, save as on each of the four images, enter a filename and extension, answer the jpg prompt and finally get the image to save.

Is there a script/plugin/a default value or another method I can use to quickly save all 4 images as jpg with a filename that increments from the last filename, such as image0001.jpg, image 0002.jpg, etc.

Thanks in advance.
0

#15 User is offline   RobA 

  • Member
  • PipPip
  • Group: Members
  • Posts: 721
  • Joined: 05-November 07

Posted 05 April 2009 - 11:08 PM

Well.... are you on windows or linux?

I have extended the script to allow this, using something called parasites, and discovered a bug in tiny-fu using parasites that can cause the script to fail ungracefully, or cause gimp to crash completely. It happens a lot on windows, and occasionally under linux.

I have posted the bug report, but suspect it won't get fixed for a while.

The only option I can think of to get it working is to port the whole thing over to python.... which I am working on, learning python as I go :)

-Rob A>
Posted Image
Fantasy Cartography and Mapping by RobA
0

#16 User is offline   madthad 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 05-April 09

Posted 20 April 2009 - 12:52 AM

Sorry for the long wait.

Running it on Vista.

Also, I have another issue. A number of my photos and business cards that have a lot of white background and the plugin is not picking up the photo separation properly. I was thinking about using an off-color such as Flourescent Green paper on the scanner's lid so when the white items are scanned, the plugin can pick up the separation easier. But by using the plugin, it assumes the green paper is part of the image and therefore no images are separated. Is there a way to tell the plugin to use a different color, etc. as the background separation.
0

#17 User is offline   RobA 

  • Member
  • PipPip
  • Group: Members
  • Posts: 721
  • Joined: 05-November 07

Posted 20 April 2009 - 01:40 AM

madthad-

The script samples the image using the background X and Y offsets to determine the "background" colour. So long as you set these offsets to get past any shadow it should work....

I have an updated version here: http://www.silent9.com/incoming/sc ... Images.scm

This one allows you to specify the corner to sample for the background colour, which is handy if your scanner has a larger shadow on a couple of edges, or if you want to align an image in the upper right hand corner.

It also will allow you to save the extracted files by specifying a directory and a file prefix and start number, then incrementing them for each image scanned. I haven;t played with the auto-saving much, but it seems to work OK.

-Rob A>

This post has been edited by RobA: 24 April 2013 - 02:09 AM

Posted Image
Fantasy Cartography and Mapping by RobA
0

#18 User is offline   madthad 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 05-April 09

Posted 20 April 2009 - 02:34 AM

Rob, thanks a bunch. I downloaded the new version and it is exactly what I was looking for. But... The issue with the business cards scanning comes down to this:
- Cards are 3.5" x 2" or 252px x 144px
- If Scanned at 75dpi, and then run through gimp and DivideScannedImages, the output size is smaller than 252x144. And that includes a minor background border that is attached to each separated image.
- If Scanned at 100dpi, and then run through gimp and DivideScannedImages, the output size is larger than 252x144. And that includes a minor background border that is attached to each separated image.
- It may be my scanner or the driver. not sure. But unfortunately for business card scanning I will not be able to use this. I have not tried photos yet. But that should be less of an issue than the cards as the cards need to be exact for the application I want to apply it to.

Again, Love the program and the concept. I will be testing with photos over the next week or two. Right now I need to focus on the business cards.

Thanks again,
Ted (madthad)
0

#19 User is offline   RobA 

  • Member
  • PipPip
  • Group: Members
  • Posts: 721
  • Joined: 05-November 07

Posted 20 April 2009 - 05:04 PM

madthad said:

Rob, thanks a bunch. I downloaded the new version and it is exactly what I was looking for. But... The issue with the business cards scanning comes down to this:
- Cards are 3.5" x 2" or 252px x 144px
- If Scanned at 75dpi, and then run through gimp and DivideScannedImages, the output size is smaller than 252x144. And that includes a minor background border that is attached to each separated image.
- If Scanned at 100dpi, and then run through gimp and DivideScannedImages, the output size is larger than 252x144. And that includes a minor background border that is attached to each separated image.
- It may be my scanner or the driver. not sure. But unfortunately for business card scanning I will not be able to use this. I have not tried photos yet. But that should be less of an issue than the cards as the cards need to be exact for the application I want to apply it to.

Again, Love the program and the concept. I will be testing with photos over the next week or two. Right now I need to focus on the business cards.

Thanks again,
Ted (madthad)


Ted - Something doesn't make sense there...the numbers you provide indicate 72 DPI (252/3.5 or 144/2).

Scanning at 75 DPI should give you 262 or 263 x 150 px and at 100 DPI should give you 350 x 200 px.

Do you have the deskew plugin installed? That could be changing the scale when it rotates...

-Rob A>
Posted Image
Fantasy Cartography and Mapping by RobA
0

#20 User is offline   madthad 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 05-April 09

Posted 20 April 2009 - 05:41 PM

Rob, you're right! Oops!

Early on I was using a program that does 72dpi by default (forgot which program). I must have jotted those numberz down somewhere and ran with it. I have been testing a bunch of other programs recently including gimp. I guess I did some assuming, not paying attention along the way, etc. Lukily I have not gone that far with my application, and it would not have mattered a few pixels off anyway.

Thanks for bringing that to my attention.

- OK, so assume 75dpi with 263/150.
- I place either 8 or 10 biz cards (testing purposes) on the scanner with about 3/8"-1/2" space around all edges.
- When I run DivideScannedImages, most will separate. I need to play with the brightness and other color options which may help in the separation.
- Take the dpi issue away, the main thing now is there is an 1/8" or so border around the images in the color of the underneath of the lid which is an grayish/white color. I could put a sheet of off-color paper over the cards and I will still get a 1/8" border after dividing but with the color of that paper. I presume the border is normal? Or is the issue with the border my scanner or the driver?

Please, don't take a lot of time with this. I have an alternate method. I have a Neat Receipts biz card scanner but it is a royal pain in the arse. By the time I scan 10 cards with it, I could have scanned 10 on the flatbad and edit it manually. So either way it takes a bunch of time.
0

Share this topic:


  • 4 Pages +
  • 1
  • 2
  • 3
  • Last »
  • You cannot start a new topic
  • You cannot reply to this topic