Python 结合100+;图像在一起

Python 结合100+;图像在一起,python,python-imaging-library,python-3.4,pillow,Python,Python Imaging Library,Python 3.4,Pillow,蟒蛇3.4 皮尔 我将100+16x16个瓷砖组合在一起,为测试应用程序随机生成地形,并保存为.png文件以供存储 我在测试时担心的是,我会收到以下完成时间: 100x100贴图(1600x1600像素)=1.16分钟 200x200贴图(3200x3200像素)=2.43分钟 300x300贴图(4800x4800像素)=5.27分钟 500x500贴图(8000x8000像素)=13.14分钟 对于移动/桌面应用程序,这太长。创建这些地图是否有更好的方法 注意:我不打算使用16x16瓷砖

蟒蛇3.4 皮尔

我将100+16x16个瓷砖组合在一起,为测试应用程序随机生成地形,并保存为.png文件以供存储

我在测试时担心的是,我会收到以下完成时间:

  • 100x100贴图(1600x1600像素)=1.16分钟
  • 200x200贴图(3200x3200像素)=2.43分钟
  • 300x300贴图(4800x4800像素)=5.27分钟
  • 500x500贴图(8000x8000像素)=13.14分钟
对于移动/桌面应用程序,这太长。创建这些地图是否有更好的方法

注意:我不打算使用16x16瓷砖以外的任何东西。不过,我对其他Python模块持开放态度

from datetime import datetime
from os import listdir, chdir
from random import randint
from time import strftime

from PIL import Image

x_size = 4800
y_size = 4800

x_tile_size = 16
y_tile_size = 16

x_tile_amount = int(x_size/x_tile_size)
y_tile_amount = int(y_size/y_tile_size)

new_im = Image.new('RGB', (x_size, y_size))

file_path = './assets/terrain/plains/'
print("Generating map.")
start_time = strftime("%H:%M:%S")
for i in range(x_tile_amount):
    for j in range(y_tile_amount):

    dice_roll = randint(1,7)

    if dice_roll == 1:
        terrain_file_path = 'grass_1.png'                        
    elif dice_roll == 2:
        terrain_file_path = 'grass_2.png'
    elif dice_roll == 3:
        terrain_file_path = 'grass_3.png'                            
    elif dice_roll == 4:
        terrain_file_path = 'grass_4.png'
    elif dice_roll == 5:
        terrain_file_path = 'white_flower.png'
    elif dice_roll == 6:
        terrain_file_path = 'red_flower.png'
    elif dice_roll == 7:
        terrain_file_path = 'blue_flower.png'

    old_im = Image.open(file_path + terrain_file_path)
    new_im.paste(old_im, (i*x_tile_size,j*y_tile_size))


end_time = strftime("%H:%M:%S")
t_format = "%H:%M:%S"
t_delta = datetime.strptime(end_time, t_format) - datetime.strptime(start_time, t_format)
print('Time to generate map = ' + str(t_delta))
new_im.save('pillow_test.png', 'PNG')

您正在打开新映像n^2次。这使得它非常慢

在执行for循环之前,应该预加载这些图像

grass_1 = Image.open(file_path + 'grass_1.png')
grass_2 = Image.open(file_path + 'grass_2.png')
...
for i in range(x_tile_amount):
    for j in range(y_tile_amount):
         dice_roll = random.randint(1, 7)
         if dice_roll == 1:
              etc.
你可以做的另一件事是将你的图片设置为dict

my_dict = {
    1: 'grass_1',
    2: 'grass_2',
    ...
}
那你就做吧

dice_roll = random.randint(1,7)
new_im.paste(my_dict[dice_roll], (i*x_tile_size,j*y_tile_size))
尝试一下,看看这是否对你的时间有帮助


主要的问题是,当您生成一个100x100的地图时,您将加载10000次图像。没有理由加载相同的7个图像文件10k次,因此您只需预加载它们并根据需要添加。

您正在为每个随机选择加载每个地形图像。最好先预加载所有图像,然后简单地使用
random.choice()
为您选择一个图像,如下所示:

from datetime import datetime
import random
from time import strftime

from PIL import Image

x_size = 4800
y_size = 4800

x_tile_size = 16
y_tile_size = 16

x_tile_amount = int(x_size/x_tile_size)
y_tile_amount = int(y_size/y_tile_size)

new_im = Image.new('RGB', (x_size, y_size))
file_path = './assets/terrain/plains/'
images = ['grass_1.png', 'grass_2.png', 'grass_3.png', 'grass_4.png', 'white_flower.png', 'red_flower.png', 'blue_flower.png']
terrain = [Image.open(os.path.join(file_path, image)) for image in images]

print("Generating map.")
start_time = strftime("%H:%M:%S")

for i in range(x_tile_amount):
    for j in range(y_tile_amount):
        old_im = random.choice(terrain)
        new_im.paste(old_im, (i*x_tile_size,j*y_tile_size))

end_time = strftime("%H:%M:%S")
t_format = "%H:%M:%S"
t_delta = datetime.strptime(end_time, t_format) - datetime.strptime(start_time, t_format)
print('Time to generate map = ' + str(t_delta))
new_im.save('pillow_test.png', 'PNG')    

这种方法应该会大大加快速度。

非常感谢,我现在正在经历不到10秒的时间来绘制500x500(8000x8000像素)的贴图。就像NinjaKitty的回答一样。非常感谢,我现在经历了不到10秒的时间来绘制500x500(8000x8000像素)的贴图。就像马丁的回答一样。