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
# -*- coding: utf-8 -*-
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import StringIO, Image

#matplotlib.use('Agg')
matplotlib.use('TkAgg')

from mpl_toolkits.basemap import Basemap

lonmin = -87.5
lonmax = -22.5
latmin = -59.5
latmax =  14.5

m = Basemap( projection = 'merc', llcrnrlat = latmin, urcrnrlat = latmax, llcrnrlon = lonmin, urcrnrlon = lonmax, resolution = 'c' )
fig  = plt.figure()
ax   = fig.add_subplot(111)
m.ax = ax
m.bluemarble()

""" trim image """
imgdata = StringIO.StringIO()
fig.savefig(imgdata, dpi=150, format='png')
imgdata.seek(0)  # rewind the data
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')

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

""" error when used in the script:
terminate called after throwing an instance of 'char const*'
Aborted"""

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

""" error when used in the script:
_tkinter.TclError: image "pyimage9" doesn't exist """
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to