15

Please consider the following command for the imagemagick package:

sudo convert -resize 460x200 /path/to/test1.jpg /path/to/test2.jpg

Now test1.jpg is a fairly large image (12.5MB), but not unheard of sizes, but the server I am working from only has 1G of memory (Amazon EC2 t2.micro if it helps)

No error is being thrown, and yet no test2.jpg is being created. I have tested this with smaller images and it works fine. I would have thought 1G if memory would be plenty to manage a resize, but perhaps not?

What are my options, do I have to get more memory or am I missing something?

Any suggestions are very welcome!

Requested Edits

strace output shows: +++ killed by SIGKILL +++ along with alot of commands that look normal (opening /usr/share/local and reading long strings, which I assume is image data)

free gives me:

         total       used       free     shared    buffers     cached
Mem:       1016292     278348     737944       6412        460      13356
-/+ buffers/cache:     264532     751760
Swap:            0          0          0

So it looks as if swap is not enabled, so I will look into creating a swapfile to see if this sorts my issues.

File gives me:

testprint10Mbv2.jpg: JPEG image data, EXIF standard

I would also paste the full /path/to/img but unfortunately it contains sensitive data (client email addresses etc)

user.dz
  • 48,105
Aphire
  • 203
  • 1
    Why are you using sudo in this command? – Charles Green Jan 22 '16 at 17:15
  • 1
    Received "Killed" message when I didn't – Aphire Jan 22 '16 at 17:15
  • What s the output of uname -a? – Charles Green Jan 22 '16 at 17:18
  • 1
    Linux ip-xxx-31-25-220 3.13.0-48-generic #80-Ubuntu SMP Thu Mar 12 11:16:15 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux – Aphire Jan 22 '16 at 17:20
  • 1
    Weird - perhaps there is something in the "killed" error message that gives a clue as to the problem. – Charles Green Jan 22 '16 at 17:22
  • I did have a look-see and found that 'Killed' is from a daemon that kills memory-intensive processes, I tried a couple work-arounds but to no avail. :( – Aphire Jan 22 '16 at 17:23
  • You could try to replace convert with mogrify on a copy of your image. – Takkat Jan 22 '16 at 19:31
  • @Takkat, I did try this previously and got the same result – Aphire Jan 25 '16 at 12:17
  • 3
    Ypu can try to limit the memory used for the pixel cache with the argument -limit memory 64MiB (32MiB is a bit too small probably. Default is 1.5GiB). And it should give you some descriptive output in case it fails. – dadexix86 Jan 25 '16 at 12:46
  • I hardly dare to ask, but did you try to press F5 (refresh nautilus)? It happened to me several times that I did not see the converted or created file under certain circumstances, but it was nevertheless created. Shortage of memory seems to be a logical reason for this to happen. – Jacob Vlijm Jan 25 '16 at 13:24
  • @dadexix86 I will give this a shot and come back with any results I get when I am back in-office, thanks. – Aphire Jan 25 '16 at 15:00
  • @JacobVlijm I'm not running GUI, I'm doing the commands from putty and checking the file system from WinSCP FTP client, but Yeah I am refreshing WinSCP, good question though, thats caught me out before :P – Aphire Jan 25 '16 at 15:01
  • 2
    Try running the command with strace appended in front of the command, see what system calls are made, errors thrown. Let us know – Sergiy Kolodyazhnyy Jan 25 '16 at 21:04
  • Is there swap enabled on the system? I would like to see an [edit] that included the output of free and also file /path/to/test1.jpg – Elder Geek Jan 26 '16 at 00:48
  • @Aphire any news on this? – dadexix86 Jan 26 '16 at 10:25
  • @dadexix86 I tried what you suggested but unfortunately got the same outcome – Aphire Jan 26 '16 at 10:49
  • I can confirm adding a swap file has solved the issue. @ElderGeek If you would submit this as an answer I will gladly accept as your comment led me to find the correct answer. – Aphire Jan 26 '16 at 16:10
  • 2
    Done. I'm glad you got it resolved! the free output confirms that swap is off and the file info confirms that it's not a mis-identified file. Nice edit! +1 – Elder Geek Jan 26 '16 at 19:13
  • What are the dimensions of the image in pixels? "12.5Mb" does not tell much as it the size of compressed data which needs to be de-compressed to be resized. 10.000 x 10.000 pixels image would require 400Mb of RAM (4 bytes per pixel), and can be easily compressed to a few Mb if it is mostly solid color. – Sergey Jan 27 '16 at 03:03

2 Answers2

7

I'm assuming you are running out of RAM. You can verify this with watch cat /proc/meminfo while your process is running.

You may have insufficient RAM and/or SWAP to accomplish your task.

Due to the low-ram condition, insure that swap is on with swapon -a and if no swap is setup on the system create a partition for swap on unused diskspace and enable it. This will likely solve the problem. There's a good answer by @Takkat on how to do that here If you don't have access to partitioning the server you can also use a swapfile for swap

Elder Geek
  • 36,023
  • 25
  • 98
  • 183
7

The issue in the question was resolved by adding appropriate swap. For completeness let me give a summary of additional options we have when running Image Magick convert on huge files on low memory systems, or when there is no swap available, or the swap was too small.

The methods mentioned below are elaborated in detail in the Image Magick manual:

Really Massive Image Handling


Summary:

  1. Limit memory usage with option -limit

    By doing so Image Magick will create a temporary file for image handling as soon as the given memory limits were exceeded. This needs write permission for Image Magick on the temporary file directory. We can give any path where the temporary file will be created in an environment variable MAGICK_TMPDIR. An example command may look similar to this:

        env MAGICK_TMPDIR=/tempdir nice -5 convert -limit memory 32 -limit map 32 largefile.jpg -resize 640x320 smallfile.png
    

    Working on disk rather than in RAM will slow down the processing speed considerably.

  2. Work with "Memory Mapped Disk Files"

    Creating MPC files is resource demanding but it does not need so much resources to convert from MPC files. Therefore is may be put into consideration in case we need to convert the same source with different parameters several times. The workflow may then be similar to this:

    convert huge.jpg huge.mpc
    convert huge.mpc -resize 50% big.png
    convert huge.mpc -resize 20% small.png
    convert huge.mpc -resize 5% thumb.png
    
  3. Work on small sections of an image using stream

    Using stream claims to only process a part of the source image without the need to load the whole image into memory.

    stream -map rgb -storage-type char -extract 600x400+1900+2900 image.png - | convert -depth 8 -size 600x400 rgb:- tile.png
    

    In above example the -extract option takes size and offset values as defined by the Image Magick geometry. We will have to stick the tiles back together for getting the scaled image as a whole. Unfortunately stream does not work for all image formats but is supposed to work fine on JPEG images.

Takkat
  • 142,284