2

I need to iterate with a Python functions into images dir, for every image I need to add extra bottom part: enter image description here

I was thinking to load image, calculate dimension, create bigger black image, and paste on it:

import numpy as np
import cv2

def add_border(image):
    s_img = cv2.imread(image)
    dimensions = s_img.shape
    blank_image = np.zeros((s_img.shape[0]+200,s_img.shape[1],3), np.uint8)
    x_offset=y_offset=50
    blank_image[y_offset:y_offset+s_img.shape[0], x_offset:x_offset+s_img.shape[1]] = s_img

    cv2.imshow("black", blank_image)
    cv2.imwrite('C:\\test\\' + 'black.jpg', blank_image)

    return (True)

add_border('C:\\test\\img001.JPG')

I receive following error:

blank_image[y_offset:y_offset+s_img.shape[0], x_offset:x_offset+s_img.shape[1]] = s_img
ValueError: could not broadcast input array from shape (522,928,3) into shape (522,878,3)

Any suggestion? Thank you

user3925023
  • 667
  • 6
  • 25
  • See numpy concatenate at https://docs.scipy.org/doc/numpy/reference/generated/numpy.concatenate.html and at https://stackoverflow.com/questions/7589012/combining-two-images-with-opencv – fmw42 Aug 29 '19 at 16:27

1 Answers1

2

You don't really need any Python to do that, you can just do it with ImageMagick which is installed on most Linux distros and is available for macOS and Windows. Just in Terminal (Command Prompt on Windows):

magick image.png -background "rgb(68,114,196)" -gravity south -splice 0x40%  result.png

enter image description here

If you want to splice 10% extra onto the top instead, use:

magick image.png -background "rgb(68,114,196)" -gravity north -splice 0x10%  result.png

enter image description here


If your ImageMagick is older than v7, use convert in place of magick in the above commands.


If you want to do all the images in a directory, go into that directory and make a new sub-directory for your results, then use mogrify:

cd <WHERE THE IMAGES ARE>
mkdir results
magick mogrify -path results -background "rgb(68,114,196)" -gravity south -splice 0x40%  *png

If you want to use OpenCV and Python, you can do this:

import cv2
import Numpy as np

# Load image
im = cv2.imread('image.png')

# Make a blue pad to append to bottom, same width as original and 30 pixels tall
# ... remembering OpenCV uses BGR ordering
pad = np.full((30,im.shape[1],3), [196,114,68], dtype=np.uint8)

# Stack pad vertically below the original image and save
result = np.vstack((im,pad))
cv2.imwrite('result.png',result)
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Mark, see numpy concatenate, also, as per my comment above. – fmw42 Aug 29 '19 at 16:36
  • @fmw42 Thank you, yes. I kind of find it easier to remember `vstack()` is vertical, `hstack()` is horizontal and `dstack()` is depth... unless there is some other benefit of `concatenate()` I have failed to appreciate? – Mark Setchell Aug 29 '19 at 16:39
  • No other benefit. Just found it searching for an answer. But vstack and hstack are fine. They all seem about as simple. Just thought you might want to mention it as an alternative in your answer. – fmw42 Aug 29 '19 at 16:43