LoginSignup
10
13

More than 5 years have passed since last update.

容量の大きいpngファイルを、jpgファイルに変換する

Last updated at Posted at 2017-12-11

はじめに

pngファイルというのは容量が結構大きいので、そのまま画像を用いるのであればjpgに変換したほうが軽くなって便利です。今回はその時に使用したコードを記載します。

PILでjpg変換

今回はpythonの画像処理ライブラリであるPILを用いましょう。以下のようなスクリプトでjpg変換することができます。また、qualityの値に応じて画像の精密度が変わるようです。

from PIL import Image
import sys
import os
import re

input_path = <input元フォルダ>
output_path = <output先フォルダ>
files = os.listdir(input_path)
count = 1

for file in files:
    if file[-4:] == ".png":
        input_im = Image.open(input_path + str(count) + ".png")
        rgb_im = input_im.convert('RGB')
        rgb_im.save(output_path + str(count) + ".jpg",quality=30)
        count = count + 1
        print("transaction finished" + str(count))

10
13
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
13