Split image into two

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
menteith
Posts: 10
Joined: 2015-03-30T14:49:19-07:00
Authentication code: 6789

Split image into two

Post by menteith »

Hi all,

I am new to IM but I am amazed by its features.

I use the following command to crop and split all jpgs in a folder in two (producing two files)

Code: Select all

convert page000.jpg -crop 2x1@ +repage -colorspace gray +dither -colors 4 *.jpg
from viewtopic.php?t=19500

but this command tends to not split the last file and makes file names looking quite strange.

I am amazed how IM works and how much time it can save - I have lots of scanned book and the command is to make it less time-consuming

What I would like to achieve is to better naming (e.g. file.jpg -> file_a.jpg file_b.jpg - it is just an example).

What's the best setting to convert scanned books to jpg and then to pdf? The setting I use is great when I print a book it it takes much space.

Thank you!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Split image into two

Post by fmw42 »

Use this syntax for naming:

Code: Select all

convert page000.jpg -crop 2x1@ +repage -colorspace gray +dither -colors 4 page000_%d.jpg
'

The two parts will be named page000_0.jpg and page000_1.jpg

or just

Code: Select all

convert page000.jpg -crop 2x1@ +repage -colorspace gray +dither -colors 4 page000.jpg
'

and the two parts will be named page000-0.jpg and page000-1.jpg

Sorry I do not understand what you mean by
this command tends to not split the last file
menteith
Posts: 10
Joined: 2015-03-30T14:49:19-07:00
Authentication code: 6789

Re: Split image into two

Post by menteith »

Thank you very much for your detailed answer. Please have a look at this output:
D:\split>dir /B
00002.jpg
00037.jpg
00038.jpg

D:\split>convert page000.jpg -crop 2x1@ +repage -colorspace gray +dither -colors 4
page000_%d.jpg *.jpg

D:\split>dir /B
00002.jpg
00037.jpg
00038-0.jpg
00038-1.jpg
00038-2.jpg
00038-3.jpg
00038.jpg

D:\split>
As you can see, I have 3 files:
00002.jpg
00037.jpg
00038.jpg
So, when they are splitted, I should have 3x2=6 files but I have only four:
00038-0.jpg
00038-1.jpg
00038-2.jpg
00038-3.jpg.

The last file have not been splitted.

Is it more clear now?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Split image into two

Post by snibgo »

Code: Select all

D:\split>convert page000.jpg -crop 2x1@ +repage -colorspace gray +dither -colors 4
page000_%d.jpg *.jpg
Is this a single command (after the DOS prompt)?

What is the "*.jpg" doing at the end?

Without that "*.jpg", the command should read one file called "page000.jpg" and create 2 files called "page000_0.jpg" and "page000_1.jpg".

But your dir command shows no files called "page000.jpg".
snibgo's IM pages: im.snibgo.com
menteith
Posts: 10
Joined: 2015-03-30T14:49:19-07:00
Authentication code: 6789

Re: Split image into two

Post by menteith »

Yes, this is one command.
Without that "*.jpg", the command should read one file called "page000.jpg" and create 2 files called "page000_0.jpg" and "page000_1.jpg".
Now everything is clear and works like a charm.
Is there a possibility to rewrite this code this code to work on every jpg in a catalog?
I wanted to achieve this by adding *.jpg at the end of the command.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Split image into two

Post by snibgo »

I would do it in a FOR loop. Type "help for" for help on "for". I would write the outputs to a different directory. Otherwise, when you process all the jpegs in a directory, you will soon split the split images, and then you will split the splits of the splits ...
snibgo's IM pages: im.snibgo.com
Post Reply