tags:

views:

211

answers:

2

I am probably looking for the wrong thing in the handbook, but I am looking to take an image object and expand it without resizing (stretching/squishing) the original image.

Toy example: imagine a blue rectangle, 200 x 100, then I perform some operation and I have a new image object, 400 x 300, consisting of a white background upon which a 200 x 100 blue rectangle rests. Bonus if I can control in which direction this expands, or the new background color, etc.

Essentially, I have an image to which I will be adding iteratively, and I do not know what size it will be at the outset.

I suppose it would be possible for me to grab the original object, make a new, slightly larger object, paste the original on there, draw a little more, then repeat. It seems like it might be computationally expensive. However, I thought there would be a function for this, as I assume it is a common operation. Perhaps I assumed wrong.

+4  A: 

The ImageOps.expand function will expand the image, but it adds the same amount of pixels in each direction.

The best way is simply to make a new image and paste:

newImage = Image.new(mode, (newWidth,newHeight))
newImage.paste(srcImage, (x1,y1,x1+oldWidth,x1+oldHeight))

If performance is an issue, make your original image bigger than needed and crop it after the drawing is done.

interjay
+2  A: 

You might consider a rather different approach to your image... build it out of tiles of a fixed size. That way, as you need to expand, you just add new image tiles. When you have completed all of your computation, you can determine the final size of the image, create a blank image of that size, and paste the tiles into it. That should reduce the amount of copying you're looking at for completing the task.

(You'd likely want to encapsulate such a tiled image into an object that hid the tiling aspects from the other layers of code, of course.)

retracile
+1 most scalable image processing is done on a tile basis.
whatnick