Vince
Vince

Reputation: 336

Keep transparency with basemap warpimage

I have an RGBA png map with all oceans transparent.

enter image description here

I want to use Basemap in a north pole stereographic projection and this map with warpimage. The transparency is lost and replaced by black when I want to keep it. What can i do ? My final goal is to plot a color grid in oceans and then the transparent map above it.

enter image description here

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

m = Basemap(projection='npstere',boundinglat=60,lon_0=300,resolution='l',round=True)
m.drawlsmask(ocean_color='white')
m.warpimage("land.png")
plt.show()

Upvotes: 3

Views: 854

Answers (1)

ptrj
ptrj

Reputation: 5212

I think it's a bug in function warpimage. If the projection is not cylindrical the original image has to be transformed and this is the place where transparency is lost. Namely, in file mpl_toolkits/basemap/__init__.py in function warpimage:

1> 4141                 for k in range(3):
   4142                     self._bm_rgba_warped[:,:,k],x,y = \
   4143                     self.transform_scalar(self._bm_rgba[:,:,k],\
   4144                     self._bm_lons,self._bm_lats,nx,ny,returnxy=True)

Here, RGB channels (for k=0,1,2) from the original file (self._bm_rgba) are transformed and passed to the warped image but the alpha channel (with index k = 3) is not.

Solution

If you can modify your python distribution, locate file mpl_toolkits/basemap/__init__.py, find a function warpimage and change line for k in range(3): (around line 4141 as in the code above) to

for k in range(self._bm_rgba.shape[2]):

This fixes the problem.

Line numbers may differ. I use basemap-1.0.7.


By the way, I used your jpg file for testing and noticed it didn't have the alpha channel. I hope you are aware of it.

Update

Actually, this bug was already fixed in the github version of basemap.

Upvotes: 1

Related Questions