473,597 Members | 2,872 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 #1
11 17635
bvdet
2,851 Recognized Expert Moderator Specialist
This will read two bytes at a time:
Expand|Select|Wrap|Line Numbers
  1. >>> print [f.read(2) for _ in range(12)]
  2. ['96', '80', '07', '91', '37', '84', '41', '71', '15', '25', '18', '99']
  3. >>> 
This was done on a text file. You should look at PIL link
Jun 26 '07 #2
bartonc
6,596 Recognized Expert Expert
This will read two bytes at a time:
Expand|Select|Wrap|Line Numbers
  1. >>> print [f.read(2) for _ in range(12)]
  2. ['96', '80', '07', '91', '37', '84', '41', '71', '15', '25', '18', '99']
  3. >>> 
This was done on a text file. You should look at PIL link
I imagine that you opened that text file in binary mode...
Expand|Select|Wrap|Line Numbers
  1. f = open('somefile.txt', 'b')
first.
Jun 26 '07 #3
bartonc
6,596 Recognized Expert Expert
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
I'm guessing that you know the inner workings of the image format that you are working on and can separate the format information (header) from the actual pixels of the image (data).

For working on large data sets, SciPy arrays have some features that native python lists lack.
Jun 26 '07 #4
eddiefisher41
15 New Member
Thanks guys.
Will investigate those options once i have time.
The RAW format i am working with is literally just raw data, it doesnt have a header file or start and stop bytes. The pixels have a bitdepth of 2bytes however the actual image data is only 10bits out of those 16, the rest are parity and padding bits.

Edit:
Ok just looking at the f.read(2) code above. The method returns the value of those two bytes as a string. The returned value is a hex value but is a string representation. How do i convert this string value into the actual hex value.
For example:

>>> f = open(r'C:\EFish er_ST_project07 \RAW test images\724_day_ mac_AV_on.raw', 'rb')
>>> pixel = f.read(2)
>>> pixel
'\xff\x00'
>>> type(pixel)
<type 'str'>

Cheers
Ed
Jun 26 '07 #5
bvdet
2,851 Recognized Expert Moderator Specialist
Thanks guys.
Will investigate those options once i have time.
The RAW format i am working with is literally just raw data, it doesnt have a header file or start and stop bytes. The pixels have a bitdepth of 2bytes however the actual image data is only 10bits out of those 16, the rest are parity and padding bits.

Edit:
Ok just looking at the f.read(2) code above. The method returns the value of those two bytes as a string. The returned value is a hex value but is a string representation. How do i convert this string value into the actual hex value.
For example:

>>> f = open(r'C:\EFish er_ST_project07 \RAW test images\724_day_ mac_AV_on.raw', 'rb')
>>> pixel = f.read(2)
>>> pixel
'\xff\x00'
>>> type(pixel)
<type 'str'>

Cheers
Ed
Maybe this will help:
Expand|Select|Wrap|Line Numbers
  1. import re
  2. patt = re.compile(r'(?<=\\x)[a-f0-9]+')
  3. s = repr('\xff\x00')
  4.  
  5. for hexNum in patt.findall(s):
  6.     print int(hexNum, 16)
Output:
>>> 255
0
>>>
>>> patt.findall(s)
['ff', '00']
>>>
Jun 26 '07 #6
bartonc
6,596 Recognized Expert Expert
Thanks guys.
Will investigate those options once i have time.
The RAW format i am working with is literally just raw data, it doesnt have a header file or start and stop bytes. The pixels have a bitdepth of 2bytes however the actual image data is only 10bits out of those 16, the rest are parity and padding bits.

Edit:
Ok just looking at the f.read(2) code above. The method returns the value of those two bytes as a string. The returned value is a hex value but is a string representation. How do i convert this string value into the actual hex value.
For example:

>>> f = open(r'C:\EFish er_ST_project07 \RAW test images\724_day_ mac_AV_on.raw', 'rb')
>>> pixel = f.read(2)
>>> pixel
'\xff\x00'
>>> type(pixel)
<type 'str'>

Cheers
Ed
I've never played with the struct module before, but here's what I found:
>>> import struct
>>> a = '\xff\xff'
>>> b = struct.unpack(' BB', a)
>>> b
(255, 255)
>>> b = struct.unpack(' H', a)
>>> b
(65535,)
>>>
Jun 27 '07 #7
bartonc
6,596 Recognized Expert Expert
I've never played with the struct module before, but here's what I found:
>>> import struct
>>> a = '\xff\xff'
>>> b = struct.unpack(' BB', a)
>>> b
(255, 255)
>>> b = struct.unpack(' H', a)
>>> b
(65535,)
>>>
How about this:

>>> a = a * 8
>>> a
'\xff\xff\xff\x ff\xff\xff\xff\ xff\xff\xff\xff \xff\xff\xff\xf f\xff'
>>> import array
>>> b = array.array('H' ,a)
>>> b
array('H', [65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535])
>>>
Jun 27 '07 #8
bartonc
6,596 Recognized Expert Expert
How about this:

>>> a = a * 8
>>> a
'\xff\xff\xff\x ff\xff\xff\xff\ xff\xff\xff\xff \xff\xff\xff\xf f\xff'
>>> import array
>>> b = array.array('H' ,a)
>>> b
array('H', [65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535])
>>>
Or better yet, use the array module to read the data directly:
fromfile( f, n)

Read n items (as machine values) from the file object f and append them to the end of the array. If less than n items are available, EOFError is raised, but the items that were available are still inserted into the array. f must be a real built-in file object; something else with a read() method won't do.
Jun 27 '07 #9
eddiefisher41
15 New Member
Cheers guys, will give these ideas a try and see how they turn out.
Thanks.
Ed
Jun 27 '07 #10

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

Similar topics

7
7083
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
2025
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
3058
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
3626
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
3284
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
1494
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
3740
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
7375
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
4222
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
7979
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
7894
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,...
1
8046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5437
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
3893
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...
0
3937
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2409
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1497
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.