2
$\begingroup$

I would like to merge a few images into a single strip with the images next to each other. Using compositor didn't work out as I expected. Thinking a bit more about it, I would prefer not to use compositor.

And use python for this now I am wondering if the python part of blender could join images like that. Blender is not PIL, so I'm not sure if this is possible. The strip can be either horizontal or vertical.

* UPDATE * My alternative option, is to break open the pgn or jpg codec, read images as RGB data, and output a final RGB string basedupon multiple images. while possible i wonder if there are easier methods using Blender python API, can blender python do memory based like image manipulation, like addressing pixels directly to set their values ?

$\endgroup$
  • 2
    $\begingroup$ so what's "PIL"? $\endgroup$ – user1853 Dec 13 '14 at 0:08
  • $\begingroup$ I understand what you wish to accomplish, but to be able to provide an answer I need to know the greater context and workflow. Where do the images come from, and what are you wanting as the larger final result of the process of which this is one of the steps? $\endgroup$ – brasshat Dec 13 '14 at 3:59
  • $\begingroup$ @cegaton, I bet he's referring to the Python Imaging Library: pythonware.com/products/pil $\endgroup$ – ajwood Dec 14 '14 at 14:50
  • $\begingroup$ Yup, Pil stands for Pytohn Image Library, as library full of functions for creating and manipulating images. $\endgroup$ – user613326 Dec 14 '14 at 19:56
  • $\begingroup$ Oh I see now, you want to do automation of this with python, Ill update my answer in the morning with some python coding.. $\endgroup$ – Jaroslav Jerryno Novotny Dec 14 '14 at 21:11
2
$\begingroup$

Using the Import Images as Planes addon you can import several images and align them.

enter image description here

Select the images, set the offset value and materials for the plane.

enter image description here

Blender will then create an plane for each image and line them up on the x axis.

enter image description here

To line up the planes in a different axis there is a modified script that you can find here

enter image description here

The blenderartists.or thread for this script is here

Then just set up an orthographic camera to frame your images and set your render size to your needs.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ I know this method, While indeed this does show an image strip, its not realy handy in terms of making the final image, in the way i like to include it in python script. $\endgroup$ – user613326 Dec 14 '14 at 19:57
2
$\begingroup$

You can create an empty texture of size of the resulting image strip and alpha over all of your images over it in compositor and translate them with translate node into positions and render it:

With python:

  • load in you image textures:

    import bpy
    
    def load_image(file_path):
        try:
            return bpy.data.images.load(filepath)
        except:
            raise NameError("Cannot load image %s" % filepath)
    
    image_names = ['first.png', 'second.jpg', 'third.bmp']
    images = []
    for name in image_names:
        real_path = bpy.abspath('//' + name)
        images.append(load_image(real_path))
    
  • Get image dimension:

    images[0].size[0]    # resolution x
    images[0].size[1]    # resolution y
    
  • Create empty image of desired resolution for example 1024x256:

    bg = bpy.ops.image.new(name="bg", width=1024, height=256, alpha=False)
    
  • How to create nodes in compositor and link them:

    Controling compositor by python

  • Render:

    render = bpy.context.scene.render
    # set this
    render.resolution_x = 1024
    render.resolution_y = 256
    # render image
    render.render()
    # use File_Output node or specify render.filepath to also save the result
    

Getting low level with manipulating image pixels:

# you get float values for each pixel like this:
# in png they are stored nicely in rgba order
image = bpy.data.images['SOME_NAME.PNG']
for i in range(0, len(image.pixels), 4):
    rgba = image.pixels[i:i+4]
    # this is also writable ofc

There are couple things to keep in mind though:

  • Blender uses opengl internally so the origin is in the lower left corner of the image (with the y-axis pointing upwards).
  • Some image programs might not save the transparency pixels
  • The image might have alpha premultiplied, but this shouldn't be a concern. In that case the values are in form of [r * a, g * a, b * a, a]
|improve this answer|||||
$\endgroup$
  • $\begingroup$ i understand this workflow, but it seams a bit how to say complex road, for just image manipulation, doesnt blender have some api for some straight image manipulation ? (as i prefer not to change composer setups) $\endgroup$ – user613326 Dec 15 '14 at 14:31
  • $\begingroup$ Ok I added some image pixel manipulation $\endgroup$ – Jaroslav Jerryno Novotny Dec 15 '14 at 16:19
  • $\begingroup$ yes i think something along that way. for exampl img1 [r,g,b,a] + img2 [r,g,b,a] = new[r,g,b,a] thinking about it, numpy is part of blender ? numpy should be ablee to do this $\endgroup$ – user613326 Dec 16 '14 at 14:32
  • $\begingroup$ Yes numpy module is shipped with blender. from numpy import * will work $\endgroup$ – Jaroslav Jerryno Novotny Dec 16 '14 at 14:38
  • $\begingroup$ I had no idea numpy was in Blender. There are actually questions on this site of people asking how to install it. I'll try using numpy for image manipulation, because it should be possible, and it should be much much much faster. $\endgroup$ – Chris Conlan Nov 15 '18 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.