Package: python-resize-image
A python package to easily resize imagesThis package provides function for easily resizing images.DependenciesPillow 2.7++Python 2.7/3.4IntroductionThe following functions are supported:resize_cropcrop the image with a centered rectangle of the
specified size.resize_coverresize the image to fill the specified area, crop as
needed (same behavior asbackground-size:cover).resize_containresize the image so that it can fit in the
specified area, keeping the ratio and without crop (same behavior asbackground-size:contain).resize_heightresize the image to the specified height adjusting
width to keep the ratio the same.resize_widthresize the image to the specified width adjusting
height to keep the ratio the same.resize_thumbnailresize image while keeping the ratio trying its
best to match the specified size.InstallationInstall python-resize-image using pip:pip install python-resize-imageUsagepython-resize-image takes as first argument aPIL.Imageand thensizeargument which can be a single integer or tuple of two
integers.In the following example, we open an image,cropit and save as new
file:fromPILimportImagefromresizeimageimportresizeimagewithopen('test-image.jpeg','r+b')asf:withImage.open(f)asimage:cover=resizeimage.resize_cover(image,[200,100])cover.save('test-image-cover.jpeg',image.format)Before resizing, python-image-resize will check whether the operation
can be done. A resize is considered valid if it doesn’t require to
increase one of the dimension. To avoid the test addvalidate=Falseas argument:cover=resizeimage.resize_cover(image,[200,100],validate=False)You can also create a two step process validation then processing usingvalidatefunction attached to resized function which allows to test
the viability of the resize without doing it just after validation.validateis available using the dot.operator on every resize
function e.g.resize_cover.validate.The first exemple is rewritten in the following snippet to use this
feature:fromPILimportImagefromresizeimageimportresizeimagewithopen('test-image.jpeg','r+b')withImage.open()asimage:is_valid=resizeimage.resize_cover.validate(image,[200,100])# do something else...ifis_valid:withImage.open('test-image.jpeg')asimage:resizeimage.resize_cover.validate(image,[200,100],validate=False)cover=resizeimage.resize_cover(image,[200,100])cover.save('test-image-cover.jpeg',image.format)Mind the fact that it’s useless to validate the image twice, so we passvalidate=Falsetoresize_cover.validate.API Referenceresize_crop(image, size, validate=True)Crop the image with a centered rectangle of the specified size.Crop an image with a 200x200 cented square:fromPILimportImagefromresizeimageimportresizeimagefd_img=open('test-image.jpeg','r')img=Image.open(fd_img)img=resizeimage.resize_crop(img,[200,200])img.save('test-image-crop.jpeg',img.format)fd_img.close()resize_cover(image, size, validate=True, resample=Image.LANCZOS)Resize the image to fill the specified area, crop as needed. It’s the
same behavior as cssbackground-size:coverproperty.Resize and crop (from center) the image so that it covers a 200x100
rectangle.fromPILimportImagefromresizeimageimportresizeimagefd_img=open('test-image.jpeg','r')img=Image.open(fd_img)img=resizeimage.resize_cover(img,[200,100])img.save('test-image-cover.jpeg',img.format)fd_img.close()resize_contain(image, size, validate=True, resample=Image.LANCZOS,bg_color=(255,255, 255, 0))Resize the image so that it can fit in the specified area, keeping the
ratio and without crop. It’s the same behavior as cssbackground-size:containproperty. A white a background border is
created.Resize the image to minimum so that it is contained in a 200x100
rectangle is the ratio between source and destination image.fromPILimportImagefromresizeimageimportresizeimagefd_img=open('test-image.jpeg','r')img=Image.open(fd_img)img=resizeimage.resize_contain(img,[200,100])img.save('test-image-contain.jpeg',img.format)fd_img.close()resize_width(image, width, validate=True, resample=Image.LANCZOS)Resize the image to the specified height adjusting width to keep the
ratio the same.Resize the image to be 200px width:fromPILimportImagefromresizeimageimportresizeimagefd_img=open('test-image.jpeg','r')img=Image.open(fd_img)img=resizeimage.resize_width(img,200)img.save('test-image-width.jpeg',img.format)fd_img.close()resize_height(image, height, validate=True, resample=Image.LANCZOS)Resize the image to the specified width adjusting height to keep the
ratio the same.Resize the image to be 200px height:fromPILimportImagefromresizeimageimportresizeimagefd_img=open('test-image.jpeg','r')img=Image.open(fd_img)img=resizeimage.resize_height(img,200)img.save('test-image-height.jpeg',img.format)fd_img.close()resize_thumbnail(image, size, validate=True, resample=Image.LANCZOS)Resize image while keeping the ratio trying its best to match the
specified size.Resize the image to be contained in a 200px square:fromPILimportImagefromresizeimageimportresizeimagefd_img=open('test-image.jpeg','r')img=Image.open(fd_img)img=resizeimage.resize_thumbnail(img,[200,200])img.save('test-image-thumbnail.jpeg',img.format)fd_img.close()resize(method, *args, **kwargs)Resize Image with the specified method : ‘crop’, ‘cover’, ‘contain’,
‘width’, ‘height’ or ‘thumbnail’.fromPILimportImagefromresizeimageimportresizeimagefd_img=open('test-image.jpeg','r')img=Image.open(fd_img)img=resizeimage.resize('thumbnail',img,[200,200])img.save('test-image-thumbnail.jpeg',img.format)fd_img.close()Testspip install -r requirements.dev.txt
pip install -e .
python setup.py testContributepython-resize-image is hosted atgithub.com/VingtCinq/python-resize-image/.Before coding installpre-commitas git hook using the following
command:cp pre-commit .git/hooks/And install the hook and pylint:pip install git-pylint-commit-hook pylintIf you want to force a commit (you need a good reason to do that) usecommitwith the-noption e.g.git commit-n.SupportIf you are having issues, please let us know.LicenseThe project is licensed under the MIT License.