libvips / pyvips

python binding for libvips using cffi
MIT License
649 stars 50 forks source link

Crop empty image spaces #495

Open sinamcr7 opened 3 months ago

sinamcr7 commented 3 months ago

is there an option to crop image before saving so the empty spaces with no useful info are removed to reduce file size? as I'm creating big array at start I need to crop image so empty spaces are removed to save space?

jcupitt commented 3 months ago

Hello @sinamcr7,

Yes, have a look at find_trim:

https://www.libvips.org/API/current/libvips-arithmetic.html#vips-find-trim

$ python
Python 3.12.3 (main, Jul 31 2024, 17:43:48) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyvips
>>> x = pyvips.Image.new_from_file("lion.png")
>>> x.width
425
>>> x.height
425
>>> x.find_trim()
[34, 41, 250, 381]
>>> 
sinamcr7 commented 3 months ago

awesome thanks