【拼图并加标注】

一、jupyter 代码

拍摄时,需要保持图片大小比例一样,全部横屏或竖屏拍摄,将照片按1-18命名。

图像按纵向顺序排列

pip install Pillow
from PIL import Image, ImageDraw, ImageFont

def create_composite_image(image_paths, labels, output_path, crop_size, margin, font_path, font_size, text_color, box_color):
    images = [Image.open(img_path).resize(crop_size) for img_path in image_paths]
    font = ImageFont.truetype(font_path, font_size)
    
    # Calculate the number of rows and columns
    rows = 3
    cols = 6
    
    # Calculate total width and height of the composite image including margins
    total_width = crop_size[0] * cols + margin * (cols - 1)
    total_height = crop_size[1] * rows + margin * (rows - 1)
    
    # Create a new composite image with a white background
    composite_image = Image.new('RGB', (total_width, total_height), color=(255, 255, 255))
    draw = ImageDraw.Draw(composite_image, "RGBA")
    
    for index, (img, label) in enumerate(zip(images, labels)):
        col = index // rows
        row = index % rows
        x_offset = col * (crop_size[0] + margin)
        y_offset = row * (crop_size[1] + margin)
        
        composite_image.paste(img, (x_offset, y_offset))
        
        # Calculate text position using textbbox
        text_bbox = draw.textbbox((0, 0), label, font=font)
        text_width = text_bbox[2] - text_bbox[0]
        text_height = text_bbox[3] - text_bbox[1]
        text_x = x_offset + (crop_size[0] - text_width) / 2
        
        # Draw the semi-transparent blue background box
        box_height = text_height + 20  # Add padding to the text box height
        box_x0 = x_offset
        box_y0 = y_offset + crop_size[1] - box_height
        box_x1 = x_offset + crop_size[0]
        box_y1 = y_offset + crop_size[1]
        draw.rectangle([box_x0, box_y0, box_x1, box_y1], fill=box_color)  # Lighter blue with 40% opacity
        
        # Draw the text slightly above the center of the box
        text_y_centered = box_y0 + (box_height - text_height) / 4  # Move text closer to the top
        
        # Simulate bold text by drawing the text multiple times
        for offset in [(0, 0), (-1, 0), (1, 0), (0, -1), (0, 1)]:
            draw.text((text_x + offset[0], text_y_centered + offset[1]), label, font=font, fill=text_color)
    
    composite_image.save(output_path)
    return composite_image

# Usage example
image_paths = [r"D:\\00workpart\\else _work\\拼图0801\\{}.jpg".format(i) for i in range(1, 19)]
labels = ["CK2", "M1", "M2", "M2", "CK2", "M1", "M1", "M2", "CK2", "CK1", "S1", "S2", "S2", "CK1", "S1", "S1", "S2", "CK1"]
output_path = "D:\\00workpart\\else _work\\拼图0801\\image0801.jpg"
crop_size = (400, 400)  # Increase the resolution of each image
margin = 10  # Margin between images
font_path = r'C:\Windows\Fonts\times.ttf'  # Path to the Times New Roman font
font_size = 50  # Increase font size
text_color = 'white'
box_color = (173, 216, 230, 90)  # Very light blue with 40% opacity

composite_image = create_composite_image(image_paths, labels, output_path, crop_size, margin, font_path, font_size, text_color, box_color)
composite_image.show()

效果图:

二、ppt 自带smart art

        优点:直接带文本,不用手动添加文本框

        缺点:图片顺序难以调整,默认是横排的

                解决方案:每三张图添加一次,调整边框大小进行竖排,顺序可控,速度比较快

蛇行图片半透明文本

操作顺序:添加图片,ctrl a 全选,图片格式→图片版式

三、对齐然后添加文本框

添加图片右击选择→大小和位置,勾选锁定横纵比,调节图片至合适大小,按顺序排列好图片

按顺序拖开排列图片,如需改变横纵比会比较麻烦,需要一个一个改

对齐:

选中第一行的图片,对齐(顶端对齐、横向分布)

选中第一列的图片,左对齐、纵向分布

选中第二、三、四...列,左对齐、纵向分布

最后依次添加文本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值