2

I want to add zeros on all sides of image. I try:

from PIL import Image
im = Image.open("/content/drive/My Drive/image31.png")

def pad(im,layers):
    return [[0]*(len(im[0])+2*layers)]*layers    + \
           [[0]*layers+r+[0]*layers for r in im] + \
           [[0]*(len(im[0])+2*layers)]*layers
pad(im,1)

Error is : TypeError: 'PngImageFile' object does not support indexing

martineau
  • 119,623
  • 25
  • 170
  • 301
  • possible duplicate https://stackoverflow.com/questions/35751306/python-how-to-pad-numpy-array-with-zeros – Cătălina Sîrbu Mar 03 '20 at 09:42
  • 2
    What about [`PIL.ImageOps.pad`](https://pillow.readthedocs.io/en/stable/reference/ImageOps.html#PIL.ImageOps.pad)? – HansHirse Mar 03 '20 at 09:45
  • That's not how to access pixel data in PIL. See [How to convert a grayscale image into a list of pixel values?](https://stackoverflow.com/questions/40727793/how-to-convert-a-grayscale-image-into-a-list-of-pixel-values) for an example of how to do it for a grayscale image. A multi-component color image would be very similar. It's also unclear what you mean by "zero pad" an image. – martineau Mar 03 '20 at 10:08

0 Answers0