def add_num(img): draw = ImageDraw.Draw(img) myfont = ImageFont.truetype('C:/windows/fonts/Arial.ttf', size=40) fillcolor = "#ff0000" width, height = img.size draw.text((width - 40, 0), '99', font=myfont, fill=fillcolor) img.save('result.jpg', 'jpeg') return 0
def create_image(filename, size=(1280, 720), message="Text", bg_color=random_color(), number=-1, text_color="white"): """Creates a image .png file with the text""" # create an image that we can draw on img = Image.new("RGB", size, bg_color) draw = ImageDraw.Draw(img) # load font for the main text (message) font_main_text = ImageFont.truetype("FiraCode-Regular.ttf", 50) # get position so that the message is located in the middle of the image width_2, height_2 = draw.textsize(message, font=font_main_text) position_text = ((size[0] - width_2) / 2, (size[1] - height_2) / 2) # draw the text on the image draw.text(position_text, message, fill=text_color, font=font_main_text) # if wanted (number!=-1) add a sub text to the image if number != -1: # load font for the sub text font_sub_text = ImageFont.truetype("FiraCode-Regular.ttf", 30) # determine position of the sub text in the middle but slightly down width_2, height_2 = draw.textsize(">> Demo file #" + str(number), font=font_sub_text) position_text = ((size[0] - width_2) / 2, (size[1] - height_2) / 2 + 100) # draw the sub text on the image draw.text(position_text, ">> Demo file #" + str(number), fill=text_color, font=font_sub_text) # save draw object as a image draw = ImageDraw.Draw(img) img.save(filename)
def gradient(): global panel img = Image.new('RGB', (300, 200), (150, 150, 255)) draw = ImageDraw.Draw(img) draw.rectangle((100, 100, 150, 100), fill=(255, 120, 232)) draw.ellipse((25, 25, 75, 75), fill=(255, 110, 0)) #img.save('test.gif', 'GIF', transparency=0) img = img.resize((1250, 900), Image.ANTIALIAS) img = ImageTk.PhotoImage(img) panel = Label(root, image=img) panel.image = img panel.pack(side="top", fill="both", expand="NO")
def RandomGenerateOneChar(y=None, character=None, radius=20): ''' y == 1 汉字正 y ==-1 汉字倒 radius < 50 ''' choices = range(-30, 30) + range(-180, -150) + range(150, 180) angle = choice(choices) if y != None: while (angle <= 30 and angle >= -30) == (y == -1): angle = choice(choices) else: y = -1 if angle <= 30 and angle >= -30: y = 1 rad = radians(angle) if character == None: character = RandomGB2312() background = Image.new("RGBA", (160, 160), (255,255,255,255)) im = Image.new("RGBA", (72, 82), (0, 0, 0, 0)) global dir_path font = ImageFont.truetype(dir_path + "/Kaiti-SC-Bold.ttf", 72) dr = ImageDraw.Draw(im) dr.fontmode = "1" dr.text((0, 0), character, font=font, fill="#000000") fore = im.rotate(angle, expand=1) width, height = fore.size scale = np.random.uniform(0.8, 1.2) fore = fore.resize((int(width *scale), int(height*scale)), Image.ANTIALIAS) width, height = fore.size background.paste(fore, (80 - width/2 + randint(-10, 10), 80 -10*y - height/2 + randint(-10, 10)), fore) return background.crop((80-radius, 80-radius, 80+radius, 80+radius))
x2, y2 = (event.x + 1), (event.y + 1) cv.create_oval(x1, y1, x2, y2, fill="black", width=5) draw.line([x1, y1, x2, y2], fill="black", width=5) def clear(): cv.delete('all') draw.rectangle([0, 200, 200, 0], fill="white") root = Tk() root.title('HAND WRITTEN DIGIT RECOGNITION') cv = Canvas(root, width=width, height=height, bg='white') cv.pack() image1 = pil.Image.new("RGB", (width, height), white) draw = ImageDraw.Draw(image1) cv.pack(expand=YES, fill=BOTH) cv.bind("<B1-Motion>", paint) button = Button(text="save", command=save) button.pack() # Load the classifier clf = joblib.load("classifier.pkl") def predict(): # Read the input image im = cv2.imread("image.png") # Convert to grayscale and apply Gaussian filtering im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) im_gray = cv2.GaussianBlur(im_gray, (5, 5), 0)
def main(): pp = pprint.PrettyPrinter(indent=4) img_path = args.img_path label_path = args.label_path img_type = args.img_type datasets = args.datasets cls_list = args.cls_list_file result = None data = None if datasets == "COCO": coco = COCO() result, data = coco.parse(label_path) elif datasets == "VOC": voc = VOC() result, data = voc.parse(label_path) elif datasets == "UDACITY": udacity = UDACITY() result, data = udacity.parse(label_path, img_path) elif datasets == "KITTI": kitti = KITTI() result, data = kitti.parse(label_path, img_path, img_type=img_type) elif datasets == "YOLO": yolo = YOLO(os.path.abspath(cls_list)) result, data = yolo.parse(label_path, img_path, img_type=img_type) if result is True: for key in data: filepath = "".join([img_path, key, img_type]) im = Image.open(filepath) draw = ImageDraw.Draw(im) print("data['{}']: ".format(key), end="") pp.pprint(data[key]) print("num_object : {}".format(data[key]["objects"]["num_obj"])) for idx in range(0, int(data[key]["objects"]["num_obj"])): print("idx {}, name : {}, bndbox :{}".format( idx, data[key]["objects"][str(idx)]["name"], data[key]["objects"][str(idx)]["bndbox"])) x0 = data[key]["objects"][str(idx)]["bndbox"]["xmin"] y0 = data[key]["objects"][str(idx)]["bndbox"]["ymin"] x1 = data[key]["objects"][str(idx)]["bndbox"]["xmax"] y1 = data[key]["objects"][str(idx)]["bndbox"]["ymax"] draw.rectangle(((x0, y0), (x1, y1)), outline='#00ff88') draw.text((x0, y0), data[key]["objects"][str(idx)]["name"]) del draw print( "===============================================================================================\n\n" ) plt.imshow(im) plt.show() plt.clf() im.close() else: print("return value : {}, msg : {}, args: {}".format( result, data, args))
def PaintPoint(image, points=[]): im = image.copy() bgdr = ImageDraw.Draw(im) for y, x in points: bgdr.ellipse((x-3, y-3, x+3, y+3), fill ="red", outline ='red') return im