【Python】西游记取景地复刻图片合成

输入两个图片,进行合成,自动在1图标记1986,2图标记2024,图片对齐,保持没有空白,程序自动复位。

import tkinter as tk
from tkinter import filedialog, messagebox
from PIL import Image, ImageTk, ImageDraw, ImageFont

class ImageCombinerApp:
    def __init__(self, root):
        self.root = root
        self.root.title("图片合成器")
        
        # 初始化存储的图片路径
        self.first_image_path = None
        self.second_image_path = None
        
        # 创建界面组件
        self.create_widgets()
    
    def create_widgets(self):
        # 第一张图片上传按钮
        self.btn_upload_first = tk.Button(self.root, text="上传第一张图片", command=self.upload_first_image)
        self.btn_upload_first.grid(row=0, column=0, padx=10, pady=10)
        
        # 第二张图片上传按钮
        self.btn_upload_second = tk.Button(self.root, text="上传第二张图片", command=self.upload_second_image)
        self.btn_upload_second.grid(row=0, column=1, padx=10, pady=10)
        
        # 合成按钮
        self.btn_combine = tk.Button(self.root, text="合成图片", command=self.combine_images)
        self.btn_combine.grid(row=1, column=0, columnspan=2, padx=10, pady=10)
        
        # 显示图片区域
        self.image_panel = tk.Label(self.root)
        self.image_panel.grid(row=2, column=0, columnspan=2, padx=10, pady=10)

    def upload_first_image(self):
        file_path = filedialog.askopenfilename(title="选择第一张图片", filetypes=[("Image files", "*.jpg;*.jpeg;*.png")])
        if file_path:
            self.first_image_path = file_path
            messagebox.showinfo("图片上传", "第一张图片已成功上传。")
    
    def upload_second_image(self):
        file_path = filedialog.askopenfilename(title="选择第二张图片", filetypes=[("Image files", "*.jpg;*.jpeg;*.png")])
        if file_path:
            self.second_image_path = file_path
            messagebox.showinfo("图片上传", "第二张图片已成功上传。")
    
    def combine_images(self):
        if not self.first_image_path or not self.second_image_path:
            messagebox.showerror("错误", "请先上传两张图片。")
            return
        
        img1 = Image.open(self.first_image_path)
        img2 = Image.open(self.second_image_path)

        # 检查并缩放图像,如果图像的尺寸超过指定最大尺寸
        img1 = self.resize_image(img1)
        img2 = self.resize_image(img2)

        # 统一宽度,按比例调整高度
        img1, img2 = self.resize_images_to_same_width(img1, img2)

        # 添加年份文字到图片
        self.add_text_to_image(img1, "1986")
        self.add_text_to_image(img2, "2024")
        
        width1, height1 = img1.size
        width2, height2 = img2.size
        
        new_image = Image.new('RGB', (width1, height1 + height2), (255, 255, 255))
        new_image.paste(img1, (0, 0))
        new_image.paste(img2, (0, height1))
        
        output_path = filedialog.asksaveasfilename(defaultextension=".jpg", filetypes=[("JPEG files", "*.jpg"), ("PNG files", "*.png")])
        if output_path:
            new_image.save(output_path)
            messagebox.showinfo("图片合成", f"图片已成功合并并保存到 {output_path}")
            
            new_image.thumbnail((300, 300))
            tk_image = ImageTk.PhotoImage(new_image)
            self.image_panel.config(image=tk_image)
            self.image_panel.image = tk_image
        
        self.first_image_path = None
        self.second_image_path = None
        self.image_panel.config(image='')
        messagebox.showinfo("复位", "程序已复位,可重新上传图片。")

    def resize_image(self, img, max_size=(2000, 2000), max_ratio=0.8):
        # 检查图像大小是否超过最大尺寸
        width, height = img.size
        max_width, max_height = max_size
        # 缩放比例,确保图像不超出最大宽度和高度
        ratio = min(max_width / width, max_height / height, max_ratio)
        
        if ratio < 1:
            new_width = int(width * ratio)
            new_height = int(height * ratio)
            img = img.resize((new_width, new_height), Image.LANCZOS)
        return img

    def resize_images_to_same_width(self, img1, img2):
        # 获取两张图的宽度
        width1, height1 = img1.size
        width2, height2 = img2.size
        
        # 选择较小的宽度
        new_width = min(width1, width2)
        
        # 计算按比例缩放后的高度
        new_height1 = int(height1 * (new_width / width1))
        new_height2 = int(height2 * (new_width / width2))
        
        # 调整大小
        img1 = img1.resize((new_width, new_height1), Image.LANCZOS)
        img2 = img2.resize((new_width, new_height2), Image.LANCZOS)
        
        return img1, img2

    def add_text_to_image(self, image, text):
        draw = ImageDraw.Draw(image)
        
        # 获取图片宽度并计算字体大小
        image_width = image.size[0]
        font_size = int(image_width * 0.10)  # 字体大小为图片宽度的 10%
        
        # 设置自定义字体路径
        font_path = r"C:\Users\Lumix\AppData\Local\Microsoft\Windows\Fonts\LCD-BOLD-5.ttf"
        
        try:
            font = ImageFont.truetype(font_path, font_size)
        except IOError:
            font = ImageFont.load_default()
        
        # 设置文字位置、颜色等
        text_position = (10, 10)
        text_color = (255, 165, 0)  # 橙黄色
        stroke_color = (139, 0, 0)  # 深红色描边
        
        # 绘制描边(文字偏移)
        for offset in [-2, 0, 2]:
            draw.text((text_position[0] + offset, text_position[1] + offset), text, fill=stroke_color, font=font)
        
        # 绘制橙黄色文字
        draw.text(text_position, text, fill=text_color, font=font)

# 创建并运行应用
root = tk.Tk()
app = ImageCombinerApp(root)
root.mainloop()

获取西游记的对应图片,可以对小红书已经合成的图片进行裁切,由于我这边看到的很多图都是一比一组合的,因此可以很方便将图片分开:

import sys
import random
import string
from PIL import Image

def generate_random_filename():
    return ''.join(random.choices(string.digits, k=8)) + '.jpg'

def split_image(image_path):
    # 打开图像
    image = Image.open(image_path)
    width, height = image.size
    half_height = height // 2

    # 分割图像
    upper_half = image.crop((0, 0, width, half_height))
    lower_half = image.crop((0, half_height, width, height))

    # 生成随机文件名
    upper_filename = generate_random_filename()
    lower_filename = generate_random_filename()

    # 保存分割后的图像
    upper_half.save(upper_filename)
    lower_half.save(lower_filename)

    print(f"图像已成功分割并保存为 {upper_filename} 和 {lower_filename}")

if __name__ == "__main__":
    # 检查是否提供了图像路径
    if len(sys.argv) < 2:
        print("请将图像文件拖动到此脚本上运行。")
    else:
        # 获取图像路径
        image_path = sys.argv[1]
        split_image(image_path)

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注