0

I'm looking for a library that enables to "create pictures" (or even videos) with the following functions:

  • Accepting picture inputs
  • Resizing said inputs to fit given template / scheme
  • Positioning the pictures in pre-set up layers or coordinates

A rather schematic approach to look at this: schematic approach picture whereas the red spots are supposed to represent e.g. text, picture (or if possible video) elements.

The end goal would be to give the .py script multiple input pictures and the .py creating a finished version like mentioned above.

Solutions I tried were looking into Python PIL, but I wasn't able to find what I was looking for.

J. Doe
  • 61
  • 1
  • 2
  • 9
  • Seems a very unique case(but hey maybe its not unique.. what do i know). Of the top of my head there arent any libraries with that subset of functionalities you are looking for. I am sure you can create your own utility library tough using pillow and opencv. – Akib Rhast May 27 '20 at 18:41
  • Oh - that suprises me, I thought I wouldn't be the first "idiot" asking how to create a picture (or how to re-size and re-position) input images on a blank canvas, so basically modifying a picture.. Anyhow, could you guide me towards what you exactly mean with "making [my] own utility library through using pillow and opencv"? – J. Doe May 27 '20 at 18:46

2 Answers2

2

Yes, it is possible to do this with Python.

The library you are looking for is OpenCV([https://opencv.org][1]/).
Some basic OpenCV python tutorials (https://docs.opencv.org/master/d9/df8/tutorial_root.html).

1) You can use imread() function to read images from files.
2) You can use resize() function to resize the images.
3) You can create a empty master numpy array matching the size and depth(color depth) of the black rectangle in the figure you have shown, resize your image and copy the contents into the empty array starting from the position you want.

Below is a sample code which does something close to what you might need, you can modify this to suit your actual needs. (Since your requirements are not clear I have written the code like this so that it can at least guide you.)

import numpy as np
import cv2
import matplotlib.pyplot as plt

# You can store most of these values in another file and load them.
# You can modify this to set the dimensions of the background image.
BG_IMAGE_WIDTH = 100
BG_IMAGE_HEIGHT = 100
BG_IMAGE_COLOR_DEPTH = 3

# This will act as the black bounding box you have shown in your figure.
# You can also load another image instead of creating empty background image.
empty_background_image = np.zeros(
    (BG_IMAGE_HEIGHT, BG_IMAGE_WIDTH, BG_IMAGE_COLOR_DEPTH),
    dtype=np.int
)

# Loading an image.
# This will be copied later into one of those red boxes you have shown.
IMAGE_PATH = "./image1.jpg"
foreground_image = cv2.imread(IMAGE_PATH)

# Setting the resize target and top left position with respect to bg image.
X_POS = 4
Y_POS = 10
RESIZE_TARGET_WIDTH = 30
RESIZE_TARGET_HEIGHT = 30

# Resizing
foreground_image= cv2.resize(
    src=foreground_image,
    dsize=(RESIZE_TARGET_WIDTH, RESIZE_TARGET_HEIGHT),
)

# Copying this into background image
empty_background_image[
    Y_POS: Y_POS + RESIZE_TARGET_HEIGHT,
    X_POS: X_POS + RESIZE_TARGET_WIDTH
] = foreground_image

plt.imshow(empty_background_image)
plt.show()
the23Effect
  • 580
  • 2
  • 7
  • Mind elaborating? How can my described functions be done in OpenCV? – J. Doe May 27 '20 at 19:19
  • I guess you should modify the question a bit(This will help people answer your questions better. I understand what you need now. Give me a few mins I will edit my answer. – the23Effect May 27 '20 at 19:22
  • @the23Effect is definitely along the trail of line I was thinking. mentioned he is going to edit his answr arund something he understood. While I am still not 100% clear on your requirements. Your idea is interesting care to explain more. (1)In the above example you provided a schematic to where your pictures will be positioned. Are you expecting to provide a similar schematic file to your python script/template. (2)After providing your schematic file to your script are you expecting the script to parse it and extract positional data, where A(coordinate of top left box),B(coord top right box) – Akib Rhast May 27 '20 at 19:51
  • C(Coordinate of bottom right box). (3) Are you then planning to feed your script some picture [P1,P2,P3] and tell it put P1 inside A, P2 inside B, P3 inside C. And the script will automatically put those pictures on those area and take care of the resize – Akib Rhast May 27 '20 at 19:57
0

SVG is a simple format that allows embedding of images and text. I can create something like the format in your question like so:

<svg version="1.1"
     width="300" height="200"
     xmlns="http://www.w3.org/2000/svg">

  <text x="90" y="20">Header text</text>
  
  <image x="5" y="5" width="80" href="https://www.gravatar.com/avatar/0637b785ee777c5e9a4ec41db669e4fb?s=256&amp;d=identicon&amp;r=PG&amp;f=y&amp;so-version=2"/>
  
  <image x="90" y="100" width="80" href="https://www.gravatar.com/avatar/fc4451a6d03317ab3465907e8402f6ba?s=256&amp;d=identicon&amp;r=PG"/>

</svg>

Since this is a text format, you can use a string templating engine like mustache to insert custom image and text content given a template, then render your SVG.

MikeFHay
  • 8,562
  • 4
  • 31
  • 52