此 python 程式工具, 可以將目錄XX 中的,png file 轉成 .jpg file. 同時將影像全部縮放到固定大小

# PNG file 可能是RGBA format, 所以須要做轉換 到RGB.

# resize 須要二個參數 一個是new size <== output size, 另一個是內插的方法

import os

def png2jpg():
  from pathlib import Path
  from PIL import Image

  inPath = Path("d:/XX")
  inFiles = inPath.glob("**/*.png")

  outPath = Path("d:/XX/jpg")  # 請在xx 中, 先建立好 Jpg 目錄

#  if not os.path.exists(outPath):
#       os.makedirs(outPath)  


  for f in inFiles:
    outFile = outPath / Path(f.stem + ".jpg")
    im = Image.open(f)
    im =im.convert("RGB")
#    newsize = (960, 540)
    newsize = (608, 608)
    # im.resize(size, resample=0) PIL.Image.NEAREST (use nearest neighbour), PIL.Image.BILINEAR (linear interpolation), PIL.Image.BICUBIC (cubic spline interpolation), or PIL.Image.LANCZOS 
    im1 = im.resize(newsize)
    im1.save(outFile)

 

png2jpg()

文章標籤
全站熱搜
創作者介紹
創作者 cianfen 的頭像
cianfen

cianfen的部落格

cianfen 發表在 痞客邦 留言(0) 人氣(19)