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
| import random
import os import random import typing as t import requests from PIL.Image import new as createImage, Image, QUAD, BILINEAR from PIL.ImageDraw import Draw, ImageDraw from PIL.ImageFilter import SMOOTH from PIL.ImageFont import FreeTypeFont, truetype from io import BytesIO import time
ColorTuple = t.Union[t.Tuple[int, int, int], t.Tuple[int, int, int, int]]
DATA_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data') DEFAULT_FONTS = [os.path.join(DATA_DIR, 'DroidSansMono.ttf')]
class Captcha: lookup_table: t.List[int] = [int(i * 1.97) for i in range(256)]
def __init__(self, width: int = 160, height: int = 60, key: int = None, length: int = 4, fonts: t.Optional[t.List[str]] = None, font_sizes: t.Optional[t.Tuple[int]] = None): self._width = width self._height = height self._length = length tkey=int(time.time()) self._key = (key or tkey) + random.randint(1,100) self._fonts = fonts or DEFAULT_FONTS self._font_sizes = font_sizes or (42, 50, 56) self._truefonts: t.List[FreeTypeFont] = [] random.seed(key)
@property def truefonts(self) -> t.List[FreeTypeFont]: if self._truefonts: return self._truefonts self._truefonts = [ truetype(n, s) for n in self._fonts for s in self._font_sizes ] return self._truefonts
@staticmethod def create_noise_curve(image: Image, color: ColorTuple) -> Image: w, h = image.size x1 = random.randint(0, int(w / 5)) x2 = random.randint(w - int(w / 5), w) y1 = random.randint(int(h / 5), h - int(h / 5)) y2 = random.randint(y1, h - int(h / 5)) points = [x1, y1, x2, y2] end = random.randint(160, 200) start = random.randint(0, 20) Draw(image).arc(points, start, end, fill=color) return image
@staticmethod def create_noise_dots(image: Image, color: ColorTuple, width: int = 3, number: int = 30) -> Image: draw = Draw(image) w, h = image.size while number: x1 = random.randint(0, w) y1 = random.randint(0, h) draw.line(((x1, y1), (x1 - 1, y1 - 1)), fill=color, width=width) number -= 1 return image
def _draw_character(self, c: str, draw: ImageDraw, color: ColorTuple) -> Image: font = random.choice(self.truefonts)
left, top, right, bottom = draw.textbbox((0, 0), c, font=font) w = int((right - left)*1.7) or 1 h = int((bottom - top)*1.7) or 1
dx1 = random.randint(0, 4) dy1 = random.randint(0, 6) im = createImage('RGBA', (w + dx1, h + dy1)) Draw(im).text((dx1, dy1), c, font=font, fill=color)
im = im.crop(im.getbbox()) im = im.rotate(random.uniform(-30, 30), BILINEAR, expand=True)
dx2 = w * random.uniform(0.1, 0.3) dy2 = h * random.uniform(0.2, 0.3) x1 = int(random.uniform(-dx2, dx2)) y1 = int(random.uniform(-dy2, dy2)) x2 = int(random.uniform(-dx2, dx2)) y2 = int(random.uniform(-dy2, dy2)) w2 = w + abs(x1) + abs(x2) h2 = h + abs(y1) + abs(y2) data = ( x1, y1, -x1, h2 - y2, w2 + x2, h2 + y2, w2 - x2, -y1, ) im = im.resize((w2, h2)) im = im.transform((w, h), QUAD, data) return im
def create_captcha_image(self, chars: str, color: ColorTuple, background: ColorTuple) -> Image: image = createImage('RGB', (self._width, self._height), background) draw = Draw(image)
images: t.List[Image] = [] for c in chars: if random.random() > 0.5: images.append(self._draw_character(" ", draw, color)) images.append(self._draw_character(c, draw, color))
text_width = sum([im.size[0] for im in images])
width = max(text_width, self._width) image = image.resize((width, self._height))
average = int(text_width / len(chars)) rand = int(0.25 * average) offset = int(average * 0.1)
for im in images: w, h = im.size mask = im.convert('L').point(self.lookup_table) image.paste(im, (offset, int((self._height - h) / 2)), mask) offset = offset + w + random.randint(-rand, 0)
if width > self._width: image = image.resize((self._width, self._height))
return image
def generate_image(self, chars: str) -> Image: background = random_color(238, 255) color = random_color(10, 200, random.randint(220, 255)) im = self.create_captcha_image(chars, color, background) self.create_noise_dots(im, color) self.create_noise_curve(im, color) im = im.filter(SMOOTH) return im
def generate(self, format: str = 'png') -> (BytesIO,str): code = generate_code(self._length) im = self.generate_image(code) out = BytesIO() im.save(out, format=format) out.seek(0) return out, code
def write(self, output: str, format: str = 'png') -> (Image, str): code = generate_code(self._length) im = self.generate_image(code) im.save(output, format=format) return im, code
def random_color(start: int, end: int, opacity: t.Optional[int] = None) -> ColorTuple: red = random.randint(start, end) green = random.randint(start, end) blue = random.randint(start, end) if opacity is None: return (red, green, blue) return (red, green, blue, opacity)
def generate_code(length: int = 4): characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' return ''.join(random.choice(characters) for _ in range(length))
tkey=int(time.time()) print(tkey) open("1.jpg","wb").write(requests.get("http://124.70.33.170:23001/captcha").content) trueKey=input() Cookie="eyJjYXB0Y2hhIjoiVjhTUyIsInVzZXJuYW1lIjoiYWRtaW4ifQ.ZT3VKQ.b53eteLdneEgpLYTiDNTBwka7Cc" for i in range(0,101): gen = Captcha(200, 80,tkey+i) out,captcha_text = gen.generate() if(trueKey.lower()==captcha_text.lower()): print(captcha_text) captcha = generate_code() print(captcha) print(requests.post("http://124.70.33.170:23001/vip",json={"captcha":captcha},headers={"Cookie":Cookie}).headers["Set-Cookie"])
|