keep image in ram

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
robuntu
Posts: 41
Joined: 2012-01-22T10:56:10-07:00
Authentication code: 8675308

keep image in ram

Post by robuntu »

Hi,
I have to perform multiple action with one large image file (the compressed Tiff is >1GByte)
Can I load the image into ram and do several actions with this one file interactive?

I know I can perform several actions with image sequence operators, but I have to interact
with a user in between via php.

E.g. I read the file and produce a "jpg-thumbnail" with 1000x1000 pixels as a preview (this takes 25 Seconds)
I display the preview.
Then a user can decide to split the original file into X slices.
Then I slice the original into X slices with "crop"
I need the interaction with the user to know how many slices I have to cut and to calculate the size of one slice.
When I use the convert -crop the file has to be loaded again (20 seconds) to get sliced.
It would be great, if this loading could be avoided by using the loaded and kept image from the first conversion(to the jpg-thumbnail).
Is there a chance to get this feature?
(I know it is in my responsibility to dispose the image after conversion to free the memory again)

Thanks
Roland
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: keep image in ram

Post by magick »

Save your intermediate files in the MPC format (e.g. convert logo: logo.mpc). Reading the MPC has near-zero overhead.
robuntu
Posts: 41
Joined: 2012-01-22T10:56:10-07:00
Authentication code: 8675308

Re: keep image in ram

Post by robuntu »

Reading the MPC has near-zero overhead.
True, but writing and reading 4GB from a harddisk is still taking unnecessary seconds.
I have plenty of Ram, I just wonder if I just write the MPC file to an 8GB ramdisk...

What do you think?

Roland
robuntu
Posts: 41
Joined: 2012-01-22T10:56:10-07:00
Authentication code: 8675308

Re: keep image in ram

Post by robuntu »

Just tried it!
Great, converting the Tiff file into an MPC, which is written into a ramdisk is faster then any other action.
Using this ram located MPC file is faster than anything I have ever experienced in image processing before.
Great. If no one tells me a better way of keeping an image in ram, I call my problem "solved"
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: keep image in ram

Post by fmw42 »

For certain kinds of processing that is all in one command line, you can put your image into memory in .mpr format.

See
http://www.imagemagick.org/Usage/files/#mpr
http://www.fmwconcepts.com/imagemagick/ ... composite1
http://www.fmwconcepts.com/imagemagick/ ... und_shadow
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: keep image in ram

Post by anthony »

robuntu wrote:Just tried it!
Great, converting the Tiff file into an MPC, which is written into a ramdisk is faster then any other action.
Using this ram located MPC file is faster than anything I have ever experienced in image processing before.
Great. If no one tells me a better way of keeping an image in ram, I call my problem "solved"

The reason it is faster is the it is a direct memory dump. No data parsing and formating, just write it ASIS.

When reading the disk image is just 'mounted' into memory as needed, much like swap space. Again no parsing of processing of the data is needed.

The draw back is that you can't use it for image storage for any length of time as it is dependant on the specific compilation of the Imagemagick that wrote it. Different version, build, or machine may not read it, and even cause sever memory faults. But for temporary save and re-read using the same program, no problems, perfect for temporary files.



Keeping images in Memory...The future using IMv7

IMv7 will allow something called co-processing. That is you run a IM program continuously in the background which holds the images in memory, and you can feed it operations and get feedback via a controlling program using data pipes. That is the shell, or PHP, can use IM to hold images, and processes though images by sending commands to it, The parent or control program can make decisions as to what to do or perform looped operations as needed.

See http://www.imagemagick.org/Usage/bugs/I ... ipting.txt (near the bottom for a few examples) for the working plan of what I am trying to implement. I am working on the main processing loop for this now.

Comments welcome.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
robuntu
Posts: 41
Joined: 2012-01-22T10:56:10-07:00
Authentication code: 8675308

Re: keep image in ram

Post by robuntu »

Another idea...

When using php to use image magick
I load the image with Imagick::readImage
then I do my modifications and then I
write it back to disk with Imagick::writeImage

Would this work as well?
Even faster? (because the Image is kept in ram - is it really?)

I have to interact with a user anyway in between some stepts of image modifications ,
so writing a php script is a job I have to do anyway.
As I said, I have plenty of ram, and installing a ramdisk is probably even more ram intensive than
the php-keep-in-ram solution.

Greetings
Roland

@anthony: IMv7 sounds great for my pupose. But I have to produce a stable solution within the next two weeks...
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: keep image in ram

Post by magick »

See http://www.imagemagick.org/MagickStudio. We save all intermediate images in the MPC format.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: keep image in ram

Post by anthony »

Interactive web interfaces is much harder, as these are 'event driven' over long periods of time (user speed, not computer speed).

Essentually you can never be sure when the user will next 'do somthing'. Actions (events) may happen after just a few seconds, or they may stop and continue after a period of minutes, or even hours. You may also have a hundred active users similtaniously working on their own image.

In such as situation keeping images in memory for web work really is not a good idea. Either save images in a temprary area using MCP for speed, or save them into a database, which will cache recent and active work to avoid disk IO.

Alturnativally, rather than store images, store a base image and what actions the user has applied to an image. This has the advantage that users can 'undo' actions, or restart from the base image with a completely different set of actions.
This means you are not saving images over and over (saving is usually more expensive), making reloads even faster as you already have a MCP cached image ready as a start point. When the user requests it, or on some other condition, the actions can be rolled together and a new 'start image' created.

In all cases the work should have a timeout and automatic cleanup, as you don't want to be holding all working images of the last year, or ten years, and use up all your disk space!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
robuntu
Posts: 41
Joined: 2012-01-22T10:56:10-07:00
Authentication code: 8675308

Re: keep image in ram

Post by robuntu »

Yes, I am convinced!
I will not use the php-imagemagick.
I will store the Image in MPC on a ramdrive and use the console imagemagick commands to do my modifications.
But it was worth a thought :D

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

Re: keep image in ram

Post by fmw42 »

You can use PHP with exec() to run commands as if they were being run from the command line. You can even run bash shell scripts with IM commands in them via exec(). That is if you need to use PHP for a web interface.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: keep image in ram

Post by anthony »

The PHP Imagick interface will work fine with reading saving and modifying MPC images, though their are more examples and help when using the CLI instead.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply