向图像添加透明填充
Add transparent padding to image
如何为特定目录中的所有图像添加透明填充?
例如,我有分辨率为 100x100px 的图像并想添加:
顶部 20px,
10px 对,
底部 40px,
还剩 20 像素。
这段代码对我有用:
for imagePath in os.listdir(inPath):
# imagePath contains name of the image
inputPath = os.path.join(inPath, imagePath)
# inputPath contains the full directory name
img = Image.open(inputPath)
# fullOutPath contains the path of the output
fullOutPath = os.path.join(outPath, imagePath)
# add padding
img = ImageOps.expand(img, border=(10,10,10,10), fill=(0,0,0,0) )
# image that needs to be generated
img.save(fullOutPath)
print(fullOutPath)
如何为特定目录中的所有图像添加透明填充?
例如,我有分辨率为 100x100px 的图像并想添加: 顶部 20px, 10px 对, 底部 40px, 还剩 20 像素。
这段代码对我有用:
for imagePath in os.listdir(inPath):
# imagePath contains name of the image
inputPath = os.path.join(inPath, imagePath)
# inputPath contains the full directory name
img = Image.open(inputPath)
# fullOutPath contains the path of the output
fullOutPath = os.path.join(outPath, imagePath)
# add padding
img = ImageOps.expand(img, border=(10,10,10,10), fill=(0,0,0,0) )
# image that needs to be generated
img.save(fullOutPath)
print(fullOutPath)