Cartopy negates background images on some projections

164 views
Skip to first unread message

Matthew Mizielinski

unread,
Jul 5, 2013, 10:40:41 AM7/5/13
to scitoo...@googlegroups.com

I've been using cartopy to produce multi layered animations of model output, but have hit on an odd issue when using a background image in projections other than PlateCarree; the background image is negated.

A code example is shown below and its output are attached; it downloads an image and plots it using the PlateCarree and Orthographic projections as the image is read in (top row) and after image negation (bottom row).

I can't see a dumb mistake in my code so I presume that something in cartopy needs tweaking?


import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import os
from matplotlib.image import imread
import urllib

#retrieve background image to TMPDIR directory
target
= os.environ['TMPDIR']+'/world.topo.bathy.200407.3x5400x2700.jpg'
if not os.path.exists(target):
    source
= 'http://eoimages.gsfc.nasa.gov/images/imagerecords/73000/73751/world.topo.bathy.200407.3x5400x2700.jpg'
   
print "retrieving image from ", source
    urllib
.urlretrieve(source, target)


img
= imread(target)
img
= img[::-1]
fig
= plt.figure()
img_neg
= 255 - img
for i,p in enumerate([ccrs.PlateCarree(), ccrs.Orthographic()]):
 
  subplt
= fig.add_subplot(2,2,i+1, projection=p)
  plt
.imshow(img, origin='lower', transform=ccrs.PlateCarree(), extent=[-180, 180, -90, 90], zorder=0)
  subplt
= fig.add_subplot(2,2,i+3, projection=p)
  plt
.imshow(img_neg, origin='lower', transform=ccrs.PlateCarree(), extent=[-180, 180, -90, 90], zorder=0)

plt
.show()



background_funny.png

bjlittle

unread,
Jul 9, 2013, 3:48:20 AM7/9/13
to scitoo...@googlegroups.com
Hi Matthew,

I think that this is a known issue that I've also seen before. If I recall correctly it's to do with the fact that the image source is a *.jpg and how Cartopy handles it.

i.e. Cartopy is making some broad brush assumptions about the type of the image, which isn't necessarily true, thus causing the negation.

Can you try the same again but with a *.png ?

Regards,
-Bill


Matthew Mizielinski

unread,
Jul 9, 2013, 5:41:02 AM7/9/13
to scitoo...@googlegroups.com
Converting the background to a png file works as expected (i.e. fixes the issue).

Is it safe to assume that this will be fixed in the next version of cartopy? (png files can be a lot larger than jpegs)

TIA,

Matthew

bjlittle

unread,
Jul 9, 2013, 1:48:30 PM7/9/13
to scitoo...@googlegroups.com
Hi Matthew,

I've created Cartopy issue https://github.com/SciTools/cartopy/issues/299.

As far as I know the latest version of Cartopy v0.8.0 has already been cut and is ready for deployment, so unfortunately the fix for this clearly won't make that release.

Reply all
Reply to author
Forward
0 new messages