字节明智的修改在JPG图像的特定范围内(被视为二进制对象)
我正在测试在.jpg文件的末尾编写数据。众所周知,所有.jpg文件以FFD8开头,并以FFD9结束。利用这个事实,我成功地写了“ Hello World!”在我的.jpg文件的末尾,带有看起来像这样的函数:
def writte_at_the_end_of_jpg_fct (img_path:str, msg:str):
with open(img_path, 'ab') as fimg:
fimg.write(b"Hello World!")
但是现在我该怎么做才能仅删除文件末尾添加的数据(这意味着“ Hllo World!”
我尝试了这一点:
def erase_data_in_img_fct(img_jpg_file_path: str) -> None:
with open(img_jpg_file_path, 'rb') as fimg:
content = fimg.read()
offset = content.index(bytes.fromhex('FFD9'))
end_i = fimg.seek(offset + 2)
with open(img_jpg_file_path, 'ab') as fimg:
fimg = fimg[0:end_i]
但是它不起作用,我获取此错误:
typeError:'_io.bufferedwriter'对象不可订阅
我在网络上搜索了很多时间,答案是我的问题,但找不到。
谢谢
I am testing to write data at the end of a .jpg file. As i know all the .jpg files begin by FFD8 and end by FFD9. Using that fact i succeeded in writting "Hello World!" at the end of my .jpg file with a function that looks like this:
def writte_at_the_end_of_jpg_fct (img_path:str, msg:str):
with open(img_path, 'ab') as fimg:
fimg.write(b"Hello World!")
But now how can i do to remove only the data that i ve added at the end of the file (that means "Hllo World!"
I ve tried this:
def erase_data_in_img_fct(img_jpg_file_path: str) -> None:
with open(img_jpg_file_path, 'rb') as fimg:
content = fimg.read()
offset = content.index(bytes.fromhex('FFD9'))
end_i = fimg.seek(offset + 2)
with open(img_jpg_file_path, 'ab') as fimg:
fimg = fimg[0:end_i]
But it didn't work, i get this error:
TypeError: '_io.BufferedWriter' object is not subscriptable
I have searched a lot of time on the web an answer to my problem and didn't found it.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用 bytes.rindex ,因为
> code>> code>> FFD9
字节可能在文件中多次发生:You should use bytes.rindex instead because the
ffd9
bytes may occur multiple times in the file: