Hamza M. S. Abazeed
Hamza M. S. Abazeed

Reputation: 105

add a black space for an image in Python pillow

I wish to expand an image, so I can write something at the black expanded space under the original image, but it doesn't work. I can't expand a black space and add it to the image, neither can write at a specific place

I'm new to the Pillow library, can anyone help?

Upvotes: 0

Views: 1403

Answers (1)

Tibebes. M
Tibebes. M

Reputation: 7538

You could do something like this:

  • read the image
  • create a new image (black by default) with the desired size
  • get data of the input image and put it down on the new one
from PIL import Image

HEIGH_OF_THE_BLACK_AREA = 100

with Image.open('image.jpg') as im:        
    new_im = Image.new(im.mode, size = (im.size[0], im.size[1] + HEIGH_OF_THE_BLACK_AREA))

    new_im.putdata(im.getdata())

    new_im.save('out.jpg')

Upvotes: 1

Related Questions