为什么Windows上的Python不能读取二进制模式的图像? [英] Why Python on Windows can't read an image in binary mode?

查看:154
本文介绍了为什么Windows上的Python不能读取二进制模式的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读的二进制模式的图像,这样我可以把它保存到我的数据库,像这样的:

I want to read a image in binary mode so that I could save it into my database, like this:

img = open("Last_Dawn.jpg")
t = img.read()
save_to_db(t)

这是工作在Mac上。但在Windows上,有什么img.read()不正确。这只是一点点出来的一整套。

This is working on Mac. But on Windows, what img.read() is incorrect. It's just a little out of the whole set.

所以我的第一个问题是:为什么code以上不工作在Windows

So my first question is: why code above doesn't work in Windows?

和第二个是:是否有任何其他的方式来做到这一点。

And second is: is there any other way to do this?

非常感谢!

推荐答案

不能肯定地说,但我知道,打电话时ISO C标准不二进制和非二进制模式区分的fopen ,但视窗的确实

Can't say for sure but I do know that the ISO C standard doesn't distinguish between the binary and non-binary modes when calling fopen and yet Windows does.

这可能是Python的code只是使用的fopen(Last_Dawn.jpg,R)在幕后(因为它是用C语言编写)和这是Windows被打开的非二进制模式。

It's likely that the Python code just uses fopen("Last_Dawn.jpg","r") under the covers (since it's written in C) and this is being opened in Windows in non-binary mode.

这将最有可能转换行结束字符( LF - > CRLF )。可能还有其他

This will most likely convert line end characters (LF -> CRLF) and possibly others.

如果您自己指定模式上的公开声明RB,这应该修复它:

If you yourself specify the mode as 'rb' on your open statement, that should fix it:

img = open("Last_Dawn.jpg", "rb")

这篇关于为什么Windows上的Python不能读取二进制模式的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆