2
0
Fork 0
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.8 KiB
Python

from PIL import Image
import os
def func():
imgNew = Image.new("RGBA", (9*16, 10*16))
dir = os.getcwd()
for file in os.listdir(dir):
if file.endswith("png") and not file.startswith("output"):
img = Image.open(dir+"/"+file)
# 保证居中
offset = 0
midImg = img.size[0]//2
mid = 0
colorCountPre = 0
for j in reversed(range(img.size[1])):
colorCount = 0
first, last = 0, 0
for i in range(img.size[0]):
color = img.getpixel((i, j))
if color[3] != 0:
if colorCount == 0:
first = i
last = i
colorCount += 1
if colorCountPre == 0:
colorCountPre = colorCount
else:
if colorCount >= colorCountPre:
mid = (first+last)//2
offset = midImg - mid
break
# 调整size
a = img.size[0]-mid
half = (a if offset > 0 else img.size[0]-a)
imgSize = list(img.size)
imgSize[0] = half*2
if imgSize[0] % 2 == 1:
imgSize[0] += 1
if imgSize[1] % 2 == 1:
imgSize[1] += 1
move = half - a + offset*2
imgNew = Image.new("RGBA", tuple(imgSize))
for i in range(img.size[0]):
for j in range(img.size[1]):
color = img.getpixel((i, j))
imgNew.putpixel((i+move, j), color)
print(file,offset)
imgNew.save(file, "PNG")
if __name__ == "__main__":
func()