Splitting images vertically, halfway across width of image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
jrtayloriv
Posts: 2
Joined: 2009-10-28T07:55:45-07:00
Authentication code: 8675309
Location: Inland NW

Splitting images vertically, halfway across width of image

Post 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
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Splitting images vertically, halfway across width of image

Post by Bonzo »

What method do you plan on using, command line php, magickwand, batch scripts?
jrtayloriv
Posts: 2
Joined: 2009-10-28T07:55:45-07:00
Authentication code: 8675309
Location: Inland NW

Re: Splitting images vertically, halfway across width of image

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Splitting images vertically, halfway across width of image

Post by fmw42 »

see my tidbits page on this topic at http://www.fmwconcepts.com/imagemagick/ ... .php#crop1
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Splitting images vertically, halfway across width of image

Post 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/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Splitting images vertically, halfway across width of image

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Splitting images vertically, halfway across width of image

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Splitting images vertically, halfway across width of image

Post 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.
Post Reply