import qrcode
from PIL import Image

def generate_qr(data, file_name="qr.png"):
    """
    生成二维码并保存为文件
    :param data: 要编码的数据
    :param file_name: 保存的文件名
    """
    qr = qrcode.QRCode( # type: ignore
        version=10,
        error_correction=qrcode.constants.ERROR_CORRECT_L, # type: ignore
        box_size=10,
        border=4,
    )
    qr.add_data(data)
    qr.make(fit=True)

    img = qr.make_image(fill_color="black", back_color="white")
    img.save(file_name)


with open("flag.txt","r") as f:
    text = f.read()
generate_qr(text,"qr.png")

def split_image_into_quarters(image_path):
    # 打开图像文件
    image = Image.open(image_path)

    # 获取图像的宽度和高度
    width, height = image.size

    # 计算每个区块的宽度和高度
    quarter_width = width // 2
    quarter_height = height // 2

    # 切割图像为四块
    top_left = image.crop((0, 0, quarter_width, quarter_height))
    top_right = image.crop((quarter_width, 0, width, quarter_height))
    bottom_left = image.crop((0, quarter_height, quarter_width, height))
    bottom_right = image.crop((quarter_width, quarter_height, width, height))

    return top_left, top_right, bottom_left, bottom_right

# 图像文件路径
image_path = "qr.png"

# 将图像切割成四块
top_left, top_right, bottom_left, bottom_right = split_image_into_quarters(image_path)

# 保存切割后的图像
top_left.save("3.jpg")
top_right.save("2.jpg")
bottom_left.save("4.jpg")
bottom_right.save("1.jpg")

