66
77app = None
88
9- def PyAggImagePhoto (photoimage , data , mode ):
9+ def PyAggImagePhoto (photoimage , data_as_str , mode , bbox_as_str = None ):
1010 interp = PyAggImagePhoto .interp
1111 if not tklib .Tk_MainWindow (interp ):
1212 raise _tkinter .TclError ("this isn't a Tk application" )
1313 photo = tklib .Tk_FindPhoto (interp , photoimage )
1414 if not photo :
15- tklib .Tcl_AppendResult (interp , "destination photo must exist" , 0 )
15+ tklib .Tcl_AppendResult (interp , "destination photo must exist" , 0 )
1616 return tklib .TCL_ERROR
17- img = FromTclStringNDArray (data )
18- has_bbox = False
17+ data = FromTclStringNDArray (data_as_str )
18+ if bbox_as_str :
19+ try :
20+ bbox = FromTclStringNDArray (bbox_as_str )
21+ except :
22+ tklib .Tcl_AppendResult (interp , "bbox not valid" , 0 )
23+ return tklib .TCL_ERROR
24+ destx = int (bbox [0 , 0 ])
25+ desty = data .shape [0 ] - int (bbox [1 , 1 ])
26+ destwidth = int (bbox [1 , 0 ] - bbox [0 , 0 ])
27+ destheight = int (bbox [1 , 1 ] - bbox [0 , 1 ])
28+ deststride = 4 * destwidth ;
29+ destbuffer = np .empty ([destheight , destwidth , 4 ], dtype = 'uint8' )
30+ has_bbox = True
31+ destbuffer [:,:,:] = data [desty :desty + destheight , destx :destx + destwidth ,:]
32+ else :
33+ has_bbox = False
34+ destbuffer = None
35+ destx = desty = destwidth = destheight = deststride = 0 ;
1936 pBlock = tkffi .new ('Tk_PhotoImageBlock[1]' )
2037 block = pBlock [0 ]
2138 block .pixelSize = 1
2239 if mode == 0 :
2340 block .offset [0 ] = block .offset [1 ] = block .offset [2 ] = 0
2441 nval = 1
25- else :
42+ else :
2643 block .offset [0 ] = 0
2744 block .offset [1 ] = 1
2845 block .offset [2 ] = 2
@@ -37,25 +54,25 @@ def PyAggImagePhoto(photoimage, data, mode):
3754 block .width = destwidth
3855 block .height = destheight
3956 block .pitch = deststride
40- block .pixelPtr = destbuffer
57+ block .pixelPtr = tkffi . from_buffer ( destbuffer )
4158
4259 tklib .Tk_PhotoPutBlock_NoComposite (photo , pBlock , destx , desty ,
4360 destwidth , destheight );
4461
4562 else :
46- block .width = shape [1 ]
47- block .height = shape [0 ]
48- block .pitch = img .strides [0 ]
49- block .pixelPtr = tkffi .from_buffer (img )
63+ block .width = data . shape [1 ]
64+ block .height = data . shape [0 ]
65+ block .pitch = data .strides [0 ]
66+ block .pixelPtr = tkffi .from_buffer (data )
5067
5168 #/* Clear current contents */
5269 tklib .Tk_PhotoBlank (photo );
5370 #/* Copy opaque block to photo image, and leave the rest to TK */
5471 tklib .Tk_PhotoPutBlock_NoComposite (photo , pBlock , 0 , 0 ,
5572 block .width , block .height );
56- return tklib .TCL_OK
73+ return tklib .TCL_OK
5774
5875def tkinit (tk ):
5976 tk .createcommand (b"PyAggImagePhoto" , PyAggImagePhoto )
6077 PyAggImagePhoto .interp = tk .interp
61-
78+
0 commit comments