CopyToBuffer is not working with bmp images

Hi All,

I need the image data as buffer of bmp/jpeg/png images so, i can
convert the RGB888 data into RGB565 data buffer.For this i am using
the following approach. Its working fine with the JPEG and PNG (24
bit) images

bitmap = wx.Bitmap(bmpFilePath, wx.BITMAP_TYPE_BMP)
bitDepth = bmp.GetDepth()
if bitDepth == 24:
                bpp = 3 # bytes per pixel
                buffer_length = bmp.GetWidth() * bmp.GetHeight() * bpp
                src_buffer = array.array('B', [0] * buffer_length )
                bmp.CopyToBuffer(buffer, wx.BitmapBufferFormat_RGB)

bitmap_array = ctypes.c_ushort * (bitmap.GetWidth() *
bitmap.GetHeight())
target_arr = bitmap_array()
for counter in range (0, buffer_length, bpp):
if bpp == 3:
               r = buffer[counter]
               g = buffer[counter + 1]
               b = buffer[counter + 2]
               # Convert from RGB888 to RGB565
               arr[count] = (((r>>3)<<11) | ((g>>2)<<5) | (b>>3))
count += 1

This code is not working for bmp image with the 24 bit depth and
throwing following error.
return _gdi_.Bitmap_CopyToBuffer(*args, **kwargs)
RuntimeError: Failed to gain raw access to bitmap data.

Any help really apprecaible.

Knowing the platform, version and having a sample image would be helpful.

···

On 2/24/11 3:05 AM, mukul wrote:

Hi All,

I need the image data as buffer of bmp/jpeg/png images so, i can
convert the RGB888 data into RGB565 data buffer.For this i am using
the following approach. Its working fine with the JPEG and PNG (24
bit) images

bitmap = wx.Bitmap(bmpFilePath, wx.BITMAP_TYPE_BMP)
bitDepth = bmp.GetDepth()
if bitDepth == 24:
                 bpp = 3 # bytes per pixel
                 buffer_length = bmp.GetWidth() * bmp.GetHeight() * bpp
                 src_buffer = array.array('B', [0] * buffer_length )
                 bmp.CopyToBuffer(buffer, wx.BitmapBufferFormat_RGB)

bitmap_array = ctypes.c_ushort * (bitmap.GetWidth() *
bitmap.GetHeight())
target_arr = bitmap_array()
for counter in range (0, buffer_length, bpp):
if bpp == 3:
                r = buffer[counter]
                g = buffer[counter + 1]
                b = buffer[counter + 2]
                # Convert from RGB888 to RGB565
                arr[count] = (((r>>3)<<11) | ((g>>2)<<5) | (b>>3))
count += 1

This code is not working for bmp image with the 24 bit depth and
throwing following error.
  return _gdi_.Bitmap_CopyToBuffer(*args, **kwargs)
RuntimeError: Failed to gain raw access to bitmap data.

Any help really apprecaible.

--
Robin Dunn
Software Craftsman

I am using Windows 7 os.
WxPython version is 2.8.10.1
Python version is 2.6.4
Robin you can use any Image of .bmp format with 24 bit depth.
Even a very simple image made in window Paint not working.(Sorry I didn't find any attachment option here that's why i am not able to attach the image file)

> Hi All,

> I need the image data as buffer of bmp/jpeg/png images so, i can
> convert the RGB888 data into RGB565 data buffer.For this i am using
> the following approach. Its working fine with the JPEG and PNG (24
> bit) images

> bitmap = wx.Bitmap(bmpFilePath, wx.BITMAP_TYPE_BMP)
> bitDepth = bmp.GetDepth()
> if bitDepth == 24:
> bpp = 3 # bytes per pixel
> buffer_length = bmp.GetWidth() * bmp.GetHeight() * bpp
> src_buffer = array.array('B', [0] * buffer_length )
> bmp.CopyToBuffer(buffer, wx.BitmapBufferFormat_RGB)

> bitmap_array = ctypes.c_ushort * (bitmap.GetWidth() * bitmap.GetHeight())
> target_arr = bitmap_array()
> count = 0
> for counter in range (0, buffer_length, bpp):
> if bpp == 3:
> r = buffer[counter]
> g = buffer[counter + 1]
> b = buffer[counter + 2]
> # Convert from RGB888 to RGB565
> arr[count] = (((r>>3)<<11) | ((g>>2)<<5) | (b>>3))
> count += 1

> This code is not working for bmp image with the 24 bit depth and
> throwing following error.
> return _gdi_.Bitmap_CopyToBuffer(*args, **kwargs)
> RuntimeError: Failed to gain raw access to bitmap data.

> Any help really apprecaible.

Knowing the platform, version and having a sample image would be helpful.

--
Robin Dunn
Software Craftsmanhttp://wxPython.org

···

On Feb 24, 9:24 pm, Robin Dunn <ro...@alldunn.com> wrote:

On 2/24/11 3:05 AM, mukul wrote:

you can use google gmail to add an attachment, but it may end up creating a second thread, do a search in this group for gmail. there were some good instructions for setting that up

···


Hi, I will kill all ads in google gmail.

They will all be dead and gone for all my emails to you. HA HA bye bye ads I just massacred you!!!

No, it’s just that you have to subscribe to the email list and with any email service or email client you want and then you would click Reply to whatever message and it will keep it in the thread. Gmail (or any email service or application) is irrelevant to the point of preserving threads, and the OP won’t create a second thread if he just clicks Reply.

The OP can subscribe to the list here (under wxPython-users…Subscribe to List):
http://www.wxpython.org/maillist.php

···

On Fri, Feb 25, 2011 at 10:29 PM, Micah Nordland mpnordland@gmail.com wrote:

you can use google gmail to add an attachment, but it may end up creating a second thread, do a search in this group for gmail. there were some good instructions for setting that up

I am using Windows 7 os.
WxPython version is 2.8.10.1
Python version is 2.6.4
Robin you can use any Image of .bmp format with 24 bit depth.
Even a very simple image made in window Paint not working.

  PyCrust 0.9.8 - The Flakiest Python Shell
  Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on win32
  Type "help", "copyright", "credits" or "license" for more information.
  >>> import wx
  >>> wx.version()
  '2.8.11.0 (msw-unicode)'
  >>>
  >>> cd c:/projects/wx/2.8/wxPython/demo/bitmaps
  c:\projects\wx\2.8\wxPython\demo\bitmaps
  >>>
  >>> bmp = wx.Bitmap('image.bmp')
  >>> bmp.Ok()
  True
  >>> bmp.GetDepth()
  24
  >>>
  >>> import array
  >>> a = array.array('B', [0] * (bmp.Width * bmp.Height * 3))
  >>> bmp.CopyToBuffer(a)
  >>> a[:40]
  array('B', [102, 102, 102, 107, 107, 104, 102, 102, 102, 109, 136, 118, 108, 133, 117, 108, 132, 116, 108, 133, 117, 109, 135, 117, 109, 137, 119, 111, 140, 120, 111, 143, 121, 112, 146, 123, 102, 102, 102, 115])
  >>>

(Sorry I didn't find any attachment option here that's why i am not able to attach the image file)

That is why the preferred way to read the messages on this "mail" list is with your email client. Set your google groups account to email the messages to you and reply by using your mail client.

···

On 2/24/11 9:50 PM, mukul wrote:

--
Robin Dunn
Software Craftsman