首页 > 解决方案 > 保存时 Python PIL 边框为 16x16 图像

问题描述

编码

该代码采用 16x16 像素图像的图块并将它们转换为图块集以在我的游戏中使用。为了测试这些图像的纹理是纯色的,但是程序的输出为每个图块添加了一个边框。

这发生在输出 png 和使用游戏中的 tileset 时。

import os, os.path
from PIL import Image

RES=16
setW=input("tileset width:")
#for ease of access defaults to 16 on ENTER
if setW=='':
    setW=16
else:
    setW=int(setW)

DIR='tSet/'
files = os.listdir(DIR)

texD={}
numTiles=len(files)
#Determines the size of the tileset image
if(numTiles%setW!=0):
    #if an extra row required for spare tiles
    tileSize=('RGB',(RES*setW,RES*(1+(numTiles//setW))), (250,250,250))
else:
    #if the entire area of the tileset is used
    tileSize=('RGB',(RES*setW,RES*((numTiles//setW))), (250,250,250))
tileset= Image.new(tileSize[0],tileSize[1],tileSize[2])
for i in range(0,numTiles):
    texture = files[i]
    #opens each texture as item of dictionary texD
    texD[texture]=Image.open(DIR+texture)
    #coordinate location for the tile
    tileLoc=(RES*(i%setW),RES*(i//setW))
    tileset.paste(texD[texture],(tileLoc))
    
    
tileset.save("tileset.png")
#opens the tileset in photos
tileset.show()

我需要什么帮助

有没有办法使生成的图像更清晰或更接近原始图像。

我试过的

到目前为止,我已将图块集的输出更改为 png 而不是 jpeg,但这不起作用。

图片

图像输出 3x3

图像输出单瓦

在游戏中使用时的 Tileset

标签: pythonpython-imaging-library

解决方案


推荐阅读