def create_image(storage, filename, size=(100, 100), image_mode='RGB', image_format='PNG'): data = BytesIO() Image.new(image_mode, size).save(data, image_format) data.seek(0) if not storage: return data image_file = ContentFile(data.read()) return storage.save(filename, image_file)
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))
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 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)
# Create the ST7789 display: disp = st7789.ST7789(spi, cs=cs_pin, dc=dc_pin, rst=reset_pin, baudrate=BAUDRATE, width=135, height=240, x_offset=53, y_offset=40) # Create blank image for drawing. # Make sure to create image with mode 'RGB' for full color. height = disp.width # we swap height/width to rotate it to landscape! width = disp.height image = Image.new('RGB', (width, height)) rotation = 90 # Get drawing object to draw on image. draw = ImageDraw.Draw(image) # Draw a black filled box to clear the image. draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0)) disp.image(image, rotation) # Draw some shapes. # First define some constants to allow easy resizing of shapes. padding = -2 top = padding bottom = height - padding # Move left to right keeping track of the current x position for drawing shapes. x = 0
def create_empty_pil_image(pil_image): """Create an empty PIL Image.""" return Image.new('RGB', (pil_image.size[0], pil_image.size[1]))