Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_Captcha_Generator.py
- import tkinter as tk
- from PIL import Image, ImageDraw, ImageFont, ImageTk, ImageFilter
- import os
- import winreg
- import random
- import string
- import math
- import tkinter.font as tkfont
- root = tk.Tk()
- root.title("# tk_Captcha_Generator.py")
- root.geometry("+-10+0")
- canvas = tk.Canvas(root, bg="white", highlightthickness=0)
- image_label = tk.Label(root)
- image_label.pack()
- characters = string.ascii_uppercase + string.ascii_lowercase + string.digits
- dir_ = "C:\Windows\Fonts\\"
- font_list = '''
- Candara
- Candarab
- Candarai
- Candaral
- Candarali
- Candaraz
- Deng
- Dengb
- Dengl
- Gabriola
- Inkfree
- LatoWeb-Bold
- LatoWeb-BoldItalic
- LatoWeb-Italic
- LatoWeb-Light
- LatoWeb-LightItalic
- LatoWeb-Regular
- LatoWeb-Semibold
- LatoWeb-SemiboldItalic
- LeelUIsl
- LeelaUIb
- LeelawUI
- Nirmala
- NirmalaB
- NirmalaS
- NotoSansSC-VF
- SimsunExtG
- Sitka.
- SitkaB.
- SitkaI.
- SitkaZ.
- UrdType
- UrdTypeb
- YuGothB.
- YuGothL.
- YuGothM.
- YuGothR.
- aldhabi
- andlso
- arabtype
- arial
- arialbd
- arialbi
- ariali
- ariblk
- bahnschrift
- calibri
- calibrib
- calibrii
- calibril
- calibrili
- calibriz
- cambria.
- cambriab
- cambriai
- cambriaz
- consola
- consolab
- consolai
- consolaz
- constan
- constanb
- constani
- constanz
- corbel
- corbelb
- corbeli
- corbell
- corbelli
- corbelz
- cour
- courbd
- courbi
- couri
- ebrima
- ebrimabd
- framd
- framdit
- gadugi
- gadugib
- georgia
- georgiab
- georgiai
- georgiaz
- himalaya
- impact
- javatext
- l_10646
- lucon
- majalla
- majallab
- malgun
- malgunbd
- malgunsl
- micross
- mingliub.
- mmrtext
- mmrtextb
- monbaiti
- msgothic.
- msjh.
- msjhbd.
- msjhl.
- msuighub
- msuighur
- msyh.
- msyhbd.
- msyhl.
- msyi
- mvboli
- ntailu
- ntailub
- pala
- palab
- palabi
- palai
- phagspa
- phagspab
- segoepr
- segoeprb
- segoesc
- segoescb
- segoeui
- segoeuib
- segoeuii
- segoeuil
- segoeuisl
- segoeuiz
- seguibl
- seguibli
- seguiemj
- seguihis
- seguili
- seguisb
- seguisbi
- seguisli
- seguisym
- simfang
- simhei
- simkai
- simpbdo
- simpfxo
- simpo
- simsun.
- simsunb
- sylfaen
- tahoma
- tahomabd
- taile
- taileb
- times
- timesbd
- timesbi
- timesi
- tradbdo
- trado
- trebuc
- trebucbd
- trebucbi
- trebucit
- verdana
- verdanab
- verdanai
- verdanaz
- '''.strip().splitlines()
- font_list = [dir_+font+'ttc' if '.' in font else dir_+font+'.ttf' for font in font_list]
- def generate_regular_captcha():
- img = Image.new('RGBA', (40*6+40, 90), (255, 255, 255, 0))
- captcha_text = ""
- for i in range(0, 40*6, 40):
- canvas_size = 600
- img_base = Image.new('RGBA', (canvas_size, canvas_size), (255, 255, 255, 0))
- char = random.choice(characters)
- captcha_text += char
- font_name = random.choice(font_list)
- rot = random.uniform(-20, 20)
- x = random.randint(-5, 5)
- y = random.randint(-2, 2)
- font_size = random.randint(270, 320)
- font_obj = ImageFont.truetype(font_name, font_size)
- color = get_color()
- draw = ImageDraw.Draw(img_base)
- draw.text((canvas_size//3, canvas_size//3), char, font=font_obj, fill=color, anchor="mm")
- img_base = img_base.rotate(rot, expand=False)
- img_base = img_base.resize((120, 120), Image.LANCZOS)
- img.alpha_composite(img_base, (x + i, y))
- img = generate_noise(img)
- return img, captcha_text
- def generate_noise(img):
- w, h = img.size
- noise_layer = Image.new('RGBA', (w, h), (255, 255, 255, 0))
- draw = ImageDraw.Draw(noise_layer)
- square_size = 10
- a = 80
- for x in range(0, w, square_size):
- for y in range(0, h, square_size):
- noise_r = random.randint(0, 255)
- noise_g = random.randint(0, 255)
- noise_b = random.randint(0, 255)
- draw.rectangle(
- (x, y, x + square_size, y + square_size),
- fill=(noise_r, noise_g, noise_b, a)
- )
- blended = Image.alpha_composite(img, noise_layer)
- return blended
- def get_color():
- max_sum = int(255 * 1.25)
- r = random.randint(0, 255)
- g = random.randint(0, min(255, max_sum - r))
- b = random.randint(0, min(255, max_sum - r - g))
- return f"#{r:02x}{g:02x}{b:02x}"
- def show():
- captcha_img, test_label = generate_regular_captcha()
- photo = ImageTk.PhotoImage(captcha_img)
- image_label.config(image=photo)
- image_label.image = photo
- root.after(1000, show)
- show()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment