字节明智的修改在JPG图像的特定范围内(被视为二进制对象)

发布于 2025-02-05 23:10:11 字数 978 浏览 3 评论 0原文

我正在测试在.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!")

And Indeed i get:
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日裸衫吸 2025-02-12 23:10:11

您应该使用 bytes.rindex ,因为> code>> code>> FFD9字节可能在文件中多次发生:

$ diff -u old.py new.py 
--- old.py  2022-06-08 08:07:33.381031019 +0100
+++ new.py  2022-06-08 08:07:45.581315987 +0100
@@ -1,8 +1,8 @@
 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'))
+        offset = content.rindex(bytes.fromhex('FFD9'))
         end_i = fimg.seek(offset + 2)
 
-    with open(img_jpg_file_path, 'ab') as fimg:
-        fimg = fimg[0:end_i]
+    with open(img_jpg_file_path, 'wb') as fimg:
+        fimg.write(content[:end_i])

You should use bytes.rindex instead because the ffd9 bytes may occur multiple times in the file:

$ diff -u old.py new.py 
--- old.py  2022-06-08 08:07:33.381031019 +0100
+++ new.py  2022-06-08 08:07:45.581315987 +0100
@@ -1,8 +1,8 @@
 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'))
+        offset = content.rindex(bytes.fromhex('FFD9'))
         end_i = fimg.seek(offset + 2)
 
-    with open(img_jpg_file_path, 'ab') as fimg:
-        fimg = fimg[0:end_i]
+    with open(img_jpg_file_path, 'wb') as fimg:
+        fimg.write(content[:end_i])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文