This tool aims to process a collection of base64-encoded images provided in a CSV file, compress each image by adjusting its JPEG quality, and save the compressed images alongside their IDs back to a new CSV file.
The solution involves the following steps:
- Reading CSV Input: The input
input_images.csv
contains two columns:id
for image identification andimage
for the base64-encoded image data. - Decoding Base64: Each base64-encoded string is decoded to retrieve the actual image data.
- Image Compression: The image is compressed by adjusting its JPEG quality.
- The JPEG quality factor used in this tool is configurable but set to 75 by default, providing a balance between image quality and compression.
- Encoding to Base64: The compressed image is re-encoded to a base64 string.
- Writing CSV Output: The ID and compressed base64-encoded image data are saved to
compressed_images.csv
.
-
Install Node.js: Ensure Node.js is installed on your machine. You can download it from nodejs.org.
-
Clone or Download This Repository: Obtain the project files to your local machine.
-
Install Dependencies: Navigate to the project directory in your terminal and run:
npm install csv-parser csv-writer jimp fs
or simply
npm install
-
Prepare Your CSV Input: Place your input CSV file named
input_images.csv
in the project directory. Ensure it follows the format withid
andimage
columns. -
Run the Script: Execute the Node.js script with the command:
node index.js
or
npm start
-
Check the Output: Upon completion, the script will generate a file named
compressed_images.csv
in the project directory, containing the IDs and the compressed image data.
This project relies on the following NPM packages:
csv-parser
: For reading the input CSV file.csv-writer
: For writing the output CSV file.jimp
: For image processing, including decoding, compressing, and encoding images.
- The current implementation primarily focuses on compressing images by converting them to JPEG and adjusting the JPEG quality factor. This approach introduces lossy compression and does not apply to non-JPEG images in the same way.
- For different image formats, such as PNG, different techniques (such as color depth reduction, metadata removal, or encoding optimization) might be applied to achieve compression. However, these techniques are not implemented in the current solution.
- Adjustments to the JPEG quality factor can be made within the script to balance between compression and quality as required for your specific needs.