-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_image.scpt
executable file
·227 lines (197 loc) · 8.24 KB
/
process_image.scpt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
(*
Script: process_image.scpt
Used to process an image file and writing the recognized text to a text file.
The recognized text is written to a text file with the same name as the image file but with a .txt extension.
The script uses the Vision framework to recognize text in the image.
Usage:
Help:
osascript /my/script/process_image.scpt --help
Single image:
osascript /my/script/process_image.scpt "/my/images/image.png"
Multiple images:
find /my/images \( -name "*.png" -o -name "*.jpg" -o -name "*.jpeg" \) -type f -exec \
bash -c 'p="$(realpath "{}")"; [[ ! "$p" =~ ^\./ ]] && osascript /my/script/process_image.scpt "$p" \;
2024 felipe.dos.santos
*)
global logFile
global scriptPath
-- Parsed Arguments
global argDetectLanguage
global argLanguage
global argLanguageCorrection
global imageFile
use framework "Foundation"
use framework "Vision"
use scripting additions
-- run: Process an image file and write the recognized text to a text file.
-- Parameters:
-- args (alias) - The file path of the image to process. (Quoted form)
-- Returns: Nothing.
on run args
if not init(args) then
return
end if
-- log("INFO", "Processing image: " & args)
-- set imageFile to POSIX path of args
set theText to getText(imageFile)
if theText is "" then
log("WARN", "No text recognized in image: " & args)
return
else
set resultFile to getResultFileName(imageFile, ".txt")
writeFile(resultFile, theText)
end if
end run
-- init: Initialize the script log file.
-- Parameters: None.
-- Returns: Nothing.
to init(args)
set scriptPath to (path to me)
set scriptPath to do shell script "dirname " & quoted form of POSIX path of scriptPath
set logFile to (name of me)
set logFile to do shell script "basename " & quoted form of logFile & "_log.txt"
set logFile to scriptPath & "/" & logFile
set argDetectLanguage to false
set argLanguage to "en"
set argLanguageCorrection to false
set imageFile to missing value
if args is missing value or args is {} then
printHelp()
return false
end if
repeat with i from 1 to count of args
set thisArg to item i of args
-- display dialog quoted form of thisArg
if thisArg is "-h" or thisArg is "--help" then
printHelp()
return false
else if thisArg is "-d" or thisArg is "--detect-language" then
set argDetectLanguage to true
else if thisArg is "-l" or thisArg is "--language" then
set argLanguage to item (i + 1) of args
else if thisArg is "-c" or thisArg is "--language-correction" then
set argLanguageCorrection to true
else if thisArg is not missing value and thisArg does not start with "-" then
try
set imageFile to POSIX path of thisArg
on error
log("ERROR", "Invalid image file: " & thisArg)
end try
end if
end repeat
if imageFile is missing value then
log("ERROR", "No image file specified.")
return false
end if
if argDetectLanguage is true then
log("INFO", "Detecting language enabled.")
end if
if argLanguage is not missing value then
log("INFO", "Language: " & argLanguage)
end if
if argLanguageCorrection is true then
log("INFO", "Language correction enabled.")
end if
return true
end init
on printHelp()
set h to "Used to process an image file and writing the recognized text to a text file.
The recognized text is written to a text file with the same name as the image file but with a .txt extension.
Usage:
osascript /path/process_image.scpt \"/my/image.png\"
Flags:
-h, --help: Display this help message.
-d, --detect-language: Automatically detect the language. Default is disabled.
-l, --language ISO 639-1 string: Enable language correction. Default is disabled. Default is 'en'.
-c, --language-correction: Enable language correction. Default is disabled.
"
display dialog h with title "Help" buttons {"OK"} default button "OK"
end printHelp
-- getResultFileName: Generate the output file name for the recognized text.
-- Parameters:
-- imageFile (text) - The path to the image file.
-- extension (text) - The desired file extension for the output file.
-- Returns: (text) The full path of the result file.
on getResultFileName(imageFile, extension)
set imageAlias to POSIX file imageFile as alias
tell application "System Events"
set fileName to name of imageAlias
set fileExtension to name extension of imageAlias
set resultFile to my removeExtension(fileName, fileExtension)
end tell
set resultPath to (POSIX path of (imageAlias as string))
return (POSIX path of (do shell script "dirname " & quoted form of resultPath)) & "/" & resultFile & extension
end getResultFileName
-- removeExtension: Remove the extension from a file name.
-- Parameters:
-- fileName (text) - The file name.
-- fileExt (text) - The file extension to remove.
-- Returns: (text) The file name without the extension.
on removeExtension(fileName, fileExt)
if fileExt is missing value or fileExt is "" then return fileName
return text 1 thru ((count fileName) - (count fileExt) - 1) of fileName
end removeExtension
-- getText: Recognize text in an image file using Vision framework.
-- Parameters:
-- imageFile (text) - The path to the image file.
-- Returns: (text) The recognized text from the image.
on getText(imageFile)
try
set imageFileURL to current application's NSURL's fileURLWithPath:(POSIX path of imageFile)
set requestHandler to current application's VNImageRequestHandler's alloc()'s initWithURL:imageFileURL options:(missing value)
set theRequest to current application's VNRecognizeTextRequest's alloc()'s init()
theRequest's setAutomaticallyDetectsLanguage:argDetectLanguage
theRequest's setUsesLanguageCorrection:argLanguageCorrection
if argLanguage is not missing value then
theRequest's setRecognitionLanguages:{argLanguage}
end if
set success to requestHandler's performRequests:(current application's NSArray's arrayWithObject:(theRequest)) |error|:(missing value)
if success as boolean is false then error "Failed to perform text recognition."
set theResults to theRequest's results()
set theArray to current application's NSMutableArray's new()
repeat with aResult in theResults
(theArray's addObject:(((aResult's topCandidates:1)'s objectAtIndex:0)'s |string|()))
end repeat
return (theArray's componentsJoinedByString:linefeed) as Unicode text
on error errMsg number errNum
log("ERROR", "Error in getText: " & errMsg & " (" & errNum & ") - " & imageFile)
return ""
end try
end getText
-- writeFile: Write text data to a file.
-- Parameters:
-- fileName (text) - The path to the file.
-- textData (text) - The text data to write.
-- Returns: Nothing.
on writeFile(fileName, textData)
try
set the fileDescriptor to open for access fileName with write permission
set eof of the fileDescriptor to 0
write textData to the fileDescriptor starting at eof
close access the fileDescriptor
on error errMsg number errNum
try
close access file fileName
end try
log("ERROR", "Error in writeFile: " & errMsg & " (" & errNum & ") - " & fileName)
end try
end writeFile
-- log: Write a message to the script log file.
-- Parameters:
-- type (text) - The type of message (INFO, WARN, ERROR).
-- message (text) - The message to write to the log.
-- Returns: Nothing.
on log(type, message)
try
set logTimestamp to do shell script "date +'%Y-%m-%d %H:%M:%S'"
set logMessage to logTimestamp & " [" & type & "] " & message & linefeed
do shell script "echo " & quoted form of logMessage
set the logDescriptor to open for access logFile with write permission
write logMessage to the logDescriptor starting at eof
close access the logDescriptor
on error
try
close access file logFile
end try
end try
end log