473,883 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading Binary Image Data

15 New Member
Hey Guys.
I'm looking for some info on how to read binary image data. Basicly RAW image files are strings of bits with say 2 bytes per pixel. What i need to do is read the value of each pixel in turn and store then in another variable, px1, pix2 etc. I'm running an iterative algorithm on 2 pixels at a time then moving to the next two pixels.
I am having alot of problems however reading the data. from the net and various books i see the readline method however this looks for new line charicters. I need something that will read the value of a certain number of bits from the file.

for example to read the first pixel data which is 16bits long i need something that will just read those 16bits and return its value.
Thanks.
Ed
Jun 26 '07
11 17670
eddiefisher41
15 New Member
Hey Guys. I managed to do it, thats for your help.
Here is what i came up with:
Expand|Select|Wrap|Line Numbers
  1. def GetPxValue(openfile, bitdepth): #can iterate this part to get the next pixel
  2.     read_bytes = operator.div(bitdepth, 8)  
  3.     px_value = openfile.read(read_bytes)    
  4.     if (bitdepth == 8):
  5.         unpack_code = 'B'
  6.     if (bitdepth == 16):
  7.         unpack_code = 'BB'          
  8.     if (bitdepth == 32):
  9.         unpack_code = 'BBBB'
  10.     hex_value = struct.unpack(unpack_code, px_value)  
  11.     msbyte = Dec2Bin(hex_value[0])
  12.     if (bitdepth == 16):
  13.         lsbyte = Dec2Bin(hex_value[1])      
  14.         for i in range(8):          
  15.             msbyte.append(lsbyte[i])
  16.     if (bitdepth == 32):
  17.         byte2 = Dec2Bin(hex_value[1])
  18.         byte3 = Dec2Bin(hex_value[2])       
  19.         lsbyte = Dec2Bin(hex_value[3])
  20.         for i in range(8):
  21.             msbyte.append(byte2[i])
  22.         for i in range(8):
  23.             msbyte.append(byte3[i])
  24.         for i in range(8):
  25.             msbyte.append(lsbyte[i]) 
  26.     return msbyte 
  27. #return the whole bitdepth long pixel value as a list ready for filtering
  28.  
Note: Dec2Bin is a decimal to binary converter that returns a binary value as a list. the list is necessary for filtering in the next stage.
Cheers.
Ed
Jun 27 '07 #11
bartonc
6,596 Recognized Expert Expert
Hey Guys. I managed to do it, thats for your help.
Here is what i came up with:
Expand|Select|Wrap|Line Numbers
  1. def GetPxValue(openfile, bitdepth): #can iterate this part to get the next pixel
  2.     read_bytes = operator.div(bitdepth, 8)  
  3.     px_value = openfile.read(read_bytes)    
  4.     if (bitdepth == 8):
  5.         unpack_code = 'B'
  6.     if (bitdepth == 16):
  7.         unpack_code = 'BB'          
  8.     if (bitdepth == 32):
  9.         unpack_code = 'BBBB'
  10.     hex_value = struct.unpack(unpack_code, px_value)  
  11.     msbyte = Dec2Bin(hex_value[0])
  12.     if (bitdepth == 16):
  13.         lsbyte = Dec2Bin(hex_value[1])      
  14.         for i in range(8):          
  15.             msbyte.append(lsbyte[i])
  16.     if (bitdepth == 32):
  17.         byte2 = Dec2Bin(hex_value[1])
  18.         byte3 = Dec2Bin(hex_value[2])       
  19.         lsbyte = Dec2Bin(hex_value[3])
  20.         for i in range(8):
  21.             msbyte.append(byte2[i])
  22.         for i in range(8):
  23.             msbyte.append(byte3[i])
  24.         for i in range(8):
  25.             msbyte.append(lsbyte[i]) 
  26.     return msbyte 
  27. #return the whole bitdepth long pixel value as a list ready for filtering
  28.  
Note: Dec2Bin is a decimal to binary converter that returns a binary value as a list. the list is necessary for filtering in the next stage.
Cheers.
Ed
Hi Ed. It's great when members post the solutions that they have come up with. I've added CODE tags to your post. Instructions on how to do this are on the right hand side of the page while you are posting or replying.

I felt sure that you would go with the array thing when I discovered it. I think that you will get a performance boost be reading larger chunks and working on slices of the array. The cool thing about python is that you may use one approach to get things working, then, with a little tweaking, make it work really well.

Have fun,
Barton
Jun 27 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

7
7103
by: John | last post by:
I have over 5000 thumbnail pictures of size 5kb each. I would like to able to load all 5000 pictures and view 50 per page using mysql_data_seek(). I would like to know what are the advantages and disadvantages of using a MySQL blob field rather than reading the images directly from the file? How does one insert an image into a blob field? Can it be done dynamically? Thank you John
2
2052
by: Albert Tu | last post by:
Hi, I am learning and pretty new to Python and I hope your guys can give me a quick start. I have an about 1G-byte binary file from a flat panel x-ray detector; I know at the beggining there is a 128-byte header and the rest of the file is integers in 2-byte format. What I want to do is to save the binary data into several smaller files
2
3080
by: jimmyfishbean | last post by:
Hi, I am using VB6, SAX (implementing IVBSAXContentHandler). I need to extract binary encoded data (images) from large XML files and decode this data and generate the appropriate images onto disk. My XML files have the following structure: <?xml version="1.0" encoding="utf-8" ?> <imagepla xmlns:dt="urn:schemas-microsoft-com:datatypes">
3
3639
by: dale zhang | last post by:
Hi, I am trying to read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp The article author is using PictureBox for windows application, while I am doing for web. I can only find Image from web forms control and HTML control. This may be the root cause of my problem. For read button, I converted his VB to the C#. But the compiler complains:
4
3311
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any errors. After reading the ole object from db, I saved it to C: as file1.bmp and displayed on the web. But it can not be displayed. After I manually sent the file to wordpad, it shows
2
1504
by: Denise Smith | last post by:
Hello, I'm wondering if anyone can help me out here? I want to be able to browse records in a database where one of the fields contains an image. I think I might have to extract the image to a byte array, write it to a temp file then refer to the temp file as the URL source for the Image control.
2
3759
by: Chucker | last post by:
Hi Community, I think I can store Binary Data in SQL Server but when I try to retrieve it, I always only get one byte. I think I stored my Binary Data in SQL Server in a Colum of Type Image. At least when I execute the following code, I get some significant network traffic. When I check the database with query analyzer, I see 4 Hex Chars in the image colum. Like 0xe0 etc.
2
7392
by: Ed | last post by:
Hope someone can help me out... I have been tasked to read some image data from an sql database and save the files to flat files. OK, sounds easy as I'v used BLOBs before. But this is an old database and I cannot get the image to work. The columns in the database are of type text. Here is one of the images text (in full) in the database (I hope you can see it):
6
4235
by: jcasique.torres | last post by:
Hi everyboy. I trying to create a C promang in an AIX System to read JPG files but when it read just the first 4 bytes when it found a DLE character (^P) doesn't read anymore. I using fread function. Here a few lines: char *sAnv; .... sprintf(file_a, "%s/anverso.jpg", strDir);
0
9944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9796
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10757
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10420
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9583
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7975
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7134
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5804
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.