Created
October 30, 2022 11:52
-
-
Save SicroAtGit/d2c880ce69e5f16c2a97d43d5a870eb8 to your computer and use it in GitHub Desktop.
Removes the white borders from photos scanned with a scanner.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; MIT License | |
; | |
; Copyright (c) 2022 Alexander Bolli | |
; | |
; Permission is hereby granted, free of charge, to any person obtaining a copy | |
; of this software and associated documentation files (the "Software"), to deal | |
; in the Software without restriction, including without limitation the rights | |
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
; copies of the Software, and to permit persons to whom the Software is | |
; furnished to do so, subject to the following conditions: | |
; | |
; The above copyright notice and this permission notice shall be included in all | |
; copies or substantial portions of the Software. | |
; | |
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
; SOFTWARE. | |
EnableExplicit | |
UsePNGImageDecoder() | |
UsePNGImageEncoder() | |
Procedure GetXStart() | |
Protected width, height, x, y, color, brightness, whiteColorCounter, xStart | |
If StartDrawing(ImageOutput(0)) | |
width = ImageWidth(0) - 1 | |
height = ImageHeight(0) - 1 | |
For x = 50 To width | |
For y = 50 To height | |
color = Point(x, y) | |
brightness = Red(color) + Green(color) + Blue(color) | |
If brightness > (248 + 248 + 248) | |
whiteColorCounter + 1 | |
EndIf | |
Next | |
If whiteColorCounter < 3000 | |
xStart = x | |
Break | |
EndIf | |
whiteColorCounter = 0 | |
Next | |
StopDrawing() | |
EndIf | |
ProcedureReturn xStart | |
EndProcedure | |
Procedure GetXEnd() | |
Protected width, height, x, y, color, brightness, whiteColorCounter, xEnd | |
If StartDrawing(ImageOutput(0)) | |
width = ImageWidth(0) - 1 | |
height = ImageHeight(0) - 1 | |
For x = width - 50 To 50 Step -1 | |
For y = 50 To height | |
color = Point(x, y) | |
brightness = Red(color) + Green(color) + Blue(color) | |
If brightness > (248 + 248 + 248) | |
whiteColorCounter + 1 | |
EndIf | |
Next | |
If whiteColorCounter < 3000 | |
xEnd = x | |
Break | |
EndIf | |
whiteColorCounter = 0 | |
Next | |
StopDrawing() | |
EndIf | |
ProcedureReturn xEnd | |
EndProcedure | |
Procedure GetYEnd() | |
Protected width, height, y, x, color, brightness, whiteColorCounter, yEnd | |
If StartDrawing(ImageOutput(0)) | |
width = ImageWidth(0) - 1 | |
height = ImageHeight(0) - 1 | |
For y = height - 50 To 50 Step -1 | |
For x = 50 To width - 50 | |
color = Point(x, y) | |
brightness = Red(color) + Green(color) + Blue(color) | |
If brightness > (248 + 248 + 248) | |
whiteColorCounter + 1 | |
EndIf | |
Next | |
If whiteColorCounter < 2000 | |
yEnd = y | |
Break | |
EndIf | |
whiteColorCounter = 0 | |
Next | |
StopDrawing() | |
EndIf | |
ProcedureReturn yEnd | |
EndProcedure | |
Define path$, file$, xStart, newImage | |
path$ = PathRequester("Choose picture directory", "") | |
If path$ = "" | |
Debug "No image directory specified. The program exits now." | |
End | |
EndIf | |
If Not ExamineDirectory(0, path$, "*.png") | |
Debug "No 'png' image files found. The program exits now." | |
End | |
EndIf | |
If Not CreateDirectory(path$ + "Cut/") | |
Debug "'Cut' directory could not be created. The program exits now." | |
End | |
EndIf | |
While NextDirectoryEntry(0) | |
file$ = path$ + DirectoryEntryName(0) | |
If Not LoadImage(0, file$) | |
Debug "Image file could not be loaded: " + file$ | |
End | |
EndIf | |
Debug "Processing image file: " + file$ | |
xStart = GetXStart() | |
newImage = GrabImage(0, #PB_Any, xStart, 0, GetXEnd() - xStart, GetYEnd()) | |
If newImage = 0 | |
Debug "New image could not be extracted. The program exits now." | |
End | |
EndIf | |
If Not SaveImage(newImage, path$ + "Cut/" + DirectoryEntryName(0), #PB_ImagePlugin_PNG, 10) | |
Debug "New image file could not be saved: " + path$ + "Cut/" + DirectoryEntryName(0) | |
Debug "The program exits now." | |
End | |
EndIf | |
FreeImage(newImage) | |
FreeImage(0) | |
Wend | |
FinishDirectory(0) | |
Debug "All work done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment