autocrop function

Hi all,

I am looking for an autocrop function.

It should remove borders from an image.
Is it available in matplotlib ?

Any pointer would be appreciated.

Thanks in advance

                                                Nils

I wasn’t able to find one in Matplotlib but you can use PIL library for all the imaging work. Really easy to use.
Here is the webpage for it: http://www.pythonware.com/products/pil/index.htm

Anton

···

From: Nils Wagner <nwagner@…1052…>
To: matplotlib-users@lists.sourceforge.net
Sent: Tuesday, June 9, 2009 10:22:05 AM
Subject: [Matplotlib-users] autocrop function

Hi all,

I am looking for an autocrop function.

It should remove borders from an image.
Is it available in matplotlib ?

Any pointer would be appreciated.

Thanks in advance

                                            Nils

Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing
server and web deployment.
http://p.sf.net/sfu/businessobjects


Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi Anton,

Thank you for your prompt reply.
I am aware of PIL.
However I didn't find an autocrop function within PIL.

Cheers,
                    Nils

from PIL import Image
im = Image.open('test.png')

···

On Tue, 9 Jun 2009 11:31:19 -0700 (PDT) Anton Vasilescu <vasilescu_anton@...9...> wrote:

I wasn't able to find one in Matplotlib but you can use PIL library for all the imaging work. Really easy to use. Here is the webpage for it: http://www.pythonware.com/products/pil/index.htm

Anton

#
# Calculates the bounding box of the non-zero regions in the image.
# The bounding box is returned as a 4-tuple defining
# the left, upper, right, and lower pixel coordinate.
# If the image is completely empty, this method returns None.
#
print im.getbbox()

print im.size
#
# Returns a rectangular region from the current image.
# The box is a 4-tuple defining the left, upper, right, and lower
#
box = (100, 100, 800, 800)
region = im.crop(box)
region.show()
  
It would be nice to compute the box automatically.
Any idea ?

If you're using very recent version of mpl, you may try savefig with
"bbox_inches" option.

savefig("a.png", bbox_inches="tight")

The algorithm is not perfect, but will work for most of simple plots.

Regards,

-JJ

···

On Tue, Jun 9, 2009 at 1:22 PM, Nils Wagner<nwagner@...1052...> wrote:

Hi all,

I am looking for an autocrop function.

It should remove borders from an image.
Is it available in matplotlib ?

Any pointer would be appreciated.

Thanks in advance

                                           Nils

------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hm, it doesn't work as expected. Am I missing something ?
The example is attached.

Cheers,
                   Nils

example.tar.bz2 (113 KB)

···

On Wed, 10 Jun 2009 02:26:54 -0400 Jae-Joon Lee <lee.j.joon@...287...> wrote:

If you're using very recent version of mpl, you may try savefig with
"bbox_inches" option.

savefig("a.png", bbox_inches="tight")

The algorithm is not perfect, but will work for most of simple plots.

Regards,

-JJ

I guess I misunderstood your intention.
As you posted the message in the mpl list, I assumed that you want to
crop out the boundary of the mpl figure, which seems to be not the
case.
Sorry for the noise.

-JJ

···

On Wed, Jun 10, 2009 at 12:09 PM, Nils Wagner<nwagner@...1052...> wrote:

On Wed, 10 Jun 2009 02:26:54 -0400 > Jae-Joon Lee <lee.j.joon@...287...> wrote:

If you're using very recent version of mpl, you may try savefig with
"bbox_inches" option.

savefig("a.png", bbox_inches="tight")

The algorithm is not perfect, but will work for most of simple plots.

Regards,

-JJ

Hm, it doesn't work as expected. Am I missing something ?
The example is attached.

Cheers,
Nils

Please find attached the result of gimp's autocrop function.
That is the target.

Cheers,
                    Nils

autocrop.png

···

On Wed, 10 Jun 2009 16:32:08 -0400 Jae-Joon Lee <lee.j.joon@...287...> wrote:

I guess I misunderstood your intention.
As you posted the message in the mpl list, I assumed that you want to
crop out the boundary of the mpl figure, which seems to be not the
case.
Sorry for the noise.

-JJ

Hi,

I found a way to run gimp (gimp2.2) in batch mode.

gimp --batch-interpreter plug_in_script_fu_eval -i -d -b '(script-autocrop "/home/nwagner/test.png")' '(gimp-quit 0)'

Now I would like to run gimp from python using subprocess, popen2 or os.system.

How do I call gimp with the arguments from python ?

Nils

script-autocrop.scm (346 Bytes)

···

On Wed, 10 Jun 2009 16:32:08 -0400 Jae-Joon Lee <lee.j.joon@...287...> wrote:

I guess I misunderstood your intention.
As you posted the message in the mpl list, I assumed that you want to
crop out the boundary of the mpl figure, which seems to be not the
case.
Sorry for the noise.

-JJ