From 5521490fb25fb381e55480039267d8f8bd5e1b5d Mon Sep 17 00:00:00 2001 From: kthoden Date: Thu, 27 Feb 2020 17:04:12 +0100 Subject: [PATCH] Resize images using PIL --- .../management/commands/publicationimport.py | 54 ++++++++++++++++--- 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/eoapublications/management/commands/publicationimport.py b/eoapublications/management/commands/publicationimport.py index 89a3308..f33bb93 100644 --- a/eoapublications/management/commands/publicationimport.py +++ b/eoapublications/management/commands/publicationimport.py @@ -16,6 +16,40 @@ class Command(BaseCommand): help = "Import a publication into the Django database." + def resize_image(self, source_image, dest_image, max_size, dimension): + """Resize an image, preserve ratio. + + Takes four arguments, filenames of source and destination + images, the maximal size and the dimension (width or height). + + https://stackoverflow.com/questions/273946/how-do-i-resize-an-image-using-pil-and-maintain-its-aspect-ratio + + """ + + image_object = Image.open(source_image) + + width, height = image_object.size + + if dimension == "height": + height_percent = (max_size/float(height)) + wsize = int((float(width)*float(height_percent))) + resized_image = image_object.resize((wsize, max_size), Image.ANTIALIAS) + dest_width, dest_height = wsize, max_size + elif dimension == "width": + width_percent = (max_size/float(width)) + hsize = int((float(height)*float(width_percent))) + resized_image = image_object.resize((max_size, hsize), Image.ANTIALIAS) + dest_width, dest_height = max_size, hsize + else: + print("You must either specify height or width as dimension. Exiting.") + sys.exit(0) + + image_object.save(dest_image) + + return dest_width, dest_height + # def resize_image ends here + + def process_as_string(self,xmlElement): """Create html text for elements""" @@ -241,14 +275,20 @@ def handle(self, **options): tmpImageObject = Image.open(str(settings.MEDIA_ROOT) + "/" + str(Newpublication.Coverbig)) Newpublication.Coverbigwidth = tmpImageObject.size[0] Newpublication.Coverbigheight = tmpImageObject.size[1] - shutil.copy(strDir + "/Cover.jpg", strMediaDir + "/Cover_medium.jpg") - strCommand = "/usr/bin/gm convert -resize 250x " + strMediaDir + "/Cover_medium.jpg " + strMediaDir + "/Cover_medium.jpg" - listArguments = shlex.split(strCommand) - subprocess.call(listArguments, shell=False) + + dest_width, dest_height = self.resize_image(f"{strDir}/Cover.jpg", f"{strMediaDir}/Cover_medium.jpg", 250, "height") Newpublication.Covermedium = serie_path + "/"+ publication_number + "/Cover_medium.jpg" - tmpImageObject = Image.open(str(settings.MEDIA_ROOT) + "/" + str(Newpublication.Covermedium)) - Newpublication.Covermediumwidth = tmpImageObject.size[0] - Newpublication.Covermediumheight = tmpImageObject.size[1] + Newpublication.Covermediumwidth = dest_width + Newpublication.Covermediumheight = dest_height + + # shutil.copy(strDir + "/Cover.jpg", strMediaDir + "/Cover_medium.jpg") + # strCommand = "/usr/bin/gm convert -resize 250x " + strMediaDir + "/Cover_medium.jpg " + strMediaDir + "/Cover_medium.jpg" + # listArguments = shlex.split(strCommand) + # subprocess.call(listArguments, shell=False) + # Newpublication.Covermedium = serie_path + "/"+ publication_number + "/Cover_medium.jpg" + # tmpImageObject = Image.open(str(settings.MEDIA_ROOT) + "/" + str(Newpublication.Covermedium)) + # Newpublication.Covermediumwidth = tmpImageObject.size[0] + # Newpublication.Covermediumheight = tmpImageObject.size[1] shutil.copy(strDir + "/Cover.jpg", strMediaDir + "/Cover_small.jpg") strCommand = "/usr/bin/gm convert -resize 140x " + strMediaDir + "/Cover_small.jpg " + strMediaDir + "/Cover_small.jpg" listArguments = shlex.split(strCommand)