-
-
Save namieluss/a440061734075d929f0c6b9f6bd919c7 to your computer and use it in GitHub Desktop.
from PIL import Image, ImageOps | |
# open image | |
img = Image.open("test_image.jpg") | |
# border color | |
color = "green" | |
# top, right, bottom, left | |
border = (20, 10, 20, 10) | |
new_img = ImageOps.expand(img, border=border, fill=color) | |
# save new image | |
new_img.save("test_image_result.jpg") | |
# show new bordered image in preview | |
new_img.show() |
Thanks!
Thanks alot!
Thank you, this was fantastic! I can't believe every modern OS doesn't come with a system utility to do this. I ended up generalizing this slightly, and then making an alias.
Here's the generalized version:
"""Add a border to any image.
One required positional CLI arg, for target file.
Writes a new file, target_file_bordered.png.
"""
from pathlib import Path
import sys
from PIL import Image, ImageOps
border_color = "lightgray"
border = (3, 3, 3, 3)
try:
path = Path(sys.argv[1])
except IndexError:
print("You must supply a target image.")
sys.exit()
img = Image.open(path)
new_img = ImageOps.expand(img, border=border, fill=border_color)
new_filename = path.stem + "_bordered" + path.suffix
new_path_str = str(path).replace(path.name, new_filename)
new_path = Path(new_path_str)
new_img.save(new_path)
That's great, now you can run python3 add_border.py my_fantastic_image.py
, and it will save a new version of the image with a border.
However, it's much more useful to make an alias. I'm on macOS, so in .zprofile
, I added this line:
alias add_border='python3 /Users/username/projects/border_maker/add_border.py'
Now, from anywhere on my system, I can just use:
$ add_border my_fantastic_image.png
And it will save a file called my_fantastic_image_bordered.png
to the same location as my_fantastic_image.png
.
I get a int not scriptable error, I think it's coming from the border array. Are you converting that to a string somewhere? Thank you!
@blissdismissed Are you running the original code, or the code I shared recently? Can you share your full traceback?
Exactly what I was looking for!!!
Thanks a lot...