here2share

# tk_Captcha_Generator.py

Feb 5th, 2026
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.05 KB | None | 0 0
  1. # tk_Captcha_Generator.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageDraw, ImageFont, ImageTk, ImageFilter
  5. import os
  6. import winreg
  7. import random
  8. import string
  9. import math
  10. import tkinter.font as tkfont
  11.  
  12. root = tk.Tk()
  13. root.title("# tk_Captcha_Generator.py")
  14. root.geometry("+-10+0")
  15.  
  16. canvas = tk.Canvas(root, bg="white", highlightthickness=0)
  17.  
  18. image_label = tk.Label(root)
  19. image_label.pack()
  20.  
  21. characters = string.ascii_uppercase + string.ascii_lowercase + string.digits
  22.  
  23. dir_ = "C:\Windows\Fonts\\"
  24. font_list = '''
  25. Candara
  26. Candarab
  27. Candarai
  28. Candaral
  29. Candarali
  30. Candaraz
  31. Deng
  32. Dengb
  33. Dengl
  34. Gabriola
  35. Inkfree
  36. LatoWeb-Bold
  37. LatoWeb-BoldItalic
  38. LatoWeb-Italic
  39. LatoWeb-Light
  40. LatoWeb-LightItalic
  41. LatoWeb-Regular
  42. LatoWeb-Semibold
  43. LatoWeb-SemiboldItalic
  44. LeelUIsl
  45. LeelaUIb
  46. LeelawUI
  47. Nirmala
  48. NirmalaB
  49. NirmalaS
  50. NotoSansSC-VF
  51. SimsunExtG
  52. Sitka.
  53. SitkaB.
  54. SitkaI.
  55. SitkaZ.
  56. UrdType
  57. UrdTypeb
  58. YuGothB.
  59. YuGothL.
  60. YuGothM.
  61. YuGothR.
  62. aldhabi
  63. andlso
  64. arabtype
  65. arial
  66. arialbd
  67. arialbi
  68. ariali
  69. ariblk
  70. bahnschrift
  71. calibri
  72. calibrib
  73. calibrii
  74. calibril
  75. calibrili
  76. calibriz
  77. cambria.
  78. cambriab
  79. cambriai
  80. cambriaz
  81. consola
  82. consolab
  83. consolai
  84. consolaz
  85. constan
  86. constanb
  87. constani
  88. constanz
  89. corbel
  90. corbelb
  91. corbeli
  92. corbell
  93. corbelli
  94. corbelz
  95. cour
  96. courbd
  97. courbi
  98. couri
  99. ebrima
  100. ebrimabd
  101. framd
  102. framdit
  103. gadugi
  104. gadugib
  105. georgia
  106. georgiab
  107. georgiai
  108. georgiaz
  109. himalaya
  110. impact
  111. javatext
  112. l_10646
  113. lucon
  114. majalla
  115. majallab
  116. malgun
  117. malgunbd
  118. malgunsl
  119. micross
  120. mingliub.
  121. mmrtext
  122. mmrtextb
  123. monbaiti
  124. msgothic.
  125. msjh.
  126. msjhbd.
  127. msjhl.
  128. msuighub
  129. msuighur
  130. msyh.
  131. msyhbd.
  132. msyhl.
  133. msyi
  134. mvboli
  135. ntailu
  136. ntailub
  137. pala
  138. palab
  139. palabi
  140. palai
  141. phagspa
  142. phagspab
  143. segoepr
  144. segoeprb
  145. segoesc
  146. segoescb
  147. segoeui
  148. segoeuib
  149. segoeuii
  150. segoeuil
  151. segoeuisl
  152. segoeuiz
  153. seguibl
  154. seguibli
  155. seguiemj
  156. seguihis
  157. seguili
  158. seguisb
  159. seguisbi
  160. seguisli
  161. seguisym
  162. simfang
  163. simhei
  164. simkai
  165. simpbdo
  166. simpfxo
  167. simpo
  168. simsun.
  169. simsunb
  170. sylfaen
  171. tahoma
  172. tahomabd
  173. taile
  174. taileb
  175. times
  176. timesbd
  177. timesbi
  178. timesi
  179. tradbdo
  180. trado
  181. trebuc
  182. trebucbd
  183. trebucbi
  184. trebucit
  185. verdana
  186. verdanab
  187. verdanai
  188. verdanaz
  189. '''.strip().splitlines()
  190.  
  191. font_list = [dir_+font+'ttc' if '.' in font else dir_+font+'.ttf' for font in font_list]
  192.        
  193. def generate_regular_captcha():
  194.     img = Image.new('RGBA', (40*6+40, 90), (255, 255, 255, 0))
  195.     captcha_text = ""
  196.    
  197.     for i in range(0, 40*6, 40):
  198.         canvas_size = 600
  199.         img_base = Image.new('RGBA', (canvas_size, canvas_size), (255, 255, 255, 0))
  200.         char = random.choice(characters)
  201.         captcha_text += char
  202.         font_name = random.choice(font_list)
  203.         rot = random.uniform(-20, 20)
  204.         x = random.randint(-5, 5)
  205.         y = random.randint(-2, 2)
  206.         font_size = random.randint(270, 320)
  207.         font_obj = ImageFont.truetype(font_name, font_size)
  208.         color = get_color()
  209.        
  210.         draw = ImageDraw.Draw(img_base)
  211.         draw.text((canvas_size//3, canvas_size//3), char, font=font_obj, fill=color, anchor="mm")
  212.         img_base = img_base.rotate(rot, expand=False)
  213.         img_base = img_base.resize((120, 120), Image.LANCZOS)
  214.         img.alpha_composite(img_base, (x + i, y))
  215.        
  216.     img = generate_noise(img)
  217.     return img, captcha_text
  218.  
  219. def generate_noise(img):
  220.     w, h = img.size
  221.    
  222.     noise_layer = Image.new('RGBA', (w, h), (255, 255, 255, 0))
  223.     draw = ImageDraw.Draw(noise_layer)
  224.    
  225.     square_size = 10
  226.     a = 80
  227.    
  228.     for x in range(0, w, square_size):
  229.         for y in range(0, h, square_size):
  230.             noise_r = random.randint(0, 255)
  231.             noise_g = random.randint(0, 255)
  232.             noise_b = random.randint(0, 255)
  233.            
  234.             draw.rectangle(
  235.                 (x, y, x + square_size, y + square_size),
  236.                 fill=(noise_r, noise_g, noise_b, a)
  237.             )
  238.    
  239.     blended = Image.alpha_composite(img, noise_layer)
  240.    
  241.     return blended
  242.    
  243. def get_color():
  244.     max_sum = int(255 * 1.25)
  245.  
  246.     r = random.randint(0, 255)
  247.     g = random.randint(0, min(255, max_sum - r))
  248.     b = random.randint(0, min(255, max_sum - r - g))
  249.  
  250.     return f"#{r:02x}{g:02x}{b:02x}"
  251.  
  252. def show():
  253.     captcha_img, test_label = generate_regular_captcha()
  254.    
  255.     photo = ImageTk.PhotoImage(captcha_img)
  256.     image_label.config(image=photo)
  257.     image_label.image = photo
  258.    
  259.     root.after(1000, show)
  260.  
  261. show()
  262.  
  263. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment