Basemap white space

Sorry, my bad. I always hit reply without checking... it is not the
first time did that,

Here is the solution for the list sake:

""" trim image """
import StringIO, Image
imgdata = StringIO.StringIO()
fig.savefig(imgdata, dpi=300, format='png')
imgdata.seek(0)
im = Image.open(imgdata)

def trim(im, border):
  from PIL import ImageChops
  bg = Image.new(im.mode, im.size, border)
  diff = ImageChops.difference(im, bg)
  bbox = diff.getbbox()
  if bbox:
      return im.crop(bbox)
  else:
      # found no content
      raise ValueError("cannot trim; image was empty")

im = trim(im,'white')
im.show()

The StringIO trick is a copy-and-paste from the matplotlib faq. And the
trim function I got from here:

http://mail.python.org/pipermail/image-sig/2008-July/005092.html

Thanks for the discussion, I learned a lot abouth the Agg backend.

BTW: What I meant by limitation is the fact that Agg has no GUI like the
nice QT window I was using before. The users of this script have no
experience with scripting languages and enjoyed choosing the format and
filename using a GUI interface.

In addition, PIL's show() use an external linux program "xv". In the end
I wanted to eliminate one external linux program (Imagemagik convert)
but ended up with another one...

Best, Filipe

···

On 04/06/2010 05:01 PM, Friedrich Romstedt wrote:

2010/4/5 Filipe Fernandes <ocefpaf@...287...>:

Thanks a lot. In the end the "StringIO-solution" worked fine. The only
limitation is that this works only for the Agg backend.
[...]

I'm happy to hear this. As far as I know, using the Agg backend is
not a limitation, because it provides fully anti-aliased output.
Besides being available on all platforms (?).

Let me make a small hint: The matplotlib-users mailing list is
configured that by default replies go to the sender only, not to
matplotlib-users, so maybe you want to let the list know about your
solution?

Anyway I'm happy for your Thanks,
so thanks too,
Friedrich

2010/4/8 Filipe Fernandes <ocefpaf@...287...>:

BTW: What I meant by limitation is the fact that Agg has no GUI like the
nice QT window I was using before. The users of this script have no
experience with scripting languages and enjoyed choosing the format and
filename using a GUI interface.

I'm quite convinced that you can have the figure in multiple Canvases
at the same time. Or create the FigureCanvasAgg or the call to
.savefig() only on save time. In every widget framework, it should be
fairly easy to build a custom gui having a customised "Save ..."
button, I guess?

In addition, PIL's show() use an external linux program "xv". In the end
I wanted to eliminate one external linux program (Imagemagik convert)
but ended up with another one...

When using Tkinter, you can use ImageTk:

im = {some PIL Image} # Some image to be shown
canvas = {some Tkinter.Canvas} # For drawing graphics

viewport = ImageTk.PhotoImage(im)

canvas.create_image((0, 0), image = viewport, anchor = 'nw') # Put
the image on the Tkinter.Canvas. Modify according your needs.

# Now the PIL Image has been rendered on the canvas.

# Maybe do canvas.update() or call the master's .update(),
# or call some .mainloop() entry function. For instance, use the
# Tkinter.Tk instance's method .mainloop().

When renewing the graphics, don't forget to canvas.delete({tag}) using
the {tag} returned by canvas.create_image(), otherwise you will
probably loose perfomance.

There are examples on matplotlib.sourceforge.net how to use Tkinter
with matplotlib.

hth,
Friedrich

Thanks for point TKinter to me. However, I'm stuck again.

I've tried two approaches, one is following what you suggested:

""" Tkinter """
import Tkinter as tk
root = tk.Tk()
from PIL import Image, ImageTk
image = ImageTk.PhotoImage(Image.open('map.png')) # load saved image
#image = ImageTk.PhotoImage(im) # load image from StringIO
tk.Label(root, image=image).pack()

The other is to convert the PIL image to array and show with imshow()

""" PIL to array """
from matplotlib.image import pil_to_array
rgba = pil_to_array(Image.open('map.png')) # load saved image
#rgba = pil_to_array(im) # load image from StringIO
rgba = rgba.astype(np.float32)/255.
imshow(rgba)

Both work fine for a saved image. However, they return strange errors
when I tried to apply them to the image from "StringIO", or even for a
saved image in the same script used to generate them. I guess that
something I import from matplotlib is causing a conflict here.

Here are the error messages:

""" Tkinter """
_tkinter.TclError: image "pyimage9" doesn't exist

""" PIL to array """
terminate called after throwing an instance of 'char const*'

I'm attaching the script.
Aborted

make-map.py (1.49 KB)

2010/4/12 Filipe Pires Alvarenga Fernandes <ocefpaf@...287...>:

Thanks for point TKinter to me. However, I'm stuck again.

I've tried two approaches, one is following what you suggested:

""" Tkinter """
import Tkinter as tk
root = tk.Tk()
from PIL import Image, ImageTk
image = ImageTk.PhotoImage(Image.open('map.png')) # load saved image
#image = ImageTk.PhotoImage(im) # load image from StringIO
tk.Label(root, image=image).pack()

The other is to convert the PIL image to array and show with imshow()

""" PIL to array """
from matplotlib.image import pil_to_array
rgba = pil_to_array(Image.open('map.png')) # load saved image
#rgba = pil_to_array(im) # load image from StringIO
rgba = rgba.astype(np.float32)/255.
imshow(rgba)

Both work fine for a saved image. However, they return strange errors
when I tried to apply them to the image from "StringIO", or even for a
saved image in the same script used to generate them. I guess that
something I import from matplotlib is causing a conflict here.

Here are the error messages:

""" Tkinter """
_tkinter.TclError: image "pyimage9" doesn't exist

""" PIL to array """
terminate called after throwing an instance of 'char const*'

Hi,

with the Tkinter PhotoImage, I think it's intended for use with
Tkinter.Canvas, and not compatible with Label. For what I know, it
works fine for me with Canvas (Windows). I guess what you end up with
is not a real PhotoImage but something faky.

Now, I cannot execute your script and dive in myself, because I
switched to another laptop, where I have to compile everything ... So
in some days, I will know more.

With the pil_to_image issue, I really have no idea. It's really quite
strange, isn't it? I mean, it should return a newly created
numpy.ndarray, and shouldn't borrow any memory with the PIL Image? I
really don't know at the moment, I'm sorry.

For the Tkinter issue, from your script it seems that you simply want
to display it, so give the Canvas a try. I used it in

, start with update() and __init__().

hth for now,
Friedrich