'Add marging around png image TypeError:color must be int or single-element tuple
I have a code that enables me to add margins around png images and it is working well, till I encountered a black and white color image. At this point, the code gives me an error like that
TypeError: color must be int or single-element tuple.
from PIL import Image, ImageSequence
def add_margin(pil_img, top, right, bottom, left, color):
width, height = pil_img.size
new_width = width + right + left
new_height = height + top + bottom
result = Image.new(pil_img.mode, (new_width, new_height), color)
result.paste(pil_img, (left, top))
return result
im = Image.open('Sample.png')
for i, page in enumerate(ImageSequence.Iterator(im)):
if i == 2: break
page = add_margin(page, 150, 400, 400, 400, (255, 255, 255))
page.save('Output.png')
I could solve it by changing this line in the function
result = Image.new(pil_img.mode, (new_width, new_height), 'white')
But I welcome any other ideas
Solution 1:[1]
Since your image is in black and white mode, you should specify white as 1 instead of (255, 255, 255) because it is a binary value.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Emanuele Scarabattoli |

