回复:Re: Draw bitmap(Numpy) with scale and offset

Hi Chris Barker
    In fact I had do a corp befor trans to wxBitmap then to wxImage. and do resize use wxImage. I think only corp is not enough, corp works when show a small section in a large scale. but sometimes we need show a large image in small scale to see all (my english is bad...). In this condition we need down simple first.
    but if we do corp and down simple... That means we do all the things ourself, then I think we need not wxImage at more.
    << wxImage is scaling in C++, so you&#39;re not going to get much faster unless you can use a particularly smart algorithm (or simple one -- are you interpolation, or ???>>

    I think wx should has a method that draw with offset and scale. count the final area smartly, and render useing the corresponding pixel from a bitmap. It is esay to do with C implement (Even need not a buffer). but it's difficult to do with python, then trans to bitmap...
    Thanks for your advice, I am writting ImageJ in Python, aimed to progrgram with skimage, opencv, itk seamless.

best
YXDragon

发件人:Chris Barker <chris.barker@noaa.gov>
收件人:wxpython-users <wxpython-users@googlegroups.com>
主题:Re: [wxPython-users] Draw bitmap(Numpy) with scale and offset
日期:2017年07月06日 04点52分

I need to draw a Numpy array on panel (zoom out and in ...). I use buffer to give the pixel data to the wxbitmap, but I did not find a method in dc can draw bitmap with offset and scale. so I must trans the wxbitmap to wximage, then scale and trans back to wxbitmap to draw.
yes, I think that&#39;s what you need to do :slight_smile:
though I would suspect that skimage would have a scaling ability -- maybe scale in numpy , then draw to wx.
but it cost so much memory and time (when the picture is large and scale large, a 1000 * 1000 picture scale 100 times will be 100000 * 100000). so I do a lot of work to count the final extent and corp the Numpy array first,
yup -- that&#39;s what you need to do -- again, unless skimage has this functionality.
in wx.lib.floatcanvas ScaledBitmap2, that&#39;s how I do it -- (though not from a numpy array) -- a bit of finicky code, but you really can&#39;t expect to scale up an entire huge image in memory -- so you need to crop it first.
I&#39;m not trying to get any kind of "frame rate" with FC -- but it seems snappy enough.

then: numpy -> bitmap -> image -> scale -> bitmap -> draw. It works, but so complex, and with a bad performance (Far inferior to Java&#39;s drawImage...)
you should be able to:
crop_in_numpy->wxImage->scale->wxBitmap-Draw
wxImage is scaling in C++, so you&#39;re not going to get much faster unless you can use a particularly smart algorithm (or simple one -- are you interpolation, or ???

I hope that is enough to get you started, or inspire a correction from someone who knows a better way. I should admit that I have not tested this approach thoroughly with Python 3.
nor have I :frowning:

···

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

--

You received this message because you are subscribed to the Google Groups "wxPython-users" group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

    In fact I had do a corp befor trans to wxBitmap then to wxImage. and
do resize use wxImage. I think only corp is not enough, corp works when
show a small section in a large scale. but sometimes we need show a large
image in small scale to see all (my english is bad...). In this condition
we need down simple first.

to do that, yes, you need to put the whole image in a wxImage -- is it
really that big? I"ve worked with about 10k X10k images, and it's been fine.

But if your images are so big that having two copies is causing problems,
then, yes, you want to re-scale in numpy -- or only store the wxImage, and
not the array.

I just took a look, and I don't see any scaling in skimage -- which
surprised me. I wrote some Cython code a while back that is fast for
downscaling by integer multiples -- the skimage folks said they'd likely
accept it as a contribution, but I never got around to it.

I can dig it up if you like.

    I think wx should has a method that draw with offset and scale. count

the final area smartly, and render using the corresponding pixel from a
bitmap. It is easy to do with C implement (Even need not a buffer). but
it's difficult to do with python, then trans to bitmap...

well, the issue here is that a wxBitmap is a wrapper around a
platform=specific bitmap format (matching what is used in the display, so
wx doesn't do any manipulation stuff with it -- which is why there is
wxImage. what you are describing is pretty much what you can do with
Python, except it requires a full copy of the image as a wxImage first, you
can't get away from that.

thinking now, though -- you may be able to construct a wxImage from the
numpy array buffer, without copying -- then wxImage can re-scale at full C
speed making a new Image -- which will never need to be larger than your
display.

I'd look a bit more into that -- it's been a long while, so I can't
remember if you can do that, or how.

    Thanks for your advice, I am writing ImageJ in Python, aimed to program

with skimage, opencv, itk seamless.

wx isn't going to give you full image processing capability -- I think
you'll end up wanting to add a feature or two to skimage -- maybe scaling
is that feature :slight_smile:

-CHB

···

On Fri, Jul 7, 2017 at 2:38 AM, <imagepy@sina.com> wrote:

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

way late to the game here, but I’m at the SciPy conference, and ran into one of the scikit image developers, and of course it has rescaling:

It’s in the rescaling module:

http://scikit-image.org/docs/stable/api/skimage.transform.html

They have: rescale() and .resize()

So I’ll bet you’re best off doing all your cropping and transforming in skimage.

-CHB

···

On Fri, Jul 7, 2017 at 2:36 PM, Chris Barker chris.barker@noaa.gov wrote:

On Fri, Jul 7, 2017 at 2:38 AM, imagepy@sina.com wrote:

In fact I had do a corp befor trans to wxBitmap then to wxImage. and do resize use wxImage. I think only corp is not enough, corp works when show a small section in a large scale. but sometimes we need show a large image in small scale to see all (my english is bad…). In this condition we need down simple first.

to do that, yes, you need to put the whole image in a wxImage – is it really that big? I"ve worked with about 10k X10k images, and it’s been fine.

But if your images are so big that having two copies is causing problems, then, yes, you want to re-scale in numpy – or only store the wxImage, and not the array.

I just took a look, and I don’t see any scaling in skimage – which surprised me. I wrote some Cython code a while back that is fast for downscaling by integer multiples – the skimage folks said they’d likely accept it as a contribution, but I never got around to it.

I can dig it up if you like.

I think wx should has a method that draw with offset and scale. count the final area smartly, and render using the corresponding pixel from a bitmap. It is easy to do with C implement (Even need not a buffer). but it’s difficult to do with python, then trans to bitmap…

well, the issue here is that a wxBitmap is a wrapper around a platform=specific bitmap format (matching what is used in the display, so wx doesn’t do any manipulation stuff with it – which is why there is wxImage. what you are describing is pretty much what you can do with Python, except it requires a full copy of the image as a wxImage first, you can’t get away from that.

thinking now, though – you may be able to construct a wxImage from the numpy array buffer, without copying – then wxImage can re-scale at full C speed making a new Image – which will never need to be larger than your display.

I’d look a bit more into that – it’s been a long while, so I can’t remember if you can do that, or how.

Thanks for your advice, I am writing ImageJ in Python, aimed to program with skimage, opencv, itk seamless.

wx isn’t going to give you full image processing capability – I think you’ll end up wanting to add a feature or two to skimage – maybe scaling is that feature :slight_smile:

-CHB

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov