散景旋转图像块底层图像

Bokeh rotated image blocks underlying image

我将旋转后的图像放在同一图中不同锚点的另一幅图像之上。然而,顶部图像部分覆盖底部图像,如下所示。有没有办法去除旋转图像的黑边?

示例代码在这里:

from bokeh.server.server import Server
from bokeh.application import Application
from bokeh.application.handlers.function import FunctionHandler
from bokeh.plotting import figure, ColumnDataSource, show
from bokeh.layouts import column
from bokeh.models.tools import PanTool, BoxZoomTool, WheelZoomTool, \
UndoTool, RedoTool, ResetTool, SaveTool, HoverTool

import numpy as np
from collections import namedtuple
from scipy import ndimage

def make_document(doc):

    p = figure(match_aspect=True)

    Anchor = namedtuple('Anchor', ['x', 'y'])
    img1 = np.random.rand(256, 256)
    anchor1 = Anchor(x=0, y=0)

    img2= np.random.rand(256, 256)
    anchor2 = Anchor(x=100, y=100)

    img2 = ndimage.rotate(img2, 45, reshape=True)
    p.image(image=[img1], x=anchor1.x, y=anchor1.y, 
            dw=img1.shape[0], dh=img1.shape[1], palette="Greys256")
    p.image(image=[img2], x=anchor2.x, y=anchor2.y, 
            dw=img2.shape[0], dh=img2.shape[1], palette="Greys256")
    doc.add_root(column(p, sizing_mode='stretch_both'))

apps = {'/': make_document}

server = Server(apps)
server.start()
server.io_loop.add_callback(server.show, "/")
try:
    server.io_loop.start()
except KeyboardInterrupt:
    print('keyboard interruption')
print('Done')

当您旋转图像时,新的空白区域(图像上的黑色三角形)默认初始化为 0(查看 modecval 选项 https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.rotate.html ).

如果您确定某个值永远不会在图像中使用,则可以将其作为 cval 传递。然后,您应该能够手动创建一个颜色映射器,将该值映射到透明像素并使用映射器而不是调色板(arg 名称为 color_mapper)。

如果您没有这样的值,那么您将不得不使用 image_rgba 并确保您决定使用的任何 cval 都会产生透明像素。