Created
March 16, 2020 05:36
-
-
Save namieluss/a440061734075d929f0c6b9f6bd919c7 to your computer and use it in GitHub Desktop.
Add Borders to Images using Python Pillow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
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?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
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:Now, from anywhere on my system, I can just use:
And it will save a file called
my_fantastic_image_bordered.png
to the same location asmy_fantastic_image.png
.