Comments (7)
Hello again,
You can do a range of things to limit libvips memory use:
- Don't use
resize
, usethumbnail
instead. It's much faster, needs less memory, and the quality is better. - Disable the libvips operation cache. libvips caches the result of the last 1,000 operations, or 100MB of memory, whichever is smaller. Use
pyvips.cache_set_max(0)
somewhere near the start of your program. - Shrink the libvips thread pool. libvips has a pool of worker threads, sized to fill your machine, and each worker has a set of memory buffers. Image shinking is not that CPU intensive, so threads don't really help much anyway. Do
export VIPS_CONCURRENCY=1
before running your program.
from pyvips.
I tried this program:
#!/usr/bin/python
import sys
import pyvips
pyvips.leak_set(True)
image = pyvips.Image.new_from_file(sys.argv[1], access='sequential')
image = image.resize(0.8 / 1.0)
image.write_to_file(sys.argv[2])
leak_set
makes libvips run a little slower, but it will check for leaks and report high-water memory usage. With a 10k x 10k pixel RGB image I see:
john@kiwi:~/try$ ./resize.py ~/pics/wtc.jpg x.jpg
memory: high-water mark 53.48 MB
If I change the program like this:
#!/usr/bin/python
import sys
import pyvips
pyvips.cache_set_max(0)
pyvips.leak_set(True)
image = pyvips.Image.thumbnail(sys.argv[1], 8000)
image.write_to_file(sys.argv[2])
I see:
john@kiwi:~/try$ VIPS_CONCURRENCY=1 ./resize.py ~/pics/wtc.jpg x.jpg
memory: high-water mark 33.82 MB
That's running with ulimit -m 256000
, which might be equivalent to your settings. I couldn't get it to crash.
from pyvips.
Sorry, I never answered your first question.
Out of memory errors are generally impossible to handle gracefully. Suppose memory is full and a fault is raised. What is going to handle the error? If no more memory can be used, no fault handler can execute. Like almost all software, libvips treats memory as an infinite resource and relies on the environment to manage it.
libvips does check large single allocations (for example, loading a 10gb image into memory), but does not check small ones, the reasoning being that, if a small allocation fails, the process is as good as dead.
from pyvips.
Thanks a lot. I tried your example code:
#!/usr/bin/python
import sys
import pyvips
pyvips.cache_set_max(0)
pyvips.leak_set(True)
image = pyvips.Image.thumbnail(sys.argv[1], 8000)
image.write_to_file(sys.argv[2])
but my output was like:
Traceback (most recent call last):
File "resize.py", line 20, in <module>
image = pyvips.Image.thumbnails(sys.argv[1], 800)
File "/home/munirh/venv/local/lib/python2.7/site-packages/pyvips/vimage.py", line 128, in call_function
return pyvips.Operation.call(name, *args, **kwargs)
File "/home/munirh/venv/local/lib/python2.7/site-packages/pyvips/voperation.py", line 132, in call
op = Operation.new_from_name(operation_name)
File "/home/munirh/venv/local/lib/python2.7/site-packages/pyvips/voperation.py", line 60, in new_from_name
raise Error('no such operation {0}'.format(operation_name))
pyvips.error.Error: no such operation thumbnails
VipsOperation: class "thumbnails" not found
I checked that I had vipsthumbnail
command available. So how to use it through python?
What would be the best documentation for these? I saw that thumbnail()
is there for pyvips.Image class but not the resize()
and vips_thumbnail()
is there for libvips API documentation. But how to get all for pyvips?
And where should I use ulimit -m 256000
in python to limit memory?
from pyvips.
File "resize.py", line 20, in <module>
image = pyvips.Image.thumbnails(sys.argv[1], 800)
It should be thumbnail
, of course, not thumbnails
, but I guess you fixed that.
Otherwise it's probably because you are using an old version of the libvips shared library. thumbnail
was added in 8.4 I think.
ulimit
is a shell command, so run that before you start python. It limits every process started from that shell.
from pyvips.
we are having same issue when creating large image files around 2 gb +
from pyvips.
Hello @abhijitgujar86,
Please open a new issue and include as much information as you can about the crash. Ideally, a reproducible test case.
from pyvips.
Related Issues (20)
- ICC Profile Application on SVS and tiffsave hangs randomly
- High RAM usage with fetch when reading multiple WSI tiles HOT 2
- Slower first run, even without cache HOT 2
- using resource.setrlimit makes pyvips / liborc spit the dummy HOT 2
- Memory leaking with pdf HOT 21
- dzsave_target returns NoneType not list[] as in docs HOT 1
- Pyvips fetch returns bad image on .svs files HOT 4
- Reading in image a second time breaks on qptiff file HOT 6
- Documentation link to conda package HOT 1
- Create svs file with image pyramid, label and macro in pyvips? HOT 4
- Image merge stitch HOT 2
- `addalpha` has different behaviour compared to C HOT 1
- Error installing pyvips on Mac OS HOT 15
- draw_rect fill error HOT 2
- creating tiles of 16bit multiband (4 band) tiff image leaves black canvas HOT 4
- class "jxlsave" not found HOT 9
- Assertion failed: sp->cinfo.comm.is_decompressor HOT 3
- Help installing pyvips for jupyter HOT 5
- `TIFFFillTile: Read error` when writing a TIFF file on a per-tile basis. HOT 2
- Thumbnail creation taking too long HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyvips.