Writing exif data to the exif header using PyExifTool - python-3.x

Looking over a post from 2015 link on how to use PyExifTool to write to the Exif header. I gave it a try:
import exiftool
fileno=r'DSC00001.JPG
with exiftool.ExifTool() as et:
et.execute("EXIF:GPSLongitude=100",fileno)
et.execute("EXIF:GPSLatitude=100",fileno)
In response, I got the following error:
TypeError: sequence item 0: expected a bytes-like object, str found
Then as specified in the documentation, execute takes byte commands, so I bites, so I tried that too:
with exiftool.ExifTool() as et:
et.execute(bytes("EXIF:GPSLongitude=100", 'utf-8'),fileno)
et.execute(bytes("EXIF:GPSLatitude=50",'utf-8'),fileno)
But still got the same error :
TypeError: sequence item 1: expected a bytes-like object, str found
I am not sure what am I doing wrong, and if Exiftool can write to file.

The problem is that the execute method is low-level and requires bytes as inputs for both the parameters you pass and the file name. Try this:
import exiftool
pic = b"DSC00001.JPG"
with exiftool.ExifTool() as et:
et.execute(b"-GPSLatitude=11.1", pic)
tag = et.get_tag("EXIF:GPSLatitude", pic)
print(tag)

#Gracias!
exif = r'...\exiftool.exe'
file=br"...\FRM_20220111_134802.JPG"
with exiftool.ExifTool(exif) as et:
et.execute(b"-DateTimeOriginal=2022:10:10 10:10:10", file)
tag = et.get_tag("EXIF:DateTimeOriginal", file)
...
#RCM_Chile

Related

How do I use importlib.resources with pickle files?

I'm trying to load a pickle file with importlib.resources, but I'm getting the following error:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
The bit that is raising the error is:
with importlib.resources.open_text("directory_with_pickle_file", "pickle_file.pkl") as f:
data = pickle.load(f)
I'm certain that the file (pickle_file.pkl) was created with pickle.dump.
What am I doing wrong?
Through lots of trial and error I figured out that importlib.resources has a read_binary function which can be used to read pickled files like so:
text = importlib.resources.read_binary("directory_with_pickle_file", "pickle_file.pkl")
data = pickle.loads(text)
Here, data is the pickled object.

Getting the "bytes-like object is required" error, but the fix causes a "write() arguments must be str" error

This is a piece of a script that is giving me fits. If I use 'wb', I get the "bytes like" error. If I change it to 'w', I get the "write() arguments" error.
with open(output_path+filename, 'wb') as f:
f.write('<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n')
f.write(ET.tostring(elem, pretty_print = True))
If I have 'wb', it gives me this error:
f.write("<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n")
TypeError: a bytes-like object is required, not 'str'
If I have 'w', it gives me:
f.write(ET.tostring(elem, pretty_print = True))
TypeError: write() argument must be str, not bytes
The reason I'm doing it like this, is because I'm using lxml to take one HUGE XML sheet with several files'-worth of metadata and split it up into several XML files. So the like with the UTF-8 encoding is so that each individual file, has that written at the top. Without posting a ton of code, ET = lxml.etree.

Protobuf RuntimeWarning : Unexpected end-group tag Not all data was converted

I have this utf-8 encoded file from which i need to collect the hexadecimal dump which is protobuf viable and then feed it to the protobuf.The .proto file works as expected and life is almost perfect.
message_content = message_content.replace(" ","")
message_content = binascii.unhexlify(message_content)
I convert the string to raw bytes and then feed it to the protobuf
msg.ParseFromString(message_content)
from which results the error
RuntimeWarning: Unexpected end-group tag: Not all data was converted
msg.ParseFromString(message_content)
I can't tell if I collect the hex part poorly or if its corrupted.
message_content looks like this:
b"87\x00\x00C\x17\x11\x10j\x17\x11\x10\x0c\x00\xc2\x00\x08\xec\xad\xe8\xe0\xf9\x04\x10\x01\x1a\x1f\x08\xea\xae\x18\x12\x14\x01\x00\x0f\x00\x02\x02|\xf0%\x00\x01&\x00\x01'\x00\x01*\x00\x01*\x01\x00\x1a\x00\x1a \x08\xea\xae\x14\x12\x14\x01\x00\x0f\x00\x02\x02|\xf0%\x00\x01&\x00\x01'\x00\x01(\x00\x01*\x02\x00\x00\x1a\x00\x1a#\x08\xea.\x12\x14\x01\x00\x0f\x00\x02\x02|\xf0%\x00\x01&\x00\x011\x00\x012\x00\x01*\x06\x00\x00\x00\x00\x00\x00\x1a\x00\x1a \x08\xea\xae\x14\x12\x14\x01\x00\x0f\x00\x02\x02|\xf0%\x00\x01&\x00\x01'\x00\x01(\x00\x02*\x02\x00\x00\x1a\x00\x1a\x1d\x08\xea\xae\x0c\x12\x11\x01\x00\x0f\x00\x02\x02|\xf0%\x00\x01&\x00\x011\x00\x01*\x02\x00\x00\x1a\x00"
I had a similar problem. Finally I found there is a problem with the data provided by the upstream source. You can also check whether there is a problem with the base64 data source.
I met the same err.
the err code is:
rsp_serialize = base64.b64decode(str(rsp))
item_info = StrucItem()
item_info.ParseFromString(rsp_serialize)
the right code is:
rsp_serialize = base64.b64decode(str(rsp, encoding="utf-8"))
item_info = StrucItem()
item_info.ParseFromString(rsp_serialize)
so you should check your upstream data is ok?

Stripping newlines off of read file doesn't work

I have a function that is supposed to read a file as bytes and strip off newline characters, but when I try to use .strip() it gives me the error TypeError: a bytes-like object is required, not 'str', so then I try to encode it using .encode('utf-8') before stripping, and I get AttributeError: 'bytes' object has no attribute 'encode'. I don't really know where to begin with this problem. Here's the code:
file = open(str(filename + ".data"), "rb")
file.seek(0)
array = file.readlines()
b = array[lineNumber].strip('\n\r')
The file is encrypted bytes that I'm trying to feed into a decryption function to get ascii.
This comment showed me that I needed to use .strip() with bytes instead of a string: .strip(b'\n\r') instead of .strip('\n\r') since I was stripping bytes.

Python3 open text file

I am trying to open a simple text file using
dir_path = os.path.dirname(os.path.realpath(__file__))
F = open(dir_path+"\\sankey2.txt","r")
The path to the file is :C:\Users\David\Google Drive\Jobs\1.Efe\Transporte\0.MODEL
Inside the text file is the following text :
sankey2.txt
$SETGLOBAL REFERENCIA
Now if I run python it gives me the error :
TypeError: decoding to str: need a bytes-like object, _io.TextIOWrapper found
So I tried the following 2 solutions :
Trying this
F = bytes(open(dir_path+"\\sankey2.txt","r"))
I get
TypeError: 'str' object cannot be interpreted as an integer
Trying this
F = open(dir_path+"\\sankey2.txt","rb")
I get
TypeError: decoding to str: need a bytes-like object, _io.BufferedReader found
It is not entirely clear to me how to solve this problem. Encoding to UTF-8 doesn't help either.
Thanks for your help.

Resources