有没有办法在不读取整个文件的情况下推断文件的图像格式?

发布于 2024-07-05 13:26:05 字数 189 浏览 6 评论 0原文

有没有一种好方法可以查看图像的格式,而无需将整个文件读入内存?

显然,这会因格式的不同而有所不同(我对 TIFF 文件特别感兴趣),但是什么样的过程有助于确定文件的图像格式而无需读取整个文件?

奖励:如果图像是 Base64 编码的字符串怎么办? 在解码之前有什么可靠的方法来推断它吗?

Is there a good way to see what format an image is, without having to read the entire file into memory?

Obviously this would vary from format to format (I'm particularly interested in TIFF files) but what sort of procedure would be useful to determine what kind of image format a file is without having to read through the entire file?

BONUS: What if the image is a Base64-encoded string? Any reliable way to infer it before decoding it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

天涯离梦残月幽梦 2024-07-12 13:26:05

大多数图像文件格式在开头都有唯一的字节。 unix file 命令查看文件的开头以查看它包含的数据类型。 请参阅关于文件中的幻数magicdb.org

Most image file formats have unique bytes at the start. The unix file command looks at the start of the file to see what type of data it contains. See the Wikipedia article on Magic numbers in files and magicdb.org.

灰色世界里的红玫瑰 2024-07-12 13:26:05

当然有。 就像其他人提到的那样,大多数图像都以某种“魔法”开始,它总是会转换为某种 Base64 数据。 以下是几个示例:

位图将以 Qk3 开头

Jpeg 以 /9j/

开头 GIF 以 R0l 开头(即零作为第二个字符)。

等等。 获取不同的图像类型并找出它们编码的内容并不难。 请小心,因为有些拥有不止一件魔法,因此您需要在 B64“翻译代码”中考虑它们。

Sure there is. Like the others have mentioned, most images start with some sort of 'Magic', which will always translate to some sort of Base64 data. The following are a couple examples:

A Bitmap will start with Qk3

A Jpeg will start with /9j/

A GIF will start with R0l (That's a zero as the second char).

And so on. It's not hard to take the different image types and figure out what they encode to. Just be careful, as some have more than one piece of magic, so you need to account for them in your B64 'translation code'.

尾戒 2024-07-12 13:26:05

在 *nix 命令行上执行 file 或读取文件的初始字节。 大多数文件的前几个字节都有一个唯一的标头。 例如,TIFF 的标​​头如下所示:

0x00000000: 4949 2a00 0800 0000

For more information on the TIFF file format specifically if you'd like to know what those bytes stand for, go here.

Either file on the *nix command-line or reading the initial bytes of the file. Most files come with a unique header in the first few bytes. For example, TIFF's header looks something like this:

0x00000000: 4949 2a00 0800 0000

For more information on the TIFF file format specifically if you'd like to know what those bytes stand for, go here.

遗失的美好 2024-07-12 13:26:05

TIFF 将以 II 或 MM(Intel 字节排序或 Motorolla)开头。
TIFF 6 规范可以在此处下载,但并非如此太难遵循

TIFFs will begin with either II or MM (Intel byte ordering or Motorolla).
The TIFF 6 specification can be downloaded here and isn't too hard to follow

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文