I have an image as below. It is 2579*2388 pixels. Lets assume that it's bottom left corner is at 0,0. From that image I want to create multiple images as follows and save them in the working folder. Each image will have size of 100*100 pixels. Each image will be saved by it's bottom left hand coordinates.

  1. first image will have its bottom left hand corner at 0,0. Top right hand corner will be at 100,100 and the image will be saved as 0-0.jpg
  2. second image will have its bottom left hand corner at 10,0. Top right hand corner will be at 110,100 and the image will be saved as 10-0.jpg
  3. Once the bottom row is completed, Y coordinate will move by 10. In case of second row, the first image will be at 0,10 and that image will be saved as 0-10.jpg

what is the fastest way to do this? I would prefer a command line approach. But to begin, i am okay with any type of tool.

I understand that in the case of the current image, it will split it into around 257*238 images. But I have sufficient disk space and i need each image to perform text detection.

enter image description here

  • Have you looked at ImageMagick's convert -crop? What have you tried so far? – John1024 Apr 23 '15 at 23:48
  • Just to be sure; the sub-images are largely overlapping, right? – Janis Apr 23 '15 at 23:49
  • @Janis yes. images will be overlapping – user2543622 Apr 23 '15 at 23:51
  • @John1024 I was trying this in R and couldnt get it to work...WOuld it be possible to provide some sample code for imagemagick? if i get some starting coded then i can develope further – user2543622 Apr 23 '15 at 23:52

Here is shell code (bash, ksh, or zsh) that may do what you want:


size=$( identify -ping -format "%wx%h" "${image}" )


for ((x=0; x<x_upb; x+=x_inc))
    for ((y=0; y<y_upb; y+=y_inc))
        convert "${image}" -crop "${x_tile}x${y_tile}+${x}+${y}" "$x-$y.jpg"

The program convert is from the ImageMagick program suite. Since it's also possible to automatically extract the picture size from the picture files I've added that step as well. Following don_crissti's comment, I'm as well proposing using identify for that purpose (it's also, conveniently, from the ImageMagick program suite). You can of course also hard-code the picture size in x_upb and y_upb, and also adjust the tile size and tile shift increment as needed. I hope and think the code is self explanatory.

Note: It certainly needs some tweaks; e.g. the tiles shall probably not go beyond the upper bounds (or you'd get smaller sub-pictures if you're close to the borders), so you'd need to subtract the tile size from the upper bounds if you want equal-sized sub-images across the whole image.

Note: As far as I recall, the 'geometry' is defined to have an orientation starting with (0,0) in the upper left corner. If you want some other orientation you'd need to do the math yourself to fit that interface definition. The usual code patterns for that are to address the respective coordinates depending on the upper bound, e.g., $((y_upb - y)), were y would be the variant part, the (loop-)variable.

  • is there a way to set bottom left hand corner as 0-0? I tried and it worked...thanks – user2543622 Apr 24 '15 at 16:47
  • As far as I recall, the 'geometry' is defined to have an orientation starting with (0,0) in the upper left corner. I appended an additional note to my answer how to address that. – Janis Apr 25 '15 at 11:56
  • @don_crissti; It's a separate program call anyway, so using either one of the tools is okay, I'd say. Okay you save the awk instance, so also +1 from my side. (The reason why I used exiftool was just because I used it before for various other tasks and so it immediately came to my mind.) Getting the dimensions of the source file was anyway not the topic of the question, I just added it for convenience proactively. – Janis Apr 25 '15 at 12:44
  • @don_crissti; Right and valid. Please note that I already noted your suggestion as part of my answer. – Janis Apr 25 '15 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.