Created
October 26, 2020 13:10
-
-
Save clint74/98b46e63a52375146985dd9b94937233 to your computer and use it in GitHub Desktop.
Resize images using ImageMagick and supbprocess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# dependencies ImageMagick | |
# reduce files > 1024MB | |
import os | |
from pathlib import Path | |
import subprocess | |
import time | |
for c in CadImages.objects.filter(somefilter='lslslsl'): | |
ext = c.my_image_field.path.split('/')[-1].split('.')[-1] | |
if ext.lower() == 'pdf': | |
# exclude PDF | |
continue | |
if Path(c.my_image_field.path).exists(): | |
# check size | |
stats = os.stat(c.my_image_field.path) | |
if stats.st_size >= 1024000: | |
print(c.my_image_field.url) | |
print('Size file WAS', (stats.st_size // 1024000), 'GB') | |
comando = f'convert {c.my_image_field.path} -resize 30% {c.my_image_field.path}' | |
subprocess.call(comando, | |
shell=True) | |
time.sleep(0.5) | |
stats = os.stat(c.my_image_field.path) | |
print('Size file is', (stats.st_size // 1024), ' M bytes') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment