Best Way To Remove First 6 Bytes, And Very Last Byte. Python
I'm simply looking for the best way to take a file, remove the first 6 bytes, and the very last byte, then save it as a .JPG format. (Original file is a .TEC format, used as Cache
Solution 1:
Not sure what you mean by "best", but probably the easiest way is to just read it all in and slice the string:
fp = open(filename, "rb")
data = fp.read()
fp.close()
fp = open(jpegfilename, "wb")
fp.write(data[6:-1])
fp.close()
Edit:
As pointed out in the comments, if your JPEG is very large, reading the whole thing at once could exhaust your memory. Instead, you could read it a bit at a time, like this:
with open(filename, "rb") as ifile:
with open(jpegfilename, "wb") as ofile:
ifile.read(6)
prev = None
while True:
chunk = ifile.read(4096)
if chunk:
if prev:
ofile.write(prev)
prev = chunk
else:
break
if prev:
ofile.write(prev[:-1])
But given that most JPEG's probably aren't going to come anywhere close to exhausting your memory, this is probably way more complicated than you really need.
Post a Comment for "Best Way To Remove First 6 Bytes, And Very Last Byte. Python"