Skip to content

Commit

Permalink
Resize images using PIL
Browse files Browse the repository at this point in the history
  • Loading branch information
kthoden committed Feb 27, 2020
1 parent f51d611 commit 5521490
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions eoapublications/management/commands/publicationimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5521490

Please sign in to comment.