Made it possible to convert PNG to JPG with Pillow of Python

environment

macOS Catalina 10.15.6 python 3.8.2 pillow 7.2.0

Motivation

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.

specification

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.

code

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")

At the end

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.

reference

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

Made it possible to convert PNG to JPG with Pillow of Python
Convert HEIC files to PNG files with Python
Convert PDF to image (JPEG / PNG) with Python
Convert svg file to png / ico with Python
Convert list to DataFrame with python
IPynb scoring system made with TA of Introduction to Programming (Python)
Convert the result of python optparse to dict and utilize it
Convert memo at once with Python 2to3
Convert psd file to png in Python
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
Convert FX 1-minute data to 5-minute data with Python
Convert PDF files to PNG files with GIMP
Convert Chinese numerals to Arabic numerals with Python
Sample to convert image to Wavelet with Python
Read CSV file with Python and convert it to DataFrame as it is
Convert DICOM to PNG with Ascending and Descending
Convert data with shape (number of data, 1) to (number of data,) with numpy.
Convert "number" of excel date to python datetime
Convert PDFs to images in bulk with Python
[Python] Convert from DICOM to PNG or CSV
Convert Windows epoch values to date with python
How to convert SVG to PDF and PNG [Python]
How to specify attributes with Mock of python
Convert the character code of the file with Python3
Convert strings to character-by-character list format with python
How to convert Web pages to PDF, PNG, JPG with VBA (Excel) (Selenium Basic)
How to convert / restore a string with [] in python
Procedure to load MNIST with python and output to png
A memo connected to HiveServer2 of EMR with python
Convert 202003 to 2020-03 with pandas
I made a program to convert images into ASCII art with Python and OpenCV
Change IP settings to ACL of conoha with python
[Chapter 5] Introduction to Python with 100 knocks of language processing
Convert the image in .zip to PDF with Python
How to convert JSON file to CSV file with Python Pandas
[Chapter 3] Introduction to Python with 100 knocks of language processing
PyInstaller memorandum Convert Python [.py] to [.exe] with 2 lines
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Chapter 4] Introduction to Python with 100 knocks of language processing
I made a server with Python socket and ssl and tried to access it from a browser
Return the image data with Flask of Python and draw it to the canvas element of HTML
I made a function to crop the image of python openCV, so please use it.
I tried to find the entropy of the image with python
I want to specify another version of Python with pyvenv
Is it possible to detect similar images only with ImageHash?
How to convert an array to a dictionary with Python [Application]
I made a package to filter time series with python
A collection of competitive pro techniques to solve with Python
Introduction to her made with Python ~ Tinder automation project ~ Episode 6
Tank game made with python About the behavior of tanks
Convert color space from RGB to CIELAB with PIL (Pillow)
Introduction to her made with Python ~ Tinder automation project ~ Episode 5
Convert images to sepia toning with PIL (Python Imaging Library)
Convert garbled scanned images to PDF with Pillow and PyPDF
How to convert Youtube to mp3 and download it super-safely [Python]
Try to automate the operation of network devices with Python
[I made it with Python] XML data batch output tool
[Raspi4; Introduction to Sound] Stable recording of sound input with python ♪
Convert video to black and white with ffmpeg + python + opencv
It's Halloween so I'll try to hide it with Python