生成图片

from PIL import Image, ImageColor, ImageDraw, ImageFont, ImageFilter


def create_image_with_text(size, color, text, font_path, font_size, text_color, shadow_color, output_path):
"""
Create a new image of specified size and color with centered text that has a border and shadow.

:param size: A tuple containing the width and height (width, height) for the new image.
:param color: The background color of the new image as a string or a tuple.
:param text: The text to draw on the image.
:param font_path: Path to the .ttf font file.
:param font_size: Size of the text.
:param text_color: Color of the text as a string or a tuple.
:param shadow_color: Color of the shadow as a string or a tuple.
:param output_path: Path where the new image will be saved.
"""
width, height = size
# Create a new image with the given size and color
img = Image.new('RGBA', (width, height), color)

# Load a font and initialize an ImageDraw object
try:
font = ImageFont.truetype(font_path, font_size)
except IOError:
print("Font not found, using default font.")
font = ImageFont.load_default()

draw = ImageDraw.Draw(img)

# Calculate the position to center the text
left, top, right, bottom = draw.textbbox((0, 0), text, font=font)
text_width = right - left
text_height = bottom - top
x = (width - text_width) / 2
y = (height - text_height) / 2

# Define the offset for the shadow and the thickness for the border
shadow_offset = 2
border_thickness = 1

# Draw the shadow first
shadow_x = x + shadow_offset
shadow_y = y + shadow_offset
draw.text((shadow_x, shadow_y), text, fill=shadow_color, font=font)

# Apply blur filter to the shadow for a softer look
shadow_img = img.copy().convert('RGBA')
shadow_draw = ImageDraw.Draw(shadow_img)
shadow_draw.text((shadow_x, shadow_y), text, fill=shadow_color, font=font)
shadow_img = shadow_img.filter(ImageFilter.GaussianBlur(radius=2))

# Paste the blurred shadow back onto the original image
img.paste(shadow_img, mask=shadow_img)

# Draw the border by drawing the text multiple times in different positions
for dx, dy in [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]:
draw.text((x + dx * border_thickness, y + dy * border_thickness), text, fill='black', font=font)

# Finally, draw the main text over everything
draw.text((x, y), text, fill=text_color, font=font)

# Save the new image
img.save(output_path, format='PNG') # Use PNG to preserve transparency
print(f"Image created and saved to {output_path}")


# Example usage with a Chinese supporting font
image_size = (544, 334) # Width: 544px, Height: 334px
image_color = '#04C171' # Background color of the image
text_to_add = 'PyQt6' # Text to add to the image
font_file_path = 'C:/Windows/Fonts/simhei.ttf' # Path to your .ttf font file that supports Chinese characters
font_size = 100 # Size of the text
text_color = '#FFED00' # Color of the text
shadow_color = '#FFED00' # Color of the shadow
output_file_path = '2.png'

create_image_with_text(image_size, image_color, text_to_add, font_file_path, font_size, text_color, shadow_color,
output_file_path)


相关推荐

  • 生成图片

    from PIL import Image, ImageColor, ImageDraw, ImageFont, ImageFilterdef create_image_with_text(size, color, text, font_path, font_size, text_color, shadow_color, output_path): """ Create a new image of specified size and color with centered text that has a border and shadow. :param size: A tuple con

  • 获取指定目录下的所有图片信息

    1 获取指定目录下的所有图片信息// 获取指定目录下的所有图片信息 public function getImagesInfo($directory) { $images = []; // 创建递归目录迭代器 $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY ); // 遍历目录中的每个文件 foreach (

  • Thinkphp各版本的PHP要求

    ThinkPHP 8.0:运行环境要求PHP8.0+,兼容PHP8.3ThinkPHP 6.1:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 6.0:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 5.1:运行环境要求PHP5.6+,兼容PHP8.0ThinkPHP 5.0:运行环境要求PHP5.4+,兼容PHP7.3