Project | author | date | subject | keywords | lang | ||||
---|---|---|---|---|---|---|---|---|---|
Image Conversion to .jpg |
|
2022-07-03 |
Image Conversion to .jpg in Python |
|
en-US |
This is a simple Python script that converts images in a directory to .jpg format.
This script requires the following Python modules:
-PIL (Python Imaging Library)
-Image (Python Imaging Library)
-os (Operating System)
# Start by importing the image
from PIL import Image # module for image processing
import os # module for file system
#image-input-path
im = Image.open('images/in/image1.png')
print("The size of the image before conversion is: ", end = "")
print(os.path.getsize('images/in/image1.png'))
# ------------------------------------------------------------
# Display the image before editing ---
Image.open('images/in/image1.png')
# ------------------------------------
# Convert Image to .jpg -------------
rgb_im = im.convert('RGB')
# ------------------------------------
# Save the image --------------------
rgb_im.save('images/out/image1.jpg')
print("The size of the image after conversion is: ", end = "")
print(os.path.getsize('images/out/image1.jpg'))
# ------------------------------------
# Open the converted image ---------
Image.open('images/out/image1.jpg')
# ------------------------------------