Hi,
I was looking for a code to resize my picts, first code was to resize pictures by reducing the height and width using a ratio of X. Then I thought about changing the code to set the threshold as a size limit… meaning recursively reducing size until I go below the 1MB size per picture ( Why 1 MB ? because of WordPress… π ).
Here is a code that means to reduce the size. It can be improved to parallelize the treatment of pics ( currently only serial treatment ), I will see if I have time for that…
EDIT: I added threading using library in python. So Now, images are processed in a parallel way.
How it works ?
First make sure you have “PIL” installed , else, just do :
sudo apt-get install python-imaging
just call the script and do not forget the “/” at the end of the directory where the sources are stored :
python image_resizer.py source_images/
just call the script and do not forget the “/” at the end of the directory where the sources are stored :
here is the link to download the code : https://rgcloudmouradgeneralpurp.blob.core.windows.net/exchangecontainermourad/image_resizer.py
here is an example of result.
DEBUG LOOP images_reducer ******************************** path_to_process_image IS source_images/SAFA.PNG Bytes
DEBUG LOOP images_reducer ******************************** buffer_for_image IS <open file 'source_images/SAFA.PNG', mode 'r' at 0x7f606ee0e270> Bytes
DEBUG LOOP images_reducer ******************************** TEMP_NAME IS temp_reducing_images/TEMP_SAFA.PNG Bytes
DEBUG LOOP images_reducer ******************************** Image_local_Current_Size IS 1333433 Bytes
DEBUG LOOP images_reducer ******************************** path_to_process_image IS source_images/SAFA.PNG Bytes
DEBUG LOOP images_reducer ******************************** REPLACED ORGINAL FILE 1333433 Bytes
DEBUG ******************************** IMAGE IS BIGGER THAN THRESHOLD 1048576 Bytes
DEBUG LOOP images_reducer ******************************** path_to_process_image IS source_images/SAFA.PNG Bytes
DEBUG LOOP images_reducer ******************************** buffer_for_image IS <open file 'source_images/SAFA.PNG', mode 'r' at 0x7f606ee0e270> Bytes
DEBUG LOOP images_reducer ******************************** TEMP_NAME IS temp_reducing_images/TEMP_SAFA.PNG Bytes
DEBUG LOOP images_reducer ******************************** Image_local_Current_Size IS 1196675 Bytes
DEBUG LOOP images_reducer ******************************** path_to_process_image IS source_images/SAFA.PNG Bytes
DEBUG LOOP images_reducer ******************************** REPLACED ORGINAL FILE 1196675 Bytes
DEBUG ******************************** IMAGE IS BIGGER THAN THRESHOLD 1048576 Bytes
DEBUG LOOP images_reducer ******************************** path_to_process_image IS source_images/SAFA.PNG Bytes
DEBUG LOOP images_reducer ******************************** buffer_for_image IS <open file 'source_images/SAFA.PNG', mode 'r' at 0x7f606ee0e270> Bytes
DEBUG LOOP images_reducer ******************************** TEMP_NAME IS temp_reducing_images/TEMP_SAFA.PNG Bytes
DEBUG LOOP images_reducer ******************************** Image_local_Current_Size IS 1052949 Bytes
DEBUG LOOP images_reducer ******************************** path_to_process_image IS source_images/SAFA.PNG Bytes
DEBUG LOOP images_reducer ******************************** REPLACED ORGINAL FILE 1052949 Bytes
DEBUG ******************************** IMAGE IS BIGGER THAN THRESHOLD 1048576 Bytes
DEBUG LOOP images_reducer ******************************** path_to_process_image IS source_images/SAFA.PNG Bytes
DEBUG LOOP images_reducer ******************************** buffer_for_image IS <open file 'source_images/SAFA.PNG', mode 'r' at 0x7f606ee0e270> Bytes
DEBUG LOOP images_reducer ******************************** TEMP_NAME IS temp_reducing_images/TEMP_SAFA.PNG Bytes
DEBUG LOOP images_reducer ******************************** Image_local_Current_Size IS 910881 Bytes
DEBUG LOOP images_reducer ******************************** path_to_process_image IS source_images/SAFA.PNG Bytes
DEBUG LOOP images_reducer ******************************** REPLACED ORGINAL FILE 910881 Bytes
DEBUG ******************************** IMAGE IS SMALLER THAN THRESHOLD 1048576 Bytes
DEBUG ******************************** strimage_to_process is SAFA.PNG
DEBUG ******************************** path_to_process_image is source_images/SAFA.PNG
DEBUG ******************************** INC_NEW_NAME is TREATED_
DEBUG ******************************** NEW_NAME is dest_images/TREATED_SAFA.PNG
DEBUG ******************************** image_written
IMAGE SAM_0305.JPG WILL BE REZIED TO FIT THRESHOLD
DEBUG ******************************** Image_Current_Size is 6086874 Bytes
DEBUG ******************************** initial_ratio_image_source_dest_size is 5
DEBUG ******************************** IMAGE IS BIGGER THAN THRESHOLD 1048576 Bytes
DEBUG LOOP images_reducer ******************************** path_to_process_image IS source_images/SAM_0305.JPG Bytes
DEBUG LOOP images_reducer ******************************** buffer_for_image IS <open file 'source_images/SAM_0305.JPG', mode 'r' at 0x7f606ee0e270> Bytes
DEBUG LOOP images_reducer ******************************** TEMP_NAME IS temp_reducing_images/TEMP_SAM_0305.JPG Bytes
DEBUG LOOP images_reducer ******************************** Image_local_Current_Size IS 1605512 Bytes
DEBUG LOOP images_reducer ******************************** path_to_process_image IS source_images/SAM_0305.JPG Bytes
DEBUG LOOP images_reducer ******************************** REPLACED ORGINAL FILE 1605512 Bytes
DEBUG ******************************** IMAGE IS BIGGER THAN THRESHOLD 1048576 Bytes
DEBUG LOOP images_reducer ******************************** path_to_process_image IS source_images/SAM_0305.JPG Bytes
DEBUG LOOP images_reducer ******************************** buffer_for_image IS <open file 'source_images/SAM_0305.JPG', mode 'r' at 0x7f606ee0e270> Bytes
DEBUG LOOP images_reducer ******************************** TEMP_NAME IS temp_reducing_images/TEMP_SAM_0305.JPG Bytes
DEBUG LOOP images_reducer ******************************** Image_local_Current_Size IS 1351356 Bytes
DEBUG LOOP images_reducer ******************************** path_to_process_image IS source_images/SAM_0305.JPG Bytes
DEBUG LOOP images_reducer ******************************** REPLACED ORGINAL FILE 1351356 Bytes
DEBUG ******************************** IMAGE IS BIGGER THAN THRESHOLD 1048576 Bytes
DEBUG LOOP images_reducer ******************************** path_to_process_image IS source_images/SAM_0305.JPG Bytes
DEBUG LOOP images_reducer ******************************** buffer_for_image IS <open file 'source_images/SAM_0305.JPG', mode 'r' at 0x7f606ee0e270> Bytes
DEBUG LOOP images_reducer ******************************** TEMP_NAME IS temp_reducing_images/TEMP_SAM_0305.JPG Bytes
DEBUG LOOP images_reducer ******************************** Image_local_Current_Size IS 1127460 Bytes
DEBUG LOOP images_reducer ******************************** path_to_process_image IS source_images/SAM_0305.JPG Bytes
DEBUG LOOP images_reducer ******************************** REPLACED ORGINAL FILE 1127460 Bytes
DEBUG ******************************** IMAGE IS BIGGER THAN THRESHOLD 1048576 Bytes
DEBUG LOOP images_reducer ******************************** path_to_process_image IS source_images/SAM_0305.JPG Bytes
DEBUG LOOP images_reducer ******************************** buffer_for_image IS <open file 'source_images/SAM_0305.JPG', mode 'r' at 0x7f606ee0e270> Bytes
DEBUG LOOP images_reducer ******************************** TEMP_NAME IS temp_reducing_images/TEMP_SAM_0305.JPG Bytes
DEBUG LOOP images_reducer ******************************** Image_local_Current_Size IS 928476 Bytes
DEBUG LOOP images_reducer ******************************** path_to_process_image IS source_images/SAM_0305.JPG Bytes
DEBUG LOOP images_reducer ******************************** REPLACED ORGINAL FILE 928476 Bytes
DEBUG ******************************** IMAGE IS SMALLER THAN THRESHOLD 1048576 Bytes
DEBUG ******************************** strimage_to_process is SAM_0305.JPG
DEBUG ******************************** path_to_process_image is source_images/SAM_0305.JPG
DEBUG ******************************** INC_NEW_NAME is TREATED_
DEBUG ******************************** NEW_NAME is dest_images/TREATED_SAM_0305.JPG
DEBUG ******************************** image_written
DEBUG ******************************** ALL IMAGES HAVE BEEN RESIZED... MOVING DATA BACK TO INITIAL FOLDER
DEBUG ******************************** ALL IMAGES HAVE BEEN MOVED BACK.... CLEANING...
…