读取 PNG 图像文件时出现致命错误:在 Ubuntu 20.04 LTS 中不是 PNG 文件
Fatal error reading PNG image file: Not a PNG file in Ubuntu 20.04 LTS
我尝试使用 requests module in python.It works but when i try to open this image it showing "Fatal error reading PNG image file: Not a PNG file". Here 下载图像是我的错误 screenshot.And 我用来下载的代码是,
import requests
img_url = "http://dimik.pub/wp-content/uploads/2020/02/javaWeb.jpg"
r = requests.get(img_url)
with open("java_book.png","wb") as f:
f.write(r.content)
而我 运行 我在终端中的代码只是说 python3 s.py
(s.py 是文件名)。
我的代码有问题还是我的操作系统有其他问题(ubuntu 20.04 LTS)?
这是因为您试图将 javaWeb.jpg
(A JPG
文件)保存为 java_book.png
(A PNG
文件)。
为了了解我们正在做什么,我尝试重现了这个问题,请查看下面的发现。
1.) 您试图打开的文件是整个 HTML 文档。我可以支持这一点,因为我们在您的 'wb' 或 WRITE BINARY 命令的开头找到 !DOCTYPE html。
<---------------------------------------- -- 我们陷入僵局
从这里我们有几个选项可以解决我们的问题。
a.) 我们可以简单地从网页下载图像 - 将它放在本地 folder/directory/ 或任何你想要的地方。这是迄今为止我们最简单的调用,因为它允许我们稍后调用和打开它而无需做太多事情。当我在 Windows 机器上时 - Ubuntu 应该也没有问题(除非你不在带有 GUI 的 UBUNTU 中 - 可以用 startx 如果支持)
b.) 如果您必须直接从网站本身拉取内容,您可以使用此答案 here 中的 BEAUTIFULSOUP 尝试类似的操作。老实说,我从来没有真正使用过后一个选项,因为下载和移动更有效。
您只需要将图像保存为 JPG
。
import requests
img_url = "http://dimik.pub/wp-content/uploads/2020/02/javaWeb.jpg"
r = requests.get(img_url)
with open("java_book.jpg","wb") as f:
f.write(r.content)
是的,这是一个完整的 HTML 文档:
import requests
response = requests.get("https://devnote.in/wp-content/uploads/2020/04/devnote.png")
file = open("sample_image.png", "wb")
file.write(response.content)
print (response.content)
file.close()
https://devnote.in/wp-content/uploads/2020/04/devnote.png 这个 url 是禁用 mod_security。所以这个 return 错误就像:
<html><head><title>Not Acceptable!</title></head><body><h1>Not Acceptable!</h1><p>An appropriate representation of the requested resource could not be found on this server. This error was generated by Mod_Security.</p></body></html>
.
在 apache 服务器上禁用 mod_security 使用 .htaccess
Mod_security 可以在 .htaccess 的帮助下轻松禁用。
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
我尝试使用 requests module in python.It works but when i try to open this image it showing "Fatal error reading PNG image file: Not a PNG file". Here 下载图像是我的错误 screenshot.And 我用来下载的代码是,
import requests
img_url = "http://dimik.pub/wp-content/uploads/2020/02/javaWeb.jpg"
r = requests.get(img_url)
with open("java_book.png","wb") as f:
f.write(r.content)
而我 运行 我在终端中的代码只是说 python3 s.py
(s.py 是文件名)。
我的代码有问题还是我的操作系统有其他问题(ubuntu 20.04 LTS)?
这是因为您试图将 javaWeb.jpg
(A JPG
文件)保存为 java_book.png
(A PNG
文件)。
为了了解我们正在做什么,我尝试重现了这个问题,请查看下面的发现。
1.) 您试图打开的文件是整个 HTML 文档。我可以支持这一点,因为我们在您的 'wb' 或 WRITE BINARY 命令的开头找到 !DOCTYPE html。
<---------------------------------------- -- 我们陷入僵局
从这里我们有几个选项可以解决我们的问题。
a.) 我们可以简单地从网页下载图像 - 将它放在本地 folder/directory/ 或任何你想要的地方。这是迄今为止我们最简单的调用,因为它允许我们稍后调用和打开它而无需做太多事情。当我在 Windows 机器上时 - Ubuntu 应该也没有问题(除非你不在带有 GUI 的 UBUNTU 中 - 可以用 startx 如果支持)
b.) 如果您必须直接从网站本身拉取内容,您可以使用此答案 here 中的 BEAUTIFULSOUP 尝试类似的操作。老实说,我从来没有真正使用过后一个选项,因为下载和移动更有效。
您只需要将图像保存为 JPG
。
import requests
img_url = "http://dimik.pub/wp-content/uploads/2020/02/javaWeb.jpg"
r = requests.get(img_url)
with open("java_book.jpg","wb") as f:
f.write(r.content)
是的,这是一个完整的 HTML 文档:
import requests
response = requests.get("https://devnote.in/wp-content/uploads/2020/04/devnote.png")
file = open("sample_image.png", "wb")
file.write(response.content)
print (response.content)
file.close()
https://devnote.in/wp-content/uploads/2020/04/devnote.png 这个 url 是禁用 mod_security。所以这个 return 错误就像:
<html><head><title>Not Acceptable!</title></head><body><h1>Not Acceptable!</h1><p>An appropriate representation of the requested resource could not be found on this server. This error was generated by Mod_Security.</p></body></html>
.
在 apache 服务器上禁用 mod_security 使用 .htaccess
Mod_security 可以在 .htaccess 的帮助下轻松禁用。
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>