Cut large image on tiles

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
graceman9
Posts: 5
Joined: 2011-03-17T23:20:07-07:00
Authentication code: 8675308

Cut large image on tiles

Post by graceman9 »

Hi All, let me explain what I mean.

My image 16384x16384 size.
My calculations: memory = 16384 x 16384 * 4 /*rgba*/ = 1073741824 byte = 1024 Mb // Is there something wrong?

I want cut it for tiles!
I try:

Code: Select all

convert src.img -crop 256x256 tile.png
This work for smaller images, but not for my-big-image.
My comp is 4Gb of RAM memory.
According to my calculations, the memory is enough, but the real code shows the opposite..

Therefore, I want do this, like in simple graphics editor:
1) load image (once load big-image = 1 Gb)
2) select region 256x256
3) copy & paste it in new image 256x256 size
4) repeat 1-3 actions for every region using offset, and we get - tiles!

How can I do this via ImageMagick? Or how can I do this anyway?
Sorry for my english.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Cut large image on tiles

Post by anthony »

Yes. 16bit means double that byte figure. Also add profiles or other meta-data which can be substantial. Then you have copies that are made during many of the image processing operations. For example when cropping a single sub image you will at least have the number of pixel in that sub pixel as a duplicate, for at least a period of time.

This is not counting any duplication of data that a library such as the JPEG compression algorithm may need.

Using a MPC saved file image will not use memory but will be read, as needed directly from disk, but any extra images created during processing will be in memory.


Also just because a machine has 4G of memory does not mean all that memory is available. Kernel, buffers and other programs can also take a fair percentage, though under linux thing not running will get swapped out.

On my own machine right this minute I am not using IM, but I am running a torrent, emule, and web browser, (no fancy desktop effects though) and my system monitor is showing on my 3Gb available memory, 250 Mbyes are already being used. No swap at this point though.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Cut large image on tiles

Post by magick »

Add -debug cache to your command line. It will tell you whether ImageMagick is caching pixels to disk or memory. Caching to disk will tend to slow down the performance of the conversion.
graceman9
Posts: 5
Joined: 2011-03-17T23:20:07-07:00
Authentication code: 8675308

Re: Cut large image on tiles

Post by graceman9 »

Thanks for the explanation, I solved the problem:

Code: Select all

convert src.png src.mpc # create full copy on hard drive, use it for better performance
And my small script just for remember:

Code: Select all

#!/bin/bash

# USE THIS SCRIPT ON OWN RISK

if [ -z "$1" -o ! -f "$1" -o -z "$2" ]; then
  echo "Invalid parameters"
  exit
fi

if [ "$2" -gt 21 -o "$2" -lt 1 ]; then
  echo "Invalid zoom"
  exit
fi

src=$1
z=$2
width=`identify -format %w $src`
limit=$[$width / 256]
echo "count = $limit * $limit = "$((limit * limit))" tiles"
limit=$((limit-1))
#~ k=1
for x in `seq 0 $limit`; do
  for y in `seq 0 $limit`; do
    #~ if [ $k -gt 99 ]; then
      #~ break;
    #~ fi
    # convert
    time1=$(($(date +%s%N)/1000000))
    tile=tiles/tile-$z-$x-$y.png
    echo -n $tile
    w=$((x * 256))
    h=$((y * 256))
    convert $src -crop 256x256+$w+$h $tile
    time2=$(($(date +%s%N)/1000000))
    dtime=$((time2-time1))
    echo " - "$dtime"ms"
    #~ k=$((k+1))
  done
done
HeiHon
Posts: 5
Joined: 2011-03-15T07:58:14-07:00
Authentication code: 8675308

Re: Cut large image on tiles

Post by HeiHon »

When you have to deal with big images a lot, may be vips could help
http://www.vips.ecs.soton.ac.uk/index.php?title=VIPS
VIPS is a free image processing system. It is good with large images (images larger than the amount of RAM you have available), with many CPUs
Post Reply