macOS Catalina 10.15.6 python 3.8.2 pillow 7.2.0
The free conversion software on the net has a limited number of times, and I have some concerns about security, which makes me feel inconvenient. So I decided to make it myself in Python.
As a prerequisite, prepare folders called pngs and imgs at the same level as the python program. pngs folder ... Folder to put the conversion source image imgs folder ... Folder to put the converted image
By executing this program, png images in the pngs folder will be converted to jpg at once and saved in the imgs folder.
from PIL import Image
import os
import datetime
input_path = os.getcwd()
#Get the file in the specified directory
files = os.listdir(input_path + '/pngs')
#Get only PNG file from the obtained file
pngs = []
for f in files:
if f[-4:] == '.png':
pngs.append('pngs/' + f)
#Empty the variable
files = None
#Get current time for output file name
dt_now = datetime.datetime.now().strftime('%y%m%d_%H%M%S')
#Output file name or output path
output_path = 'imgs/img' + dt_now
#Convert png to jpg
for p in pngs:
input_img = Image.open(p)
output_img = input_img.convert('RGB')
output_img.save(output_path + ".jpg ",quality=40)
print("finished")
This code is written for my convenience only, so using it as it is may cause inconvenience. So please use it for reference only. If you have any questions or corrections, we will accept them.
How to use Python's image processing library Pillow (PIL) https://note.nkmk.me/python-pillow-basic/
Convert a large png file to a jpg file https://qiita.com/hirohuntexp/items/05b7a81323dff7bdca9f
Convert dates, times and strings with Python datetime (strftime, strptime) https://note.nkmk.me/python-datetime-usage/
Recommended Posts