拼接指定的图像
from PIL import Image
import os
def GetFileFromThisRootDir(dir, ext=None):
allfiles = []
needExtFilter = (ext != None)
for root, dirs, files in os.walk(dir):
for filespath in files:
filepath = os.path.join(root, filespath)
extension = os.path.splitext(filepath)[1][1:]
if needExtFilter and extension in ext:
allfiles.append(filepath)
elif not needExtFilter:
allfiles.append(filepath)
return allfiles
# im = Image.open("TxSc_Bio_Forest_Tree_04_d.tga")
# im3 = im.crop(((im.width/2, 0), (3*im.width/4,0), (im.width/2, 1), (3*im.width/4,1)))
# im3.save("1.tga")
def crop_tex(path):
with Image.open(path) as im:
# The crop method from the Image module takes four coordinates as input.
# The right can also be represented as (left+width)
# and lower can be represented as (upper+height).
(left, upper, right, lower) = (im.width/2, 0, im.width * 3/4, im.height)
# Here the image "im" is cropped and assigned to new variable im_crop
im_crop = im.crop((left, upper, right, lower))
# im_crop.save(path.split('.')[1].replace('\\','') + "_crop.tga")
return im_crop
allfile = GetFileFromThisRootDir(".", "tga")
# for each in allfile:
# crop_tex(each)
im_combine = Image.new('RGBA', (2048, 2048))
im0 = crop_tex(allfile[0])
im1 = crop_tex(allfile[1])
im2 = crop_tex(allfile[2])
im3 = crop_tex(allfile[3])
im_combine.paste(im0, (0, 0, im0.width, im0.height))
im_combine.paste(im1, (im1.width , 0, im1.width * 2, im1.height))
im_combine.paste(im2, (im2.width * 2, 0, im2.width * 3, im2.height))
im_combine.paste(im3, (im3.width * 3, 0, im3.width * 4, im3.height))
im_combine.save(allfile[0].split('.')[1].replace('\\','') + "_crop.tga")
混合图像Alpha (类似于PS的新建图层)
from PIL import Image
import os
def GetFileFromThisRootDir(dir, ext=None):
allfiles = []
needExtFilter = (ext != None)
for root, dirs, files in os.walk(dir):
for filespath in files:
filepath = os.path.join(root, filespath)
extension = os.path.splitext(filepath)[1][1:]
if needExtFilter and extension in ext:
allfiles.append(filepath)
elif not needExtFilter:
allfiles.append(filepath)
return allfiles
def crop_tex(path):
with Image.open(path) as im:
(left, upper, right, lower) = (0, 0, im.width, im.height)
# Here the image "im" is cropped and assigned to new variable im_crop
im_crop = im.crop((left, upper, right, lower))
return im_crop
allfile = GetFileFromThisRootDir(".", "tga")
for each in allfile:
print(each)
im_combine = Image.new('RGBA', (2048, 2048))
im0 = crop_tex(allfile[0])
im1 = crop_tex(allfile[1])
im0 = Image.composite(im1, im0, im1)
im0.save(allfile[0].split('.')[1].replace('\\','') + "_crop_final.tga")
偏移图像
from PIL import Image
import os
def GetFileFromThisRootDir(dir, ext=None):
allfiles = []
needExtFilter = (ext != None)
for root, dirs, files in os.walk(dir):
for filespath in files:
filepath = os.path.join(root, filespath)
extension = os.path.splitext(filepath)[1][1:]
if needExtFilter and extension in ext:
allfiles.append(filepath)
elif not needExtFilter:
allfiles.append(filepath)
return allfiles
# im = Image.open("TxSc_Bio_Forest_Tree_04_d.tga")
# im3 = im.crop(((im.width/2, 0), (3*im.width/4,0), (im.width/2, 1), (3*im.width/4,1)))
# im3.save("1.tga")
def crop_tex(path):
with Image.open(path) as im:
# The crop method from the Image module takes four coordinates as input.
# The right can also be represented as (left+width)
# and lower can be represented as (upper+height).
(left, upper, right, lower) = (im.width/2, 0, im.width * 5/8, im.height)
# Here the image "im" is cropped and assigned to new variable im_crop
im_crop = im.crop((left, upper, right, lower))
# im_crop.save(path.split('.')[1].replace('\\','') + "_crop.tga")
return im_crop
allfile = GetFileFromThisRootDir(".", "tga")
for each in allfile:
print(each)
im_combine = Image.new('RGBA', (2048, 2048))
im0 = crop_tex(allfile[0])
im1 = crop_tex(allfile[1])
im2 = crop_tex(allfile[2])
im3 = crop_tex(allfile[3])
# im_black = Image.new('RGBA', (2048, 2048), (255, 255, 255))
width = int(2048 / 4)
offset = im0.width
left = 0 + offset
im_combine.paste(im0, (left, 0, left + im0.width, im0.height))
left = left + offset + im0.width
im_combine.paste(im1, (left, 0, left + im0.width, im1.height))
left = left + offset + im0.width
im_combine.paste(im2, (left, 0, left + im0.width, im2.height))
left = left + offset + im0.width
im_combine.paste(im3, (left, 0, left + im0.width, im3.height))
im_combine.save(allfile[0].split('.')[1].replace('\\','') + "_crop_n.tga")
合并操作
三种操作合起来
from PIL import Image
import os
# 需要修改diffuse的文件名为1 2 3 4
def GetFileFromThisRootDir(dir, ext=None):
allfiles = []
needExtFilter = (ext != None)
for root, dirs, files in os.walk(dir):
for filespath in files:
filepath = os.path.join(root, filespath)
extension = os.path.splitext(filepath)[1][1:]
if needExtFilter and extension in ext:
allfiles.append(filepath)
elif not needExtFilter:
allfiles.append(filepath)
return allfiles
def crop_tex(path):
with Image.open(path) as im:
# The crop method from the Image module takes four coordinates as input.
# The right can also be represented as (left+width)
# and lower can be represented as (upper+height).
(left, upper, right, lower) = (im.width/2, 0, im.width * 5/8, im.height)
# Here the image "im" is cropped and assigned to new variable im_crop
im_crop = im.crop((left, upper, right, lower))
# im_crop.save(path.split('.')[1].replace('\\','') + "_crop.tga")
return im_crop
allfile = GetFileFromThisRootDir(".", "tga")
for each in allfile:
print(each)
# diffuse
im_combine1 = Image.new('RGBA', (2048, 2048))
im0 = crop_tex(allfile[0])
im1 = crop_tex(allfile[1])
im2 = crop_tex(allfile[2])
im3 = crop_tex(allfile[3])
width = int(2048 / 4)
offset = im0.width
left = 0
im_combine1.paste(im0, (left, 0, left + im0.width, im0.height))
left = left + offset + im0.width
im_combine1.paste(im1, (left, 0, left + im0.width, im1.height))
left = left + offset + im0.width
im_combine1.paste(im2, (left, 0, left + im0.width, im2.height))
left = left + offset + im0.width
im_combine1.paste(im3, (left, 0, left + im0.width, im3.height))
# normal
im_combine2 = Image.new('RGBA', (2048, 2048))
im0 = crop_tex(allfile[4])
im1 = crop_tex(allfile[5])
im2 = crop_tex(allfile[6])
im3 = crop_tex(allfile[7])
left = 0 + offset
im_combine2.paste(im0, (left, 0, left + im0.width, im0.height))
left = left + offset + im0.width
im_combine2.paste(im1, (left, 0, left + im0.width, im1.height))
left = left + offset + im0.width
im_combine2.paste(im2, (left, 0, left + im0.width, im2.height))
left = left + offset + im0.width
im_combine2.paste(im3, (left, 0, left + im0.width, im3.height))
# blend
im_combine1 = Image.composite(im_combine1, im_combine2, im_combine1)
im_combine1.save(allfile[0].split('.')[1].replace('\\','') + "_crop_final.tga")