Keep transparency with basemap warpimage

断了今生、忘了曾经 提交于 2019-12-10 18:25:33

问题


I have an RGBA png map with all oceans transparent.

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.

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()

回答1:


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.



来源:https://stackoverflow.com/questions/36885953/keep-transparency-with-basemap-warpimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!