Python Imaging Library
Python Imaging Library is a free and open-source additional library for the Python programming language that adds support for opening, manipulating, and saving many different image file formats. It is available for Windows, Mac OS X and Linux. The latest version of PIL is 1.1.7, was released in September 2009 and supports Python 1.5.2–2.7.[3] Development of the original project, known as PIL, was discontinued in 2011.[2] Subsequently, a successor project named Pillow forked the PIL repository and added Python 3.x support.[5] This fork has been adopted as a replacement for the original PIL in Linux distributions including Debian[6] and Ubuntu (since 13.04).[7] CapabilitiesPIL offers several standard procedures for image manipulation. These include:
File formatsSupported file formats include PPM, PNG, JPEG, GIF, TIFF, and BMP. PIL is extensible, allowing users to create custom decoders for any file format.[11] Programming examplesimport os
from PIL import Image
def convert_jpegs_to_pngs(folder_path):
# Checks if the provided path is a folder
if not os.path.isdir(folder_path):
print(f"Error: {folder_path} is not a valid folder.")
return
# Iterates over all files in the folder
for filename in os.listdir(folder_path):
# Checks if the file has a .jpg or .jpeg extension
if filename.lower().endswith(".jpg") or filename.lower().endswith(".jpeg"):
# Full path of the file
jpeg_path = os.path.join(folder_path, filename)
# Path for the converted file
png_path = os.path.join(folder_path, os.path.splitext(filename)[0] + ".png")
try:
# Opens the JPEG image
with Image.open(jpeg_path) as img:
# Converts and saves as PNG
img.save(png_path, "PNG")
print(f"Converted {jpeg_path} to {png_path}")
except Exception as e:
print(f"Error converting {jpeg_path}: {e}")
References
External links |
Portal di Ensiklopedia Dunia