Author: Dan G. Poku (dan.gyinaye@gmail.com)
Date: April 15, 2023
This script compresses (at 50%) all image files in a folder and its subfolders, saving them in a separate folder. It avoids recompressing previously compressed files.
- Python 3.x
- OpenCV (cv2) library
-
Place the script in the root folder containing the images you want to compress.
-
Ensure the
destination_path.py
file is present in the same directory as the script. -
Open a terminal or command prompt and navigate to the root folder.
-
Run the following command:
python img_compress.py
The script will recursively compress all image files (.jpg
and .png
) in the root folder and its subfolders, saving the compressed images in a "compressed_images" folder within each folder containing the original images.
- Once the script completes, the compressed images will be available in their respective "compressed_images" folders.
The script logs its progress and any errors encountered during execution to the image_compression.log
file.
The script provides the following functions:
Returns the folder path of the Python script.
Creates a new folder within the given folder path to store the compressed images.
Retrieves a list of previously compressed file names in the destination folder to avoid recompression.
Compresses an image and saves it in another folder within the same folder.
Compresses all image files in the root folder and its subfolders.
Note: The script uses OpenCV's cv2
library to read and compress images.
This script compresses all .mp4
files in a folder and its subfolders, saving them in a separate folder. It avoids recompressing previously compressed files.
ffmpeg
andffprobe
should be installed on your system. Ensure they are accessible from the command line.
-
Place the
videocomp.py
script in the root folder containing the videos you want to compress. -
Open a terminal or command prompt and navigate to the root folder.
-
Run the following command:
python vid_compress.py
The script will recursively compress all .mp4
files in the root folder and its subfolders, saving the compressed files in a "compressed" folder within each folder containing the original videos.
- Once the script completes, the compressed videos will be available in their respective "compressed" folders.
The script provides the following functions:
Returns a dictionary containing video information using ffprobe
.
Returns the current bitrate of a video file.
Checks if a video file has already been compressed.
Compresses a video file and returns the path of the compressed file.
Recursively compresses all .mp4
files in a root folder and saves them to a "compressed" folder within each folder containing the original videos.
Note: The script applies different compression settings based on the bitrate of the video file.
For any questions or feedback, please contact Dan at dan.gyinaye@gmail.com.