Skip to content

Instantly share code, notes, and snippets.

@clint74
Created October 26, 2020 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clint74/98b46e63a52375146985dd9b94937233 to your computer and use it in GitHub Desktop.
Save clint74/98b46e63a52375146985dd9b94937233 to your computer and use it in GitHub Desktop.
Resize images using ImageMagick and supbprocess
# 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