mirror of
https://github.com/hastagAB/Awesome-Python-Scripts.git
synced 2024-11-23 20:11:07 +00:00
Merge pull request #8 from bureken/master
Add script for making images circular.
This commit is contained in:
commit
dbb9917e42
13
Image-Circulator/README.md
Normal file
13
Image-Circulator/README.md
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
# Image Circulator
|
||||||
|
Tiny Python3 script to make your images circular.
|
||||||
|
|
||||||
|
## Dependencies
|
||||||
|
It requires `python3` and `pillow`.
|
||||||
|
To install `pillow`, you need `pip3` or python3.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
To run, from the command line type:
|
||||||
|
|
||||||
|
`python3 image_circulator.py -i INPUT_FILE_PATH -o OUTPUT_FILE_PATH -d DIAMETER_IN_PIXELS`
|
||||||
|
|
||||||
|
Or make the script executable.
|
41
Image-Circulator/image_circulator.py
Normal file
41
Image-Circulator/image_circulator.py
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from PIL import Image, ImageOps, ImageDraw
|
||||||
|
from argparse import ArgumentParser
|
||||||
|
|
||||||
|
|
||||||
|
parser = ArgumentParser()
|
||||||
|
inputfile = parser.add_argument('-i', '--inputfile', help='input file path')
|
||||||
|
outputfile = parser.add_argument('-o', '--outputfile', help='output file path')
|
||||||
|
diameter = parser.add_argument('-d', '--diameter', help='output file diameter')
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
print('Input file is '+ str(args.inputfile))
|
||||||
|
print('Output file is '+ str(args.outputfile))
|
||||||
|
print('Image diameter will be '+ str(args.diameter))
|
||||||
|
|
||||||
|
im = Image.open(args.inputfile)
|
||||||
|
|
||||||
|
width, height = im.size
|
||||||
|
|
||||||
|
left = (width - int(args.diameter))/2
|
||||||
|
top = (height - int(args.diameter))/2
|
||||||
|
right = (width + int(args.diameter))/2
|
||||||
|
bottom = (height + int(args.diameter))/2
|
||||||
|
|
||||||
|
im = im.crop((left, top, right, bottom));
|
||||||
|
|
||||||
|
bigsize = (im.size[0] * 3, im.size[1] * 3)
|
||||||
|
mask = Image.new('L', bigsize, 0)
|
||||||
|
draw = ImageDraw.Draw(mask)
|
||||||
|
draw.ellipse((0, 0) + bigsize, fill=255)
|
||||||
|
mask = mask.resize(im.size, Image.ANTIALIAS)
|
||||||
|
im.putalpha(mask)
|
||||||
|
|
||||||
|
output = ImageOps.fit(im, mask.size, centering=(0.5, 0.5))
|
||||||
|
output.putalpha(mask)
|
||||||
|
output.save(args.outputfile)
|
||||||
|
|
||||||
|
print('Done!')
|
Loading…
Reference in New Issue
Block a user