Help - Search - Members - Calendar
Full Version: Image Sticher (Batch)
ieXbeta Board > Tech > Developer Center
AYHJA
I have a situation, and maybe if I explain it, I can get a good solution...

I have a series of images that need to be joined together vertically, one on top of the other to form a column...Once the columns are formed, they will then be joined horizontally to create one image...Each of the images is of a near identical size, and no special editing is needed beyond them simply being joined...An example:

The First Series

702 Images Total
27 Images Per Column
26 Columns

Each column is named like this: 5-0-0, 5-0-1, 5-0-2, etc, until the last image 5-0-26...Each subsequent column is 5-1-0, 5-2-0, 5-3-0, etc...

Screenshot:
IPB Image

What I have been doing:

Using Irfanview's panoramic image maker to join the columns one by one, and then to join the columns...A fine method for a few pics, downright tedious for 12...The method to get the images is also somewhat problematic, but not nearly as much as putting them together...

What I would like to do:

Automate the process..! Perhaps put a range of images, and just have it stitch the usual 25 or so columns together as single images...If it can then put the columns together horizontally, that's great, but I feel that doing that much myself won't be that bad...

Any help (Chugworth? lol)
DangerousDave86
I could probably do this in a small PHP app, though a web version wouldn't be the best way to go about this. the GD library in PHP should be able to handle this, and as PHP is a scripting language you can make it do whatever you want.

It looks dead easy to do, just need to figure a way to define rows and columns and user interface I guess.
Psy-Blade
Dave needs to get a life sometimes.

haha, nah good luck.
DangerousDave86
My script works now. I'm tempted to put in into exe form, but I dunno how well that will go.
AYHJA
Heya Dave...

Thanks for thinking of helping...

I could upload a zip of a broken apart image if you need it to test...
DangerousDave86
Could you? I'd like to see some results of my work.
Anyway, here's the code, very rough, barely any error checking, no user input.

CODE
<?php
/*
PROCESS
  Load all files from $dir into an array. (specifically .pngs with -'s in the name).
    0-0-0.png = image 0, column 0, row 0.
    1-0-0.png would equal a whole new file to be generated from a different set of pngs.
  sort that array into sub-arrays for images, columns, rows.
  loop columns and rows merging them into master image.
  save master image as #.png depending on source set of images.
*/

// Get Files in current working directory.
$dir = getcwd ().'/images/';
$files = scandir ($dir);

// Loop through files, getting images.
$images = array ();
foreach ($files as $file)
{
    // If image aint PNG, skip.
    if (!strpos($file, ".png")) continue;
    if (!strpos($file, "-")) continue;
    // If image PNG add it to new array
    $str = substr($file, 0, strlen($file)-4);
    $arr = explode('-', $str);
    $images[(int)$arr[0]][(int)$arr[1]][(int)$arr[2]] = $file;
}
$finishedImages = array();
foreach ($images as $index => $imgarr)
{
    $temp = getimagesize ($dir.$imgarr[0][0]);
    $x = $temp[0];
    $y = $temp[1];
    // load images
    foreach ($imgarr as $cindex => $column)
    {
        foreach ($column as $rindex => $row)
        {
            $imgarr[$cindex][$rindex] = imagecreatefrompng($dir.$row);
            if (imagesx ($imgarr[$cindex][$rindex]) != $x OR imagesy($imgarr[$cindex][$rindex]) != $y) die ("non-matching image sizes");
        }
    }
    // ! create a new GD image to get the ball rolling
    $finishedImages[$index] = imagecreatetruecolor($x*count($imgarr), $y*count($imgarr[0]));
    foreach ($imgarr as $cindex => $column)
    {
        foreach ($column as $rindex => $row)
        {
            imagecopy ($finishedImages[$index], $row, $cindex*$x, $rindex*$y, 0, 0, $x, $y);
        }
    }
    imagepng ($finishedImages[$index] , $dir.$index.'.png', 0);
}
?>


simply tell the script where the files are at the top.
and it will create a png(s) with the filename of the first number in the 0-0-0 filename. I'm assuming your using pngs from the image, if not alterations will need making.
AYHJA
Thanks Dave, I certainly appreciate it...

Heres the pieces of an image, it is not work safe, btw...

http://www.speedyshare.com/197310829.html
AYHJA
How am I supposed to run this, btw..?
DangerousDave86
I thought the lack of UI might throw u off.
Whack it on a webserver, anywhere with a folder called "images" whack ur images in images and the visit the page where the php is. That should do it.
DangerousDave86
I see your using jpgs. From your screen shot it looked like u were using pngs. ill modify it - testing now, it could do with some efficiency work, MAN theres a lot of jpgs. and PHP cant get thru them very quickly, its using an awful lot of memory
AYHJA
Hmm...

Maybe I did something wrong..?

http://www.kumicho.org/CZ/CZ.php
DangerousDave86
Its currently outputing files bigger than the input!
CODE
<?php
/*
PROCESS
  Load all files from $dir into an array. (specifically .pngs with -'s in the name).
    0-0-0.png = image 0, column 0, row 0.
    1-0-0.png would equal a whole new file to be generated from a different set of pngs.
  sort that array into sub-arrays for images, columns, rows.
  loop columns and rows merging them into master image.
  save master image as #.png depending on source set of images.
*/

// Get Files in current working directory.
$dir = getcwd ().'/images/';
$files = scandir ($dir);

// Loop through files, getting images.
$images = array ();
foreach ($files as $file)
{
    // If image aint PNG, skip.
    if (!strpos($file, ".jpg")) continue;
    if (!strpos($file, "-")) continue;
    // If image PNG add it to new array
    $str = substr($file, 0, strlen($file)-4);
    $arr = explode('-', $str);
    $images[(int)$arr[0]][(int)$arr[1]][(int)$arr[2]] = $file;
}
$finishedImages = array();
foreach ($images as $index => $imgarr)
{
    $temp = getimagesize ($dir.$imgarr[0][0]);
    $x = $temp[0];
    $y = $temp[1];
    // load images
    // ! create a new GD image to get the ball rolling
    $finishedImages[$index] = imagecreatetruecolor($x*count($imgarr), $y*count($imgarr[0]));
    foreach ($imgarr as $cindex => $column)
    {
        foreach ($column as $rindex => $row)
        {
            #if (imagesx ($imgarr[$cindex][$rindex]) != $x OR imagesy($imgarr[$cindex][$rindex]) != $y) die ("non-matching image sizes");
            echo "col:".$cindex." row:".$rindex."\n";
            $image = imagecreatefromjpeg($dir.$row);
            imagecopy ($finishedImages[$index], $image, $cindex*$x, $rindex*$y, 0, 0, $x, $y);
            imagedestroy($image);
        }
    }
    imagejpeg ($finishedImages[$index] , $dir.$index.'.jpg', 100);
}
?>
Thats with 100% jpg quality output, to change it, just change the 100 on the last line. from the rar it gave me a 10meg image!!!

Good amount of details though wink2.gif

@ 75% quality the file size is 2.5mb, so that sounds like a better number.

I might write up an interface for it. It could output the image to the browser after a zip/rar upload. But I haven't got time for that till Sunday.

wtf!! New fast replies get added to your last post! without an edit mark either.
AYHJA
Yeah, they are usually around 10 MB, I don't know why it does it, but it does it when I do it the other way also...

Can't wait to test it out man, thanks so much..!
DangerousDave86
no problem, I love to problem solve in php.
AYHJA
OK, Let me get this straight...

Lets say I create a directory on my server called czm...
In that directory, I make a folder named 'images' and that's where I put the pieces...
And then I upload the script into the images folder, or the czm folder..?
DangerousDave86
put it in czm.
AYHJA
Made some slight changes to it, but by gollie, its working..! Its ram intensive, we had to increase the buffer of the server, lol...

I made 6 CyberZooms in about 10 minutes yesterday...That's fucking unreal..! Can this be made to run locally, or is that what you meant by gui..?
DangerousDave86
You can run it locally, thats how I made it - using command line php.
i'll let you know the details if I get a chance to write somet up.
AYHJA
Take your time M8, this is excellent frame work, and it has done what I've asked it to do...Anything you do from here on out is pure polish...I've been making them like crazy, lol...
DangerousDave86
QUOTE(AYHJA @ Mar 2 2007, 19:11) *

Take your time M8, this is excellent frame work, and it has done what I've asked it to do...Anything you do from here on out is pure polish...I've been making them like crazy, lol...

I could see why!
Sinbad
This doesn't seem too hard to do in C++ just making a simple command line program. Especially if I can find a good image processing library (a la gd image)

If nothing else, good coding practice.
AYHJA
I'll see if I can find some resources, Sinbad... biggrin.gif
Hasin
this is that lifesize model pic right? lol finally got em peiced tongue.gif
AYHJA
Yes, they're fairly oversized, but damn they're gorgeous, lol...
DangerousDave86
GD should be a C library as all (most) PHP libraries are C
DangerousDave86
This exe works on the same principle as the script, requires images directory.
However, the conversion process has changed how arrays behave and you can only create one image at a time and its filename will always be 0.jpg.

Click to view attachment
AYHJA
Thanks DD86, you have been a great help to me and members of my community...
AYHJA
How's that offline version coming...? smile.gif
DangerousDave86
QUOTE(AYHJA @ Apr 4 2007, 08:31) *

How's that offline version coming...? smile.gif

Haven't really had time to make any improvements or start a user interface. Got any ideas for it?
AYHJA
Just a REEEEEALY simple interface that I can use much in the same way I use it online...I don't think my server cares much for multiple thread uploading, and uploading 500+ images takes a while...

If all it is is a button that says Go, and will make CZ's of whatever is in the images folder, that's all I need...

Guiliana Marino, Miss April would be proud to see it... smile.gif
DangerousDave86
It could be made to handle zips of files... Other than that, http://board.iexbeta.com/index.php?act=Att...ost&id=2109, is most useful for offline stiching.
AYHJA
Hey, Zips sound nice...And it would prevent me from having to delete so many files off myserver too....
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.