{"id":3311,"date":"2026-09-02T00:30:47","date_gmt":"2026-09-01T16:30:47","guid":{"rendered":"http:\/\/www.kd-trip.com\/blog\/?p=3311"},"modified":"2026-09-02T00:30:47","modified_gmt":"2026-09-01T16:30:47","slug":"how-to-add-a-border-to-an-image-using-pillow-44aa-7b1e94","status":"publish","type":"post","link":"http:\/\/www.kd-trip.com\/blog\/2026\/09\/02\/how-to-add-a-border-to-an-image-using-pillow-44aa-7b1e94\/","title":{"rendered":"How to add a border to an image using Pillow?"},"content":{"rendered":"<p>Adding a border to an image can significantly enhance its visual appeal, making it stand out in various contexts, from personal photo editing to professional graphic design. As a dedicated Pillow supplier, I&#8217;ve witnessed firsthand how versatile and powerful the Pillow library in Python can be for such tasks. In this blog, I&#8217;ll guide you through the process of adding a border to an image using Pillow, sharing tips and tricks along the way to help you achieve the best results. <a href=\"https:\/\/www.aurorahometex.com\/pillow\/\">Pillow<\/a><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.aurorahometex.com\/uploads\/46633\/small\/crushed-memory-foam-pillow1a830.jpg\"><\/p>\n<h3>Understanding Pillow<\/h3>\n<p>Pillow is a fork of the Python Imaging Library (PIL) and is widely used for opening, manipulating, and saving many different image file formats. It provides a simple yet powerful API for performing a wide range of image processing tasks, including resizing, cropping, and adding borders. One of the key advantages of using Pillow is its cross &#8211; platform compatibility, which means you can use it on Windows, macOS, and Linux systems without any major issues.<\/p>\n<h3>Installation<\/h3>\n<p>Before you can start adding borders to images, you need to have Pillow installed. If you haven&#8217;t installed it yet, you can use <code>pip<\/code>, the Python package installer. Open your terminal or command prompt and run the following command:<\/p>\n<pre><code>pip install pillow\n<\/code><\/pre>\n<p>This command will download and install the latest version of Pillow from the Python Package Index (PyPI).<\/p>\n<h3>Basic Steps to Add a Border<\/h3>\n<p>Let&#8217;s start with the basic steps of adding a border to an image using Pillow. First, you need to import the necessary modules. In Python, the <code>Image<\/code> module from Pillow is the main entry &#8211; point for working with images.<\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\n# Open the image file\nimage = Image.open('your_image.jpg')\n\n# Define the border width and color\nborder_width = 20\nborder_color = 'black'\n\n# Create a new image with the border\nnew_image = Image.new(image.mode, (image.width + 2 * border_width, image.height + 2 * border_width), border_color)\n\n# Paste the original image onto the new image\nnew_image.paste(image, (border_width, border_width))\n\n# Save the new image\nnew_image.save('new_image_with_border.jpg')\n\n<\/code><\/pre>\n<p>In this code snippet, we first open an existing image using the <code>Image.open()<\/code> method. Then, we define the width and color of the border. We create a new, larger image with the specified background color, and finally, we paste the original image onto the new image at the appropriate position. The result is saved as a new file.<\/p>\n<h3>Customizing the Border<\/h3>\n<p>The basic example above gives you a simple, solid &#8211; colored border. However, you can customize the border in many ways to achieve different effects.<\/p>\n<h4>Changing the Border Color<\/h4>\n<p>You&#8217;re not limited to using basic colors like black or white. Pillow supports a wide range of color formats, including RGB tuples. For example, if you want a bright red border, you can use the following code:<\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\nimage = Image.open('your_image.jpg')\nborder_width = 20\nborder_color = (255, 0, 0)  # RGB for red\n\nnew_image = Image.new(image.mode, (image.width + 2 * border_width, image.height + 2 * border_width), border_color)\nnew_image.paste(image, (border_width, border_width))\nnew_image.save('red_border_image.jpg')\n<\/code><\/pre>\n<h4>Creating a Gradient Border<\/h4>\n<p>Creating a gradient border is a bit more complex but can add a very professional and eye &#8211; catching look to your image. You can achieve this by creating a gradient image and then slicing and pasting it around the original image.<\/p>\n<pre><code class=\"language-python\">from PIL import Image, ImageDraw\n\n# Open the image\nimage = Image.open('your_image.jpg')\nborder_width = 20\n\n# Create a gradient image\ngradient = Image.new('RGB', (border_width, image.height), color=(0, 0, 0))\ndraw = ImageDraw.Draw(gradient)\nfor y in range(image.height):\n    r, g, b = int((y \/ image.height) * 255), int((y \/ image.height) * 255), int((y \/ image.height) * 255)\n    draw.line((0, y, border_width, y), fill=(r, g, b))\n\n# Create a new image with the border\nnew_image = Image.new(image.mode, (image.width + 2 * border_width, image.height + 2 * border_width), (0, 0, 0))\nnew_image.paste(image, (border_width, border_width))\n\n# Paste the gradient on the left and right sides\nnew_image.paste(gradient, (0, border_width))\nflipped_gradient = gradient.transpose(Image.FLIP_LEFT_RIGHT)\nnew_image.paste(flipped_gradient, (image.width + border_width, border_width))\n\n# Save the image\nnew_image.save('gradient_border_image.jpg')\n<\/code><\/pre>\n<p>In this example, we first create a vertical gradient image. Then we create a new image with space for the border and paste the original image in the middle. Finally, we paste the gradient image on the left and right sides of the original image, after flipping it for the right &#8211; hand side.<\/p>\n<h3>Advanced Border Effects<\/h3>\n<p>Beyond simple colors and gradients, you can also create more advanced border effects, such as a rounded &#8211; corner border.<\/p>\n<pre><code class=\"language-python\">from PIL import Image, ImageDraw, ImageOps\n\n# Open the image\nimage = Image.open('your_image.jpg')\nborder_width = 20\nradius = 30\n\n# Create a mask for rounded corners\nmask = Image.new('L', (image.width + 2 * border_width, image.height + 2 * border_width), 0)\ndraw = ImageDraw.Draw(mask)\ndraw.rounded_rectangle((0, 0, mask.width, mask.height), radius=radius, fill=255)\n\n# Create a new image with the border\nborder_color = (255, 255, 255)\nnew_image = Image.new(image.mode, (image.width + 2 * border_width, image.height + 2 * border_width), border_color)\nnew_image.paste(image, (border_width, border_width))\n\n# Apply the mask\nnew_image = Image.composite(new_image, Image.new(image.mode, new_image.size, (0, 0, 0)), mask)\n\n# Save the image\nnew_image.save('rounded_border_image.jpg')\n<\/code><\/pre>\n<p>In this code, we first create a mask for the rounded corners using <code>ImageDraw.rounded_rectangle()<\/code>. Then we create a new image with the border and paste the original image onto it. Finally, we use <code>Image.composite()<\/code> to apply the mask, which results in an image with rounded &#8211; corner borders.<\/p>\n<h3>Use Cases<\/h3>\n<p>Adding borders to images has numerous practical use cases. In digital marketing, a well &#8211; designed border can make an image more prominent in social media posts or email newsletters. For photographers, borders can add a professional touch to their portfolio images. Graphic designers can use borders to create a cohesive look across different elements in a design project.<\/p>\n<h3>Why Choose Our Pillow Products<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/www.aurorahometex.com\/uploads\/46633\/small\/wool-filled-blanket74faf.jpg\"><\/p>\n<p>As a leading pillow supplier, we offer high &#8211; quality Pillow &#8211; related resources. Our libraries are well &#8211; maintained, with regular updates to ensure compatibility with the latest Python versions and security patches. We also provide comprehensive documentation and excellent customer support. Whether you&#8217;re a beginner just starting to explore image processing or an experienced developer looking for advanced features, our Pillow products can meet your needs.<\/p>\n<p><a href=\"https:\/\/www.aurorahometex.com\/pillow\/microfiber-pillow\/\">Microfiber Pillow<\/a> If you&#8217;re interested in using our Pillow &#8211; related resources for your image processing projects, or if you have any questions about adding borders or other image manipulation tasks, we&#8217;d love to hear from you. Contact us to start a procurement discussion and find out how we can help you achieve your goals.<\/p>\n<h3>References<\/h3>\n<ul>\n<li>Pillow official documentation<\/li>\n<li>Python official documentation<\/li>\n<li>Various online image processing tutorials<\/li>\n<\/ul>\n<hr>\n<p><a href=\"https:\/\/www.aurorahometex.com\/\">Hangzhou Aurora Textiles Co., Ltd.<\/a><br \/>As one of the most professional pillow manufacturers and suppliers in China, we also support custom service. Please feel free to wholesale bulk high-end pillow at competitive price from our factory. Good service and quality products are available.<br \/>Address: Room 1403, Building 1, Hangzhou Xiaoshan International Business Center, No. 458 Jincheng Road, Beigan Street, Xiaoshan District, Hangzhou City<br \/>E-mail: linda@aurorahometex.com<br \/>WebSite: <a href=\"https:\/\/www.aurorahometex.com\/\">https:\/\/www.aurorahometex.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Adding a border to an image can significantly enhance its visual appeal, making it stand out &hellip; <a title=\"How to add a border to an image using Pillow?\" class=\"hm-read-more\" href=\"http:\/\/www.kd-trip.com\/blog\/2026\/09\/02\/how-to-add-a-border-to-an-image-using-pillow-44aa-7b1e94\/\"><span class=\"screen-reader-text\">How to add a border to an image using Pillow?<\/span>Read more<\/a><\/p>\n","protected":false},"author":759,"featured_media":3311,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3274],"class_list":["post-3311","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-industry","tag-pillow-464d-7b51c0"],"_links":{"self":[{"href":"http:\/\/www.kd-trip.com\/blog\/wp-json\/wp\/v2\/posts\/3311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.kd-trip.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.kd-trip.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.kd-trip.com\/blog\/wp-json\/wp\/v2\/users\/759"}],"replies":[{"embeddable":true,"href":"http:\/\/www.kd-trip.com\/blog\/wp-json\/wp\/v2\/comments?post=3311"}],"version-history":[{"count":0,"href":"http:\/\/www.kd-trip.com\/blog\/wp-json\/wp\/v2\/posts\/3311\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.kd-trip.com\/blog\/wp-json\/wp\/v2\/posts\/3311"}],"wp:attachment":[{"href":"http:\/\/www.kd-trip.com\/blog\/wp-json\/wp\/v2\/media?parent=3311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.kd-trip.com\/blog\/wp-json\/wp\/v2\/categories?post=3311"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.kd-trip.com\/blog\/wp-json\/wp\/v2\/tags?post=3311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}