Page 1 of 1
Splitting images vertically, halfway across width of image
Posted: 2009-10-28T08:02:24-07:00
by jrtayloriv
I've got a batch of about 150 JPEG images that are named 0010.jpg, 0012.jpg, 0014.jpg ...
I am trying to figure out a command that will take each of these images and say "Go 50% across the width of the image from the left edge, and split it down the middle (from top to bottom). This will create two seperate jpg files, one for the left half, one for the right half. If the original (unsplit) image is named 0150.jpg, then the left half would be named 0150.jpg (same as original), and the right image would be named 0151.jpg".
How do I do this?
Thanks,
Jesse Taylor
Re: Splitting images vertically, halfway across width of image
Posted: 2009-10-28T08:37:07-07:00
by Bonzo
What method do you plan on using, command line php, magickwand, batch scripts?
Re: Splitting images vertically, halfway across width of image
Posted: 2009-10-28T09:02:27-07:00
by jrtayloriv
Bonzo wrote:What method do you plan on using, command line php, magickwand, batch scripts?
I honestly don't know which the best tool to use here. Anything that would work would be fine with me. I am familiar with both PHP and bash scripting, so either one of those would be fine. I'm not very familiar at all with Imagemagick though -- I've been trying to read the manual to find something that will do what I want.
The -crop and -repage (to resize canvas to be half width) commands seem closest to what I want, but I can't figure out how to use it to do what I want.
Thanks for the quick response.
--Jesse
Re: Splitting images vertically, halfway across width of image
Posted: 2009-10-28T09:50:27-07:00
by fmw42
Re: Splitting images vertically, halfway across width of image
Posted: 2009-10-28T10:09:48-07:00
by Bonzo
I prefer php and would do something like read the images into an array and then crop them. I have some php and batch file examples on my site although they could do with updating.
Untested codee:
Code: Select all
<?php
// Open the directory and create an array of the images
// Directory to read images from
$dir = 'gallery/'.$read;
// Get the folder name
$folder = explode('/', $dir);
foreach (glob($dir."{*.jpg}", GLOB_BRACE) as $filename)
{
// Left image name = $filename but change last 0 to a 1 in $filename for right image
$right = str_replace( '0.jpg', '1.jpg', $filename);
// Get the image size for the split
$size = getimage_size($filename);
$width = round($size[0]/2);
$height = $size[1];
// Left image
$cmd = "$filename -crop {$width}x{$height)+0+0 ";
exec("convert $cmd $filename");
// Right image
$cmd = "$filename -crop {$width}x{$height)+{$width}+0 ";
exec("convert $cmd $right");
}
?>
If you prefer batch files as I say there are some examples on my site that you may be able to modify:
http://www.rubblewebs.co.uk/imagemagick/batch.php
Anthony has loads of examples on his site including some windows tips:
http://www.imagemagick.org/Usage/
Re: Splitting images vertically, halfway across width of image
Posted: 2009-10-30T10:00:51-07:00
by fmw42
this seems to work for me using mogrify on a folder of images. create a new folder test2. cd to original folder test1
mogrify -path /pathto/test2 -format jpg -crop 50%x100% +repage *
it splits each one into 2 files with the addition of -0 and -1 to the file names and puts them into folder test2 leaving your original images in test1 unchanged
Re: Splitting images vertically, halfway across width of image
Posted: 2009-11-01T19:17:22-07:00
by anthony
fmw42 wrote:it splits each one into 2 files with the addition of -0 and -1 to the file names and puts them into folder test2 leaving your original images in test1 unchanged
Assuming the original image did not have any -0 or -1 in their file names.
Mogrify is dangerous. Caution is always recommended.
Re: Splitting images vertically, halfway across width of image
Posted: 2009-11-01T19:51:30-07:00
by fmw42
Mogrify is dangerous. Caution is always recommended.
True. That is why I always mogrify (when I must) to a new folder, so I cannot overwrite any original images.