Im really struggling with serialized textbuffer data. I just got thru an SQL encoding issue(thanks theGtkNerd for the help.) and now my troubles are back.
Im trying to add search/replace functionality to a textview that is using pixbufs and formated text, since i have images/tags stored in the buffer, i am trying to do the replace on the serialized textview buffer data.
the following code works as long as the replace string is the same size as the searchstr.
def _diagFnRReplaceAll(self,oWidget): #Replace All Function
findbox = self.builder.get_object('FnRFindEntry')
searchstr = findbox.get_text()
buf = self.dataview.get_buffer()
repbox = self.builder.get_object('FnRReplaceEntry')
repstr = repbox.get_text()
format = buf.register_serialize_tagset()
data = buf.serialize(buf, format, buf.get_start_iter(),
buf.get_end_iter())
sys.stdout.buffer.write(data) #< print raw for debugging
newdata = data.replace(bytes(searchstr,'ascii'),bytes(repstr,'ascii'))
print("\n\n\n\n")
sys.stdout.buffer.write(newdata) #< print raw for debugging
buf.set_text('')
format = buf.register_deserialize_tagset()
buf.deserialize(buf, format, buf.get_end_iter(),newdata)
if its smaller or larger i get the following error.
Gtk:ERROR:../../../../gtk/gtktextbufferserialize.c:1588:text_handler: code should not be reached
i tried changing the encoding type, and different ways to encode, but it didnt help. the fact that a same size string works fine makes me think there is a size value for the serialized buffer data or pixbuf data somewhere, but i havnt come up with anything by searching.
i tried to do the replace like you would on a textview without pics, it worked but lost the pic/format data.
Does anyone know why this is happening?
or does anyone know another way i can do a search and replace in a textview widget that has pixbuf data and formatting tags?
well i got it wotking using a little byte patching.I just saw your comment on using marks, i will look into that as i would rather use builtin functionality instead of what im doing. My way is replacing the 4 bytes after the GTKBUFFERCONTENTS-001 with a new 4 byte value for the new size of the buffer.
here is what i have working right now
def _diagFnRReplaceAll(self,oWidget):
findbox = self.builder.get_object('FnRFindEntry')
searchstr = findbox.get_text()
buf = self.dataview.get_buffer()
repbox = self.builder.get_object('FnRReplaceEntry')
repstr = repbox.get_text()
format = buf.register_serialize_tagset()
data = buf.serialize(buf, format, buf.get_start_iter(), buf.get_end_iter())
start_bytes = data[0:26]
size_bytes =data[26:30]
sizeval = int.from_bytes(size_bytes, byteorder='big', signed=False)
end_of_markup = 29 + sizeval +1
the_rest = data[end_of_markup:len(data)]
markup = data[30:end_of_markup]
newmarkup = bytearray(markup.replace(bytes(searchstr,'ascii'),bytes(repstr,'ascii')))
newsize = len(newmarkup).to_bytes(4,'big')
reconstruct =start_bytes + newsize + newmarkup +the_rest
buf.set_text('')
format = buf.register_deserialize_tagset()
buf.deserialize(buf, format, buf.get_end_iter(),reconstruct)
This works without issue so far, i will repost if i get it working with the 'Marks' suggestion. Thanks again theGtknerd.
Related
I have a binary file consists of multiple sensors data which is written by Catman software(HBM).
I am reading that file using guidelines given by Catman Software.
https://docs.google.com/spreadsheets/d/1dZOw9L6_ukHNYlcR-n64DuRZ702nBq6jjA169q-aCz0/edit?usp=sharing
please check the link for the data writing format.
I am getting all the parameters correctly, but not the data, please help.
Actual sensor data showing in Catman software
-0.05625
-0.07083
-0.07500
-0.07396
-0.07708
-0.07188
-0.06563
-0.04896
These are the values i got while reading same those values in double
-3.01998798238334e-13
1.68682055390325e-77
-1.07637219868070e-141
6.61923789516606e-206
-3.69718906070881e-270
-7.62423663153382e+282
4.68858598328491e+218
-2.61881942919867e+154
fid=open(file,"rb")
data = np.frombuffer(fid.read(8), dtype=np.double)
Please help me to get real values
https://in.mathworks.com/matlabcentral/fileexchange/6780-catman-file-importer?focused=54427cf8-d142-8e8a-812e-79e73b54b4fb&tab=function
MATLAB LINK FO REFERENCE
this should read the first data value into a numpy array for you:
with open(file, 'rb') as f:
file_id = int.from_bytes(f.read(2), byteorder='little')
data_offset = int.from_bytes(f.read(4), byteorder='little')
f.seek(data_offset, 0)
first_data_value = np.frombuffer(f.read(8), dtype='<f8')
You will also need the number of channels and length of each channel from the channel info section to know how many 8-byte blocks to read.
I am trying to use struct.pack to pack a hash disgest, but not getting the expected result.
This is how I am packing the data:
hash = hashlib.sha256(input).digest()
print('hash = ', hash.hex())
packed = struct.pack('!32p', hash)
print('packed = ', packed.hex())
Here is an example result:
hash = b5dbdb2b0a7d762fc7e429062d64b711d240e8f95f1c59fc28c28ac6677ffeaf
packed = 1fb5dbdb2b0a7d762fc7e429062d64b711d240e8f95f1c59fc28c28ac6677ffe
The bytes appear to be shifted, and "1f" has been added. Is this a result of an incorrect format specifier?
EDIT: I believe this first byte is the length of the data, because I am using 'p'. Is there any way to avoid this? I don't want to include this in my packed data
The 'p' format character encodes a “Pascal string” which includes the string's length at the beginning. This is documented. If you don't want that use 's' format to get just the bytes themselves instead:
packed = struct.pack('!32s', hash)
print('packed =', packed.hex())
Output:
packed = b5dbdb2b0a7d762fc7e429062d64b711d240e8f95f1c59fc28c28ac6677ffeaf
My script cleans arrays from the unwanted string like "##$!" and other stuff.
The script works as intended but the speed of it is extremely slow when the excel row size is big.
I tried to use numpy if it could speed it up but I'm not too familiar with is so I might be using it incorrectly.
xls = pd.ExcelFile(path)
df = xls.parse("Sheet2")
TeleNum = np.array(df['telephone'].values)
def replace(orignstr): # removes the unwanted string from numbers
for elem in badstr:
if elem in orignstr:
orignstr = orignstr.replace(elem, '')
return orignstr
for UncleanNum in tqdm(TeleNum):
newnum = replace(str(UncleanNum)) # calling replace function
df['telephone'] = df['telephone'].replace(UncleanNum, newnum) # store string back in data frame
I also tried removing the method to if that would help and just place it as one block of code but the speed remained the same.
for UncleanNum in tqdm(TeleNum):
orignstr = str(UncleanNum)
for elem in badstr:
if elem in orignstr:
orignstr = orignstr.replace(elem, '')
print(orignstr)
df['telephone'] = df['telephone'].replace(UncleanNum, orignstr)
TeleNum = np.array(df['telephone'].values)
The current speed of the script running an excel file of 200,000 is around 70it/s and take around an hour to finish. Which is not that good since this is just one function of many.
I'm not too advanced in python. I'm just learning as I script so if you have any pointer it would be appreciated.
Edit:
Most of the array elements Im dealing with are numbers but some have string in them. I trying to remove all string in the array element.
Ex.
FD3459002912
*345*9002912$
If you are trying to clear everything that isn't a digit from the strings you can directly use re.sub like this:
import re
string = "FD3459002912"
regex_result = re.sub("\D", "", string)
print(regex_result) # 3459002912
I have a program (in Pyglet) in which I would like to keep the icons of them in the form of bytes, then convert them into temporary images and pass them to the program to be able to use them.
This is a dictionary where the data of the two icons are present:
ico = {
"ico_16": {
"dimension": (16, 16),
"byte": b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x06\x00\x00\x00\x1f\xf3\xffa\x00\x00\x00\x04gAMA\x00\x00\xb1\x8f\x0b\xfca\x05\x00\x00\x00 cHRM\x00\x00z&\x00\x00\x80\x84\x00\x00\xfa\x00\x00\x00\x80\xe8\x00\x00u0\x00\x00\xea`\x00\x00:\x98\x00\x00\x17p\x9c\xbaQ<\x00\x00\x00\x06bKGD\x00\xff\x00\xff\x00\xff\xa0\xbd\xa7\x93\x00\x00\x00\x07tIME\x07\xe2\x08\x05\x12-\x0f?\xc1\x9b\xba\x00\x00\x03\x0fIDAT8\xcbU\x92Kh\\U\x00\x86\xbfs\xee\x99{g\xe6>f:\xe9<,3i51\xa6\x92B\xb5iHQ\n%.\n\xf6A)E(\xb5YtS\x8a R\\\xe8F\r\xe2^\x10WZ\x91\xae\x8a\x8b`\t\xbaP\xac\x0b\xad\x8a\xf4\x85\n\xadv\xa16\x19\'N\x9b\xcc+3\xb9\xcf\xe3b\xda\x85\xff\xf2\x87\xff\xdb\xfc\x9f`Q\xc31\x01\x07\xce\xc0\xee\xb9\x14?^~\x81^{\x9e`\xf3y\x92\xa4\x0c\x80\x94\xab\x98\xe9\xefpr\x9f2{\xe4kn~\x13r\xe5#X\xd4\x18L\x1f\x85\x9b\xdf\xc2\xe8\xce\n\xb7\xae\xbcK\xeb\xfe\x02A0\x8dP\x1e\x86)\x91J\x92h\x0f\x7f0E\xbf\xf3"\xcb\xb7+\x14*7\xe8\xb4{L\xeeC\x10h\x83\xb3\x1fV\xf9y\xe9\x036\xba\x87\xb0l\xc8\xb8`f\xc0H\x81\x90\x10\x87\x10\x0c\xa0\xdf\x01\xbf\x07\xb6\xbb\xc4\xdeC\xaf\xf0\xf1\xb9e\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xf8\xf5\x87y,\x17\x9c\x1cX\x19H\xa5\xc1r1\xbd\x02\x89? Z\xaf\xc3\xa0\x0bA\x88\x88\x03\xd4\xd8\xae\x8b\xf1\xfc\x1bo\xa9\xa8B\x03\xa7\xf06J\x14\xe8\xd4\x0f\xe3w\x86*\xa7,P&\xc1\xfd\xbf \x89\xfe\xa7\xb2v\xf2K\xe1\xc8\xb6w(Q\x17\\X\x85W\xf7\xc0s\xc7*\xd4\xef\xbe\xc9F\xf7e4y\x0csx\x15#\x1cA\x1c\x80\xa0\x85\xed^\xa4:\xf1\x1e_\xbc\xdf\xe0\x93\x7f\x91\xe4K\xd0]\x86\xcd\x8d\x06s\xa7\xceS\xaa\x9e\xc4v/!\xf5\n\xb1\x1f\x11\xfb\x11R\xaf`\xbb\x97(UO2w\xea<\x9b\xfd\x06ZC\xae\xc8\x7f\xde\x160\x90\xb5\xbaI\xa8\x00\x00\x00%tEXtdate:create\x002018-08-05T18:45:15+00:00\xbf\xac\x1c\xb8\x00\x00\x00%tEXtdate:modify\x002018-08-05T18:45:15+00:00\xce\xf1\xa4\x04\x00\x00\x00\x00IEND\xaeB`\x82'
},
"ico_32": {
"dimension": (32, 32),
"byte": b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00 \x00\x00\x00 \x08\x06\x00\x00\x00szz\xf4\x00\x00\x00\x04gAMA\x00\x00\xb1\x8f\x0b\xfca\x05\x00\x00\x00 cHRM\x00\x00z&\x00\x00\x80\x84\x00\x00\xfa\x00\x00\x00\x80\xe8\x00\x00u0\x00\x00\xea`\x00\x00:\x98\x00\x00\x17p\x9c\xbaQ<\x00\x00\x00\x06bKGD\x00\xff\x00\xff\x00\xff\xa0\xbd\xa7\x93\x00\x00\x00\x07tIME\x07\xe2\x08\x05\x12+\x1a\x04F\xd8\xd7\x00\x00\x08\xc3IDATX\xc3\x9d\x97il\\\xd5\x15\xc7\x7f\xf7\xad\xb3\xd8c{<\xb6gL\xec\xd8\xb1]\xc7\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xd8\xba\xe98\x19\\d\x17\xa7\n(\xcc!\xd8\x7f\xe7An\x9c\xfe:\x9e\xfd\x18\x0b\xd3}\x94\xf2\xd2+\xd7\x96Q\xa8\x91\xce\x1a\xe1\x14A9\xf4\x83\xca\xe6\xd5\xa2`\xcb7\x81]\x86jI\xde\xd5\xd6\x8e\t\xd6o\xfa\x16\xf7\xde\xf6\x12G\xa9]JO\xf0\xb4\xbfR$#\xc0g\xa3>\xd7}{\x1b3\xef=\xcc\xc2\xf4\x0eI8W\x1d\xc3G}\x98\xb4\xf7\xdd\xcb\x1f\x7f:\xca3\xbe\xa0z\xce\xa2\xaf\\P|\xf7\x9d\x80Bv\x8c\xee\xa1\x9b\xe9\x1cx\x88xk\x1a\\(\xe7\xc1\xcaA)\x07\xa5\xfc\x7f\x91\x1cXY\xf9_\\\x88\xb7\xa6\xe9\x1cx\x88\xee\xa1\x9b),\x8d\xf2\x9b9\x10k\x0b\xfd\xda\xbef\x01\xaf=U;\xb7\x19\xf6\x1f\xd8K\xf7\xd0\xb5t\x0e\xec#\xd1~\x1a3\xe4\xe2\xd9\x92v\x9fo\xbcZ\x00\xcf\x063\xe4\x92h?M\xe7\xc0>\xba\x87\xae\xe5\x89\x03{\xa9X3TJ\xf0\xe4\xf7dy\xff\x1f\xfdN\x8e\xbb\xf6Cn\xfe\xa3>\xcf_"\xd6\xfc\xcf\x0b\x9f\xe7\xad\xf0\xf8\xed\x17\x98\xfa\x0f\x10\x99\xc4\xde3\xad\xf4\x83\x00\x00\x00%tEXtdate:create\x002018-08-05T18:43:26+00:00\r\xd5q\x81\x00\x00\x00%tEXtdate:modify\x002018-08-05T18:43:26+00:00|\x88\xc9=\x00\x00\x00\x00IEND\xaeB`\x82'
}
}
While this is the part of the script in which it is converted into temporary images and passed to Pyglet:
temp_ico = []
for ic, ic_key in ico.items():
with BytesIO() as ic_key["byte"]:
from PIL import Image
with Image.new("RGB", ic_key["dimension"]) as ico_img:
ico_img.save(ic_key["byte"], format="PNG")
ico_img = pyglet.image.load(ico_img)
temp_ico.append(ico_img)
self.set_icon(temp_ico[0], temp_ico[1])
The problem I'm having is that Pyglet, with: pyglet.image.load(), expects the file path and not the file itself.
Since the file is not physical, I do not know how to get the path of it to get it used by Pyglet. Do you have any suggestions or advice on how to solve the problem?
Edit:
I tried with both answers obtained from "Torxed":
temp_ico = []
self.temp_sp = []
for ic, ic_key in ico.items():
ico_img = pyglet.image.ImageData(ic_key["dimension"][0], ic_key["dimension"][1], 'RGB', ic_key["byte"])
temp_ico.append(ico_img)
temp = pyglet.sprite.Sprite(ico_img)
temp.x = randint(0, 100)
temp.y = randint(0, 100)
self.temp_sp.append(temp)
self.set_icon(temp_ico[0], temp_ico[1])
And:
temp_ico = []
self.temp_sp = []
for ic, ic_key in ico.items():
ico_img = pyglet.image.SolidColorImagePattern((255, 255, 255, 255)).create_image(ic_key["dimension"][0], ic_key["dimension"][1])
ico_img.set_data('RGB', ico_img.width * len('RGB'), ic_key["byte"])
temp_ico.append(ico_img)
temp = pyglet.sprite.Sprite(ico_img)
temp.x = randint(0, 100)
temp.y = randint(0, 100)
self.temp_sp.append(temp)
self.set_icon(temp_ico[0], temp_ico[1])
But in both methods I get a mix of confusing pixels and not the original image.
I have also modified the method in which I obtained the printing of the image bytes. Before I used this method:
from io import BytesIO
from PIL import Image
with BytesIO() as output:
from PIL import Image
with Image.open("ico_32.png") as img:
img.convert('RGB').save(output, "png")
data = output.getvalue()
print(data)
Which I had obtained from here Answer by: perillaseed
But then I changed to:
s = open("ico_32.png", "rb")
data = s.read()
print(data)
s.close()
f = open('new_image.png', 'wb')
f.write(data)
f.close()
If I go to check the new saved image, it is exactly the same as the original, so I copy the byte string manually, insert it in the dictionary and it does not work.
Here are the visual results:
Also, I wanted to warn that I put the two icons as sprite and designed to see them better, since they were too small on the actual icon of the program.
To respond to snakecharmerb, I tried when you answered the suggestion you gave me, but as he also answered Torxed, I get the error he reported himself.
Edit2:
I tried to change the icons (since the two I used were only for testing and not the final ones of the program) with two others, but I get the same result. Above are the original ones, under the new ones.
Bytes string (new_ico_16):
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x03\x00\x00\x00(-\x0fS\x00\x00\x00\x03sBIT\x08\x08\x08\xdb\xe1O\xe0\x00\x00\x00\tpHYs\x00\x00\x01\xc1\x00\x00\x01\xc1\x01E\x85\\9\x00\x00\x00\x19tEXtSoftware\x00www.inkscape.org\x9b\xee<\x1a\x00\x00\x00\xd2PLTE\xff\xff\xff\x00\x00\x00O33gNNm/1l/1t./\x87`cy*-\x86_b\x8f*+\x8efiz,/\x90hl\x94!$\xa1\'(\xa3{|\x8eln\xb4$&\xb9 !\xa1\x88\x8b\x8b"#\xae\x8f\x91\xbc\xa8\xaa\xbe\xb0\xb2\xe4\x19\x1a\xec\x1d\x1d\x90\x8d\x90\x85\x88\x8c\x89\x8b\x8e\x8b\x92\x96\x90\x95\x98\x96\x9e\xa2\x97\x9f\xa3\xa2or\xa4`b\xa9\x19\x1a\xaa\x1b\x1d\xaa\xb5\xba\xaa\xb6\xbb\xab\xb2\xb7\xab\xb3\xb8\xab\xb4\xb8\xab\xba\xc0\xb7\x11\x12\xb9\xc4\xc9\xbe\xc0\xc2\xbf\xbb\xbc\xc4\x14\x15\xd0\xde\xe3\xd6\t\t\xd8\n\n\xd8\xe5\xea\xda\n\n\xda\x0b\x0b\xda\xe6\xeb\xdb\xe7\xec\xdc\xe8\xec\xe0\r\r\xe1\r\r\xe2\xec\xef\xe2\xec\xf0\xe4\xe5\xe6\xe7\x10\x10\xe8\xf0\xf3\xec\xea\xeb\xf4\x16\x16\xf5\xf8\xfa\xff\x1a\x1a\xff\xff\xff\xeb\x12-N\x00\x00\x00\x1ctRNS\x00\x01-4\x81\x82\x91\x9d\x9e\x9e\xb0\xb2\xb8\xc1\xd0\xd1\xd8\xd9\xe5\xe9\xf1\xf2\xf2\xfb\xfc\xfd\xfd\xfe(\xffw\x99\x00\x00\x00\xb0IDAT\x18\x19=\xc1\x89\x1a\x81#\x14\x06\xd0\x7f\n\x955Id\xdf\xf7\xc24\xd6\x90\xf5\xfd_\xc9\x1d\xfa:\x07\x92\x92\xc9\x96J\xd9\x8c\x82?\x96\xb6\xde?V\x9a\x81\xb0|\xe3\x19\x85\xfb}\x18=\x1by\x06 \xe5\x9ey\xec\xec\xa6\x00\xa5\x12\xf0DPQ\xa0\xef\xb8\xb4\xd9pi\xa7\xa3\xc8\xa5\xc1l6\xe4R\x11\xd5\x15\x19\xcf\xbb\x9d\xf5hE\xaahzd\xbah\xb7\xfc\x89G\x9a\xb0\x85\xd4\xf7\xfd\x9e\x90l\x14\x8e\x82l\x97\xcb\xad \xc7\x02\xd4\xfaA$\x0eu\x15\xd0n\x17\x11\xbb\xdc4\x00\xac\xfcx\xdd\xaf\xa7\xd3\xf5\xfez\x94\x19\x083j\x9f\x9f\x9a\xc1\xf0\xa7\xe6L\xc71s*\xc8\x17B\xf5#3;\xd8r\xdc\x00\x00\x00\x00IEND\xaeB`\x82'
Bytes string (new_ico_32):
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00 \x00\x00\x00 \x08\x03\x00\x00\x00D\xa4\x8a\xc6\x00\x00\x00\x03sBIT\x08\x08\x08\xdb\xe1O\xe0\x00\x00\x00\tpHYs\x00\x00\x03\x82\x00\x00\x03\x82\x01\x83\x06\xfdz\x00\x00\x00\x19tEXtSoftware\x00www.inkscape.org\x9b\xee<\x1a\x00\x00\x01\xb3PLTE\xff\xff\xff+++$$I77739944;77;F47UFIJ36K27M27ZIL[JM[JNG37J37SGKN26L26J36N14T15UIMU25P16U15O26Q/2YLPWLOUJNYMQ\x8f)+^RUXORYOS^SV_TWK15\xaa&\'\xae%\'\x89%\'YRV\xc9\x1f \xd5 !\xd7\x1f NMQ\x95\x8c\x8e\xad\xa8\xa9\xb2\xac\xae\xc5"#V\\`Z_d^?B_KOj8;kptnDGnruotxpy~qtxtvy\x81\x88\x8c\x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\xba\xf3\xb1\xb8|wU\x04\xc2tm\xa1=\x16\xc7^\xa8M\x07\xcf$\xd3*\xde\x04\xd8\x1b\x81\x1b\\5I\xf0\x82\x94\x1c#^l3\x85\xed\x0b\xc4\xe3\x12\x02\xafT\xc5\xeb\x88\x18\xba>;\xdc\xf7\xf9\xf6\x0f\xcf\xaeC\x88\xb8^\xac\x02\x81\x98\xda\x9f0\xc6S\xbb\x89\x80\x82\xa4\xab\x9eC\x85\xb9j\x9d\x04\xb1\x88\xa1\xb2u\xed\x01\xc3\x1e\xd6Z+\r\x04\xde\x935)9E\xe5\xe5E9)\x1a\x19\x84\xff\xe6\x8b7\xda7\x9aJ\x93\x00\x00\x00\x00IEND\xaeB`\x82'
I also tested this way (by modifying the script in the first edit), but with no different results from before:
s = open(ic + ".png", "rb")
data = s.read()
ico_img = pyglet.image.SolidColorImagePattern((255, 255, 255, 255)).create_image(ic_key["dimension"][0], ic_key["dimension"][1])
ico_img.set_data('RGBA', ico_img.width * len('RGBA'), data)
s.close()
What #snakecharmerb said is completely true, you could load it via a BytesIO stream. And it's usually the way most people go. The problem with it is that you need the complete data in order for it to load, otherwise you'll get:
Traceback (most recent call last):
File "test.py", line 19, in <module>
kitten = pyglet.image.load('kitten.png', file=kitten_stream)
File "C:\Program Files\Python37\lib\site-packages\pyglet\image\__init__.py", line 205, in load
raise first_exception
File "C:\Program Files\Python37\lib\site-packages\pyglet\image\__init__.py", line 195, in load
image = decoder.decode(file, filename)
File "C:\Program Files\Python37\lib\site-packages\pyglet\image\codecs\gdiplus.py", line 283, in decode
bitmap = self._load_bitmap(file, filename)
File "C:\Program Files\Python37\lib\site-packages\pyglet\image\codecs\gdiplus.py", line 226, in _load_bitmap
'GDI+ cannot load %r' % (filename or file))
pyglet.image.codecs.ImageDecodeException: GDI+ cannot load 'kitten.png'
To get around this, you can create your own valid image structure and in-line replace the data.
And I like quirky solution, so I'll drop this little nugget for anyone who needs to manipulate data/pixels in a way you're planning on doing.
ico_img = pyglet.image.SolidColorImagePattern((255,255,255,255)).create_image(16, 16) # Width, Height
ico_img.set_data('RGB', image_data.width*len('RGB'), ico['ico_16']['byte'])
What this does, is basically just crate (pretty quickly) a solid image blob with the right format. Then it in-line replaces the data with whatever you give it. The only important thing here is that your width and height matches the data-length/format chosen. I assumed the data you've got going is RGB and it appears to be working.
Now, I recon this is the fastest way to do it.
But there's a better and almost as fast way of doing it, and it's more readable:
ico_img = pyglet.image.ImageData(16, 16, 'RGB', ico['ico_16']['byte'])
This accepts any data stream that's valid RGB data (0-255).
Lastly, don't forget to place your image in a sprite object to make it faster and easier to work with:
ico_img_sprite = pyglet.sprite.Sprite(ico_img)
It'll make your life easier in the long run! :)
Edit: there are three differences in the first post and the methods to solve this.
Firstly, the original image data appears to be broken, so it's hard/impossible to load.
Secondly, seeing as the new raw data contains meta-data such as headers, creation timestamp etc. It will only be loadable via the io.BytesIO method, because it needs to go through any available image decoder.
Thirdly, you need to extract the image data (pixel-data) from the image in order to use any methods posted in my answer. Since it will bypass any image encoders and/or compressions. Making it a little bit faster, but at the expense of image data size.
Here is snakes answer working with the new data:
raw_image = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x03\x00\x00\x00(-\x0fS\x00\x00\x00\x03sBIT\x08\x08\x08\xdb\xe1O\xe0\x00\x00\x00\tpHYs\x00\x00\x01\xc1\x00\x00\x01\xc1\x01E\x85\\9\x00\x00\x00\x19tEXtSoftware\x00www.inkscape.org\x9b\xee<\x1a\x00\x00\x00\xd2PLTE\xff\xff\xff\x00\x00\x00O33gNNm/1l/1t./\x87`cy*-\x86_b\x8f*+\x8efiz,/\x90hl\x94!$\xa1\'(\xa3{|\x8eln\xb4$&\xb9 !\xa1\x88\x8b\x8b"#\xae\x8f\x91\xbc\xa8\xaa\xbe\xb0\xb2\xe4\x19\x1a\xec\x1d\x1d\x90\x8d\x90\x85\x88\x8c\x89\x8b\x8e\x8b\x92\x96\x90\x95\x98\x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x17B\xf5#3;\xd8r\xdc\x00\x00\x00\x00IEND\xaeB`\x82'
image_buffer = io.BytesIO(raw_image)
image = pyglet.image.load('temp.png', file=image_buffer)
image_sprite = pyglet.sprite.Sprite(pyglet_image)
If you want to store the pixel data alone, you could extract it like this:
print(image.image_data.get_data('RGBA', 16*4))
Where 'RGBA' is the format you want on the data, and 16*4 is the number of pixels per row times the number of colors per pixel (RBGA is 4).
That will give you the option to do:
raw_data = b'\x00\xae\xff\x00\x00\x00\x00\x00\x00#b\x00\x00q\xa5\x05\x009XI\x00-I\xa8\x00\'A\xe4\x00%?\xfb\x00%?\xfb\x00\'A\xe4\x00-I\xa8\x009XI\x00q\xa5\x05\x00Be\x00\x00\xff\xff\x00\x00\xae\xff\x00\x00\x9e\xe9\x00\x00\r\x1c\x00\x00Hm\x18\x00.I\x92\x00#<\xef\x00"<\xff\x00%B\xff\x00(F\xff\x00(F\xff\x00%B\xff\x00"<\xff\x00#<\xef\x00.I\x92\x00Jo\x18\x00\x00\x00\x00\x00\x9f\xea\x00\x00\xcf\xff\x06\x00a\x93 \x00-I\xb2\x00";\xff\x00)G\xff\x002T\xff\x007[\xff\x00;a\xff\x00;c\xff\x009_\xff\x003V\xff\x00)H\xff\x00"<\xff\x00.L\xb3\x00i\xa0#\x00\xcb\xff\x07\x04\xb7\xff\x1e\x06U\x81\xa0\x00)H\xff\x000U\xff\x00<d\xff\x00Cm\xff\x00Hu\xff\x00O\x82\xff\x06]\x94\xff\x04V\x8b\xff\x00Eq\xff\x00<d\xff\x006_\xff\x013Y\xff\x05V\x86\xa2\x02\xb5\xff!\x02v\xb4f2\x8d\xbf\xf3\x10[\x95\xff\x0b[\x97\xff\x04\\\x9a\xff\x02^\x9e\xff\x0cn\xaf\xff\tu\xc6\xffA\xad\xee\xff5\x9f\xdc\xff\x01Z\x92\xff\x00M\x82\xff!u\xb0\xff?\x9b\xd3\xff(\x88\xbf\xf3\x01u\xb5g\x00#l\xb5+{\xab\xff4\xa0\xea\xff:\xaa\xf6\xff4\xa5\xf0\xff,\xa0\xee\xff;\xb0\xfa\xff4\xaf\xfd\xff8\xb3\xff\xff3\xa9\xf4\xff\x05i\xaf\xff\x00d\xad\xffL\xb0\xec\xff[\xc2\xff\xff$w\xad\xff\x00Am\xb5\x004X\xe7*}\xb1\xffD\xb8\xff\xff\x11\x96\xff\xff:\xb4\xff\xff;\xb7\xff\xff\x0f\x99\xff\xffE\xbf\xff\xff8\xb8\xff\xff1\xac\xf9\xff\x03q\xca\xff\x19\x89\xda\xff9\xb4\xfe\xffE\xbb\xff\xff$y\xb0\xff\x003W\xe7\x001R\xfc+\x80\xb6\xffD\xb9\xff\xff\x10\x97\xff\xff9\xb4\xff\xff;\xb7\xff\xff\x0e\x99\xff\xffE\xbe\xff\xff8\xba\xff\xff1\xaf\xfe\xff\x07\x80\xea\xff1\xa5\xf5\xff\x1d\x9e\xff\xffF\xbb\xff\xff$|\xb3\xff\x001Q\xfc\x001R\xfc+\x80\xb6\xffD\xb8\xff\xff\x10\x93\xff\xff:\xb2\xff\xff;\xb5\xff\xff\x0e\x95\xff\xffE\xbe\xff\xff8\xbc\xff\xff0\xb2\xff\xff\x1f\x9d\xfd\xff&\x9b\xf2\xff\x11\x8b\xf6\xffG\xba\xff\xff${\xb2\xff\x001Q\xfc\x004W\xe7*}\xb2\xffD\xb7\xff\xff\x0f\x88\xf5\xff8\xa8\xf4\xff?\xaf\xf6\xff\x0c\x89\xf7\xffE\xbc\xff\xff8\xbd\xff\xff4\xb9\xff\xff8\xb0\xf9\xff\tx\xcf\xff\x12\x80\xdc\xffG\xb7\xfe\xff$x\xae\xff\x003V\xe7\x00>h\xb3+z\xab\xffE\xb5\xf7\xff\x0fv\xc8\xff\x07g\xaa\xff\tl\xad\xff\nw\xcd\xffG\xbb\xfd\xff7\xb9\xff\xff\\\xc7\xff\xff3\x9b\xdc\xff\x00\\\xa4\xff\x13v\xbd\xffH\xb6\xf6\xff%u\xa6\xff\x00=f\xb2\x00X\x8f^"o\x9c\xf2F\xa5\xd8\xff\tY\x92\xff\x00Iw\xff\x00N|\xff\x04b\x9e\xffB\xab\xe6\xff+\xa0\xee\xff=\xa4\xe6\xff\x0ej\xa6\xff\x00L\x7f\xff\x0b[\x93\xffI\xa7\xd9\xff\x1ek\x97\xf2\x00W\x8c]\x00\xa1\xf0\x17\x02Gq\x9d\x056Y\xff\x002X\xff\x00:_\xff\x00Ah\xff\x00Jw\xff\x04Z\x90\xff\x02Z\x97\xff\x00U\x8e\xff\x00Gt\xff\x00<c\xff\x003Y\xff\x057Z\xff\x02Gp\x9d\x00\xa1\xf0\x16\x00\xfa\xff\x03\x00b\x95\x1f\x00-I\xb3\x00"<\xff\x00)G\xff\x002T\xff\x008]\xff\x00;b\xff\x00<d\xff\x009_\xff\x003V\xff\x00)H\xff\x00"<\xff\x00-I\xb3\x00b\x95\x1f\x00\xff\xff\x03\x00\x8b\xce\x00\x00\x15\'\x00\x00In\x18\x00.I\x92\x00#<\xef\x00"<\xff\x00%B\xff\x00(F\xff\x00(F\xff\x00%B\xff\x00"<\xff\x00#<\xef\x00.I\x92\x00In\x18\x00\x16(\x00\x00\x8a\xcc\x00\x00\xaf\xff\x00\x00\x00\x00\x00\x00Ac\x00\x00q\xa6\x05\x009XI\x00-I\xa8\x00\'A\xe4\x00%?\xfb\x00%?\xfb\x00\'A\xe4\x00-I\xa8\x009XI\x00q\xa6\x05\x00Ad\x00\x00\x00\x00\x00\x00\xaf\xff\x00'
image_sprite = pyglet.sprite.Sprite(pyglet.image.ImageData(16, 16, 'RGBA', raw_data), x=10, y=10)
And in both cases, you'll get a result that looks like this:
According to the docs, you like load an image from a file-like object (like a BytesIO) by specifying the file keyword parameter:
kitten_stream = io.BytesIO(some_bytes)
kitten = pyglet.image.load('kitten.png', file=kitten_stream)
(You may need to rewind the file pointer to the beginning of the file by calling kitten_stream.seek(0) before loading).
I used to decode AIS messages with theis package (Python) https://github.com/schwehr/noaadata/tree/master/ais until I started getting a new format of the messages.
As you may know, AIS messages come in two types mostly. one part (one message) or two parts (multi message). Message#5 is always comes in two parts. example:
!AIVDM,2,1,1,A,55?MbV02;H;s<HtKR20EHE:address#hidden#Dn2222222216L961O5Gf0NSQEp6ClRp8,0*1C
!AIVDM,2,2,1,A,88888888880,2*25
I used to decode this just fine using the following piece of code:
nmeamsg = fields.split(',')
if nmeamsg[0] != '!AIVDM':
return
total = eval(nmeamsg[1])
part = eval(nmeamsg[2])
aismsg = nmeamsg[5]
nmeastring = string.join(nmeamsg[0:-1],',')
bv = binary.ais6tobitvec(aismsg)
msgnum = int(bv[0:6])
--
elif (total>1):
# Multi Slot Messages: 5,6,8,12,14,17,19,20?,21,24,26
global multimsg
if total==2:
if msgnum==5:
if nmeastring.count('!AIVDM')==2 and len(nmeamsg)==13: # make sure there are two parts concatenated together
aismsg = nmeamsg[5]+nmeamsg[11]
bv = binary.ais6tobitvec(aismsg)
msg5 = ais_msg_5.decode(bv)
print "message5 :",msg5
return msg5
Now I'm getting a new format of the messages:
!SAVDM,2,1,7,A,55#0hd01sq`pQ3W?O81L5#E:1=0U8U#000000016000006H0004m8523k#Dp,0*2A,1410825672
!SAVDM,2,2,7,A,4hC`2U#C`40,2*76,1410825672,1410825673
Note. the number at the last index is the time in epoch format
I tried to adjust my code to decode this new format. I succeed in decoding messages with one part. My problem is multi message type.
nmeamsg = fields.split(',')
if nmeamsg[0] != '!AIVDM' and nmeamsg[0] != '!SAVDM':
return
total = eval(nmeamsg[1])
part = eval(nmeamsg[2])
aismsg = nmeamsg[5]
nmeastring = string.join(nmeamsg[0:-1],',')
dbtimestring = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(float(nmeamsg[7])))
bv = binary.ais6tobitvec(aismsg)
msgnum = int(bv[0:6])
Decoder can't bring the two lines as one. So decoding fails because message#5 should contain two strings not one. The error i get is in these lines:
if nmeastring.count('!SAVDM')==2 and len(nmeamsg)==13:
aismsg = nmeamsg[5]+nmeamsg[11]
Where len(nmeamsg) is always 8 (second line) and nmeastring.count('!SAVDM') is always 1
I hope I explained this clearly so someone can let me know what I'm missing here.
UPDATE
Okay I think I found the reason. I pass messages from file to script line by line:
for line in file:
i=i+1
try:
doais(line)
Where message#5 should be passed as two lines. Any idea on how can I accomplish that?
UPDATE
I did it by modifying the code a little bit:
for line in file:
i=i+1
try:
nmeamsg = line.split(',')
aismsg = nmeamsg[5]
bv = binary.ais6tobitvec(aismsg)
msgnum = int(bv[0:6])
print msgnum
if nmeamsg[0] != '!AIVDM' and nmeamsg[0] != '!SAVDM':
print "wrong format"
total = eval(nmeamsg[1])
if total == 1:
dbtimestring = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(float(nmeamsg[8])))
doais(line,msgnum,dbtimestring,aismsg)
if total == 2: #Multi-line messages
lines= line+file.next()
nmeamsg = lines.split(',')
dbtimestring = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(float(nmeamsg[15])))
aismsg = nmeamsg[5]+nmeamsg[12]
doais(lines,msgnum,dbtimestring,aismsg)
Be aware that noaadata is my old research code. libais is my production library thst is in use for NOAA's ERMA and WhaleAlert.
I usually make decoding a two pass process. First join multi-line messages. I refer to this as normalization (ais_normalize.py). You have several issues in this step. First the two component lines have different timestamps on the right of the second string. By the USCG old metadata standard, the last one matters. So my code will assume that these two lines are not related. Second, you don't have the required station id field.
Where are you getting the SA from in SAVDM? What device ("talker" in the NMEA vocab) is receiving these messages?
If you're in Ruby, I can recommend the NMEA and AIS decoder ruby gem that I wrote, available on github. It's based on the unofficial AIS spec at catb.org which is maintained by one of Kurt's colleagues.
It handles combining of multipart messages, reads from streams, and supports a large of NMEA and AIS messages. Decoding the 50 binary subtypes of AIS messages 6 and 8 is presently in development.
To handle the nonstandard lines you posted:
!SAVDM,2,1,7,A,55#0hd01sq`pQ3W?O81L5#E:1=0U8U#000000016000006H0004m8523k#Dp,0*2A,1410825672
!SAVDM,2,2,7,A,4hC`2U#C`40,2*76,1410825672,1410825673
It would be necessary to add a new parse rule that accepts fields after the checksum, but aside from that it should go smoothly. In other words, you'd copy the parser line here:
| BANG DATA CSUM { result = NMEAPlus::AISMessageFactory.create(val[0], val[1], val[2]) }
and have something like
| BANG DATA CSUM COMMA DATA { result = NMEAPlus::AISMessageFactory.create(val[0], val[1], val[2], val[4]) }
What do you do with those extra timestamp(s)? It almost looks like they've been appended by whatever software is doing the logging, rather than being part of the actual message.