def get_code(request): mode = 'RGB' size = (200, 100) image = Image.new(mode=mode, size=size, color=rand_color()) image_draw = ImageDraw(image, mode=mode) font = ImageFont.truetype(code_font, 80) text = rand_wd().lower() request.session['verify_code'] = text for i in range(4): image_draw.text((45 * i + randrange(20), randrange(30)), text[i], fill=rand_color(), font=font) for i in range(6000): image_draw.point((randrange(201), randrange(101)), rand_color()) for i in range(randrange(3)): xy = ((randrange(201), randrange(101)), (randrange(201), randrange(101))) image_draw.line(xy, fill=rand_color(), width=2) fp = BytesIO() image.save(fp, 'png') return HttpResponse(fp.getvalue(), content_type='image/png')
def line_brezenhem( start: gr.Point , end: gr.Point , draw: ImageDraw , color=(255 , 0 , 0) ) : """ Draws line from start to end 2D Point via Brezenhem algorithm ( without gradient subline ) :param start: starting line 2D point :param end: ending line 2D point :param draw: drawing tool from Pillow module :param color: base painting color ( optional, red is default) """ # decompose point x0 , y0 = (start.x , start.y) x1 , y1 = (end.x , end.y) # reverse coordinates if y axis part of line longer thar x axis steeps = np.abs( x0 - x1 ) < np.abs( y0 - y1 ) if steeps : x0 , y0 = y0 , x0 x1 , y1 = y1 , x1 if x0 > x1 : x0 , x1 = x1 , x0 y0 , y1 = y1 , y0 dx = x1 - x0 dy = y1 - y0 derror = np.abs( dy / dx ) error = 0 y = y0 for i in np.arange( x0 , x1 , 1 ) : # Draw base line draw.point( [ i , y ] if not steeps else [ y , i ] , fill=color ) error += derror if error > 0.5 : y += 1 if y1 > y0 else -1 error -= 1
def set_noise(self, __image: ImageDraw, img_width, img_height): for i in range(self.max_line_count): # 噪线的起点横坐标和纵坐标 x1 = random.randint(0, img_width) y1 = random.randint(0, img_height) # 噪线的终点横坐标和纵坐标 x2 = random.randint(0, img_width) y2 = random.randint(0, img_height) # 通过画笔对象draw.line((起点的xy, 终点的xy), fill='颜色')来划线 __image.line((x1, y1, x2, y2), fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))) for i in range(self.max_point_count): __image.point( [random.randint(0, img_width), random.randint(0, img_height)], fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))) x = random.randint(0, img_width) y = random.randint(0, img_height) __image.arc((x, y, x + 4, y + 4), 0, 40, fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
def draw_pixel(point: od.Point, z_buffer, draw: ImageDraw, color=(255, 0, 0)): """ Draws pixel, if it's z depth is lower then previous points """ if point.third < z_buffer[point.first, point.second]: draw.point([point.first, point.second], fill=color) z_buffer[point.first, point.second] = point.third
def line_vu(start: od.Point, end: od.Point, draw: ImageDraw, color=(255, 0, 0)): """ Draws line from start to end 2D Point via Vu algorithm ( with gradient subline ) :param start: starting line 2D point :param end: ending line 2D point :param draw: drawing tool from Pillow module :param color: base painting color ( optional, red is default ) """ # decompose point x0, y0 = start.x, start.y x1, y1 = end.x, end.y # reverse coordinates if y axis part of line longer thar x axis steeps = np.abs(x0 - x1) < np.abs(y0 - y1) if steeps: x0, y0 = y0, x0 x1, y1 = y1, x1 dx = x1 - x0 dy = y1 - y0 if dx < 0: x0, x1 = x1, x0 y0, y1 = y1, y0 derror = np.abs(dy / dx) error = 0 y = y0 sy = 1 if y1 > y0 else -1 for i in np.arange(x0, x1, 1): base_color = tuple([int(i * (1 - error)) for i in list(color)]) add_color = tuple([int(i * error) for i in list(color)]) # Draw base line with subline draw.point([i, y] if not steeps else [y, i], fill=base_color) draw.point([i, y + sy] if not steeps else [y + sy, i], fill=add_color) error += derror if error > 1: y += sy error -= 1
def draw_cursor(self, position: int, draw: ImageDraw, color:(int, int, int)) -> None: """ draws a color cursor on the image at the position. Args: position (int): the index of the cursor. draw (ImageDraw): the drawing object. color (int, int, int, int): RGB color code. Modifies: draw: a color cursor is drawn onto the image associated with the drawing object. Returns: None. """ if position < 0 or position > (self.num_bars - 1): raise InputError((position), "invalid cursor position") cursor_width = self.bar_width x_start = self.border_size + math.ceil(self.padding/2) cursor_center = x_start + (2*self.bar_width)*position + self.bar_width//2 max_left_bound = x_start + (2*self.bar_width)*position max_right_bound = max_left_bound + self.bar_width top_bound = self.image_height - self.border_size + 1 bottom_bound = self.image_height - math.ceil(self.border_size/2) points = [] left_bound = cursor_center right_bound = cursor_center + 1 for y in range (top_bound, bottom_bound): left_bound = max(max_left_bound, left_bound) right_bound = min(max_right_bound, right_bound) for x in range (left_bound, right_bound): points.append((x, y)) left_bound = left_bound - 1 right_bound = right_bound + 1 draw.point(points, color)
def draw(self, draw: ImageDraw): draw.point(self.points, self.fill)
def draw(self, draw: ImageDraw): draw.point([self.x, self.y], self.color)
# 隨機顏色 def rndColor(): return (random.readint(64, 255), random.readint(64, 255), random.readint(64, 255)) # 隨機顏色2 def rndColor2(): return (random.readint(32, 127), random.readint(32, 127), random.readint(32, 127)) width = 60 * 4 height = 60 image = Image.new('RGB', (width, height), (255, 255, 255)) # 創建font對象 font = ImageFont.truetype('/Library/Fonts/Arial.ttf', 36) # 創建Draw對象 draw = ImageDraw(image) for x in range(width): for y in range(height): draw.point((x, y), fill=rndColor()) image = image.filter(ImageFilter.BLUR) image.save('code.jpg', 'jpeg')