from PIL import Image, ImageDraw # 定义图片大小和参数 floor_height = 30 # 每层楼的高度 floor_width = 100 # 每个单元的宽度 num_floors = 18 # 总楼层数 num_units = 2 # 单元数 brick_color = (150, 75, 0) # 砖的颜色 repair_color = (255, 0, 0) # 需要维修的颜色 # 计算图片大小 image_height = num_floors * floor_height image_width = num_units * floor_width # 创建图片对象 image = Image.new("RGB", (image_width, image_height), "white") draw = ImageDraw.Draw(image) # 绘制外墙砖 for unit in range(num_units): for floor in range(num_floors): x1 = unit * floor_width y1 = floor * floor_height x2 = (unit + 1) * floor_width y2 = (floor + 1) * floor_height # 绘制砖块 if (floor + unit) % 2 == 0: draw.rectangle([x1, y1, x2, y2], fill=brick_color) # 绘制需要维修的砖块 if floor < num_floors // 2: # 前一半楼层的砖块需要维修 draw.rectangle([x1, y1, x2, y2], fill=repair_color) # 保存图片 image.save("building.png")
时间: 2024-04-16 17:25:42 浏览: 12
这段代码使用Python的PIL库创建了一个楼房的图像。图像由多个层次组成,每个层次由两个单元组成。代码首先定义了楼层的高度、单元的宽度、总楼层数、单元数以及砖的颜色和需要维修的颜色。然后计算了图像的大小,并创建了一个空白的图像对象。接下来,使用循环遍历每个单元和楼层,并根据条件绘制砖块和需要维修的砖块。最后,保存了生成的图像为"building.png"文件。
你可以根据需要调整参数和颜色,并保存生成的图像。
相关问题
import tkinter as tk from PIL import ImageTk, Image # 定义窗口的大小和标题 WINDOW_WIDTH = 500 WINDOW_HEIGHT = 500 WINDOW_TITLE = "点击图片进入应用程序" # 定义图片的大小和路径 IMAGE_WIDTH = 500 IMAGE_HEIGHT = 500 IMAGE_PATH = "background.jpg" # 定义标签的字体和大小 LABEL_FONT = ("Helvetica", 16) root = tk.Tk() root.geometry(f"{WINDOW_WIDTH}x{WINDOW_HEIGHT}") root.title(WINDOW_TITLE) bg_image = Image.open(IMAGE_PATH) bg_image = bg_image.resize((IMAGE_WIDTH, IMAGE_HEIGHT), Image.ANTIALIAS) bg_image = ImageTk.PhotoImage(bg_image) bg_label = tk.Label(root, image=bg_image) bg_label.place(x=0, y=0, relwidth=1, relheight=1) label = tk.Label(root, text="点击图片进入应用程序", font=LABEL_FONT) label.place(relx=0.5, rely=0.5, anchor="center") def enter_app(event): label.destroy() run_app() def run_app(): # 在这里添加你的应用程序代码 # ... bg_label.bind("<Button-1>", enter_app) root.mainloop()使此程序跳到下个程序时自动关闭窗口
要使程序在跳转到下一个程序时自动关闭窗口,可以将root的quit()方法绑定到enter_app函数中。这样,在enter_app函数中销毁标签后,调用root.quit()方法即可立即关闭窗口。下面是修改后的代码示例:
```
import tkinter as tk
from PIL import ImageTk, Image
# 定义窗口的大小和标题
WINDOW_WIDTH = 500
WINDOW_HEIGHT = 500
WINDOW_TITLE = "点击图片进入应用程序"
# 定义图片的大小和路径
IMAGE_WIDTH = 500
IMAGE_HEIGHT = 500
IMAGE_PATH = "background.jpg"
# 定义标签的字体和大小
LABEL_FONT = ("Helvetica", 16)
root = tk.Tk()
root.geometry(f"{WINDOW_WIDTH}x{WINDOW_HEIGHT}")
root.title(WINDOW_TITLE)
bg_image = Image.open(IMAGE_PATH)
bg_image = bg_image.resize((IMAGE_WIDTH, IMAGE_HEIGHT), Image.ANTIALIAS)
bg_image = ImageTk.PhotoImage(bg_image)
bg_label = tk.Label(root, image=bg_image)
bg_label.place(x=0, y=0, relwidth=1, relheight=1)
label = tk.Label(root, text="点击图片进入应用程序", font=LABEL_FONT)
label.place(relx=0.5, rely=0.5, anchor="center")
def enter_app(event):
label.destroy()
run_app()
root.quit() # 关闭窗口
def run_app():
# 在这里添加你的应用程序代码
# ...
bg_label.bind("<Button-1>", enter_app)
root.mainloop()
```
更改这段代码,让它可以从文件夹中读取图片并进行批量操作from PIL import Image import os # 加载图像 image_path = "D:/pythonProject/image_dir/DL2D00001601.jpg" # 替换为你的图像文件路径 image = Image.open(image_path) # 定义切割参数 num_rows = 4 # 指定行数 num_cols = 8 # 指定列数 # 计算每个小图像的宽度和高度 width, height = image.size crop_width = width // num_cols crop_height = height // num_rows # 切割图像并保存小图像 for i in range(num_rows): for j in range(num_cols): # 计算切割框的左上角和右下角坐标 left = j * crop_width upper = i * crop_height right = left + crop_width lower = upper + crop_height # 切割图像 crop = image.crop((left, upper, right, lower)) # 创建保存路径 save_dir = 'crop_dir' if not os.path.exists(save_dir): os.makedirs(save_dir) # 保存文件到指定路径下 crop.save(f"{save_dir}/crop_{i}_{j}.jpg")
可以使用os模块中的listdir()函数获取文件夹中的所有文件名,然后对每个文件进行操作。修改后的代码如下:
from PIL import Image
import os
# 加载图像
image_dir = "D:/pythonProject/image_dir"
for filename in os.listdir(image_dir):
image_path = os.path.join(image_dir, filename)
image = Image.open(image_path)
# 定义切割参数
num_rows = 4 # 指定行数
num_cols = 8 # 指定列数
# 计算每个小图像的宽度和高度
width, height = image.size
crop_width = width // num_cols
crop_height = height // num_rows
# 切割图像并保存小图像
for i in range(num_rows):
for j in range(num_cols):
# 计算切割框的左上角和右下角坐标
left = j * crop_width
upper = i * crop_height
right = left + crop_width
lower = upper + crop_height
# 切割图像
crop = image.crop((left, upper, right, lower))
# 创建保存路径
save_dir = 'crop_dir'
if not os.path.exists(save_dir):
os.makedirs(save_dir)
# 获取文件名和扩展名
name, ext = os.path.splitext(filename)
# 保存文件到指定路径下
crop.save(f"{save_dir}/{name}_crop_{i}_{j}{ext}")
这段代码会批量读取指定文件夹下的所有图片,对每个图片进行切割,并将切割后的小图像保存到指定的文件夹中。