captcha.py: Add json output mapping

Bug: T370535
Change-Id: I0c8964b3d6ef087bd41ed11cfaa91cd83fff6275
This commit is contained in:
Reedy 2024-07-19 16:57:19 +01:00
parent db5ce2dba1
commit 99b1478884

View file

@ -33,6 +33,7 @@ import os
import sys import sys
import re import re
import multiprocessing import multiprocessing
import json
try: try:
from PIL import Image from PIL import Image
@ -262,6 +263,7 @@ def run_in_thread(object):
opts = object[3] opts = object[3]
font = object[4] font = object[4]
fontsize = object[5] fontsize = object[5]
jsonmap = object[6]
for i in range(count): for i in range(count):
word = pick_word( word = pick_word(
@ -283,6 +285,9 @@ def run_in_thread(object):
filename = os.path.join(subdir, filename) filename = os.path.join(subdir, filename)
if opts.verbose: if opts.verbose:
print(filename) print(filename)
if opts.jsonmap:
jsonmap[filename] = word
gen_captcha(word, font, fontsize, os.path.join(opts.output, filename)) gen_captcha(word, font, fontsize, os.path.join(opts.output, filename))
@ -379,10 +384,15 @@ if __name__ == "__main__":
) )
parser.add_option( parser.add_option(
"--threads", "--threads",
help="Maximum number of threads to be used to generate captchas.", help="Maximum number of threads to be used to generate captchas",
type="int", type="int",
default=1, default=1,
) )
parser.add_option(
"--jsonmap",
help="Outputs \"filename\": \"word\" mapping for test/debug purposes",
action="store_true"
)
opts, args = parser.parse_args() opts, args = parser.parse_args()
@ -438,7 +448,13 @@ if __name__ == "__main__":
"Generating %s CAPTCHA images separated in %s image(s) per chunk run by %s threads..." "Generating %s CAPTCHA images separated in %s image(s) per chunk run by %s threads..."
% (count, chunks, threads) % (count, chunks, threads)
) )
jsonmap = multiprocessing.Manager().dict()
for i in range(0, threads): for i in range(0, threads):
data.append([chunks, words, badwordlist, opts, font, fontsize]) data.append([chunks, words, badwordlist, opts, font, fontsize, jsonmap])
p.map(run_in_thread, data) result = p.map_async(run_in_thread, data)
result.wait()
if opts.jsonmap:
with open("map.json", "w") as outfile:
json.dump(jsonmap.copy(), outfile, indent=4)