如何使用 Python 处理“作为字节字符串的图像数据”?

发布于 2024-11-10 18:12:08 字数 1172 浏览 1 评论 0原文

首先,我将解释我想要做什么,然后解释我的问题。

好的,我一直在制作一个项目的原型,我希望在整个夏天进行该项目。该项目是一个小型媒体播放器,要求之一是显示 mp3 文件的专辑插图。

经过一番研究,我发现了这个库: http://code.google.com/p/mutagen/

我选择此库的原因是因为它没有任何依赖项,它将使我的应用程序更加可移植。

在摆弄代码后,我能够从 mp3 文件中检索数据,例如艺术家和录音年份等。我将在这里进行一些演示:

from mutagen.mp3 import MP3
audio = MP3("born.mp3")
artist = audio["TPE1"]
print artist

如果这没有多大意义,这是教程由 mutagen 提供 - http://code.google.com/p/mutagen/wiki/教程(有点短)

问题 - 我想使用 pygame 显示图像。通常这会读成这样:

 monkey = pygame.image.load("monkey.jpg")

 screen.blit(monkey,(0,0))

但我想使用图像文件中的图像。因此,从第一个示例开始,它的内容如下:

audio = MP3("born.mp3")
data = audio.tags['APIC:'].data
monkey = pygame.image.load(data)

但是 pygame 会抛出异常错误,因为变量“value”的数据类型是字节字符串形式的原始图像数据。

问题 - 是否可以将字节字符串转换为某种图像格式以便 pygame 可以使用它?

有关图像的 python 文档 - http://www.pygame.org/docs/ref/image .html

我不确定是否可以做到我所要求的,所以请原谅我!

First I'm going to explain what I'm trying to do and then explain my problem.

Okay so I've been making prototypes on a project that I hope to be working on over the summer. The project is a small media player and one of the requirements is to display the albulm artwork of an mp3 file.

After some research I found this library:
http://code.google.com/p/mutagen/

The reason I chose this library was because it doesn't have any dependencies and it would make my application more portable.

After fidilling around with the code I was able to retrieve data from the mp3 file, such as the artist and recording year etc. I'll give a little demonstration here:

from mutagen.mp3 import MP3
audio = MP3("born.mp3")
artist = audio["TPE1"]
print artist

In case this doesn't make much sense, this is the tutorial that is provided by mutagen - http://code.google.com/p/mutagen/wiki/Tutorial (its a little bit short)

Problem - I want to display the image using pygame. Normally this would read something like this:

 monkey = pygame.image.load("monkey.jpg")

 screen.blit(monkey,(0,0))

but instead I want to do use the image from the image file. So going from the first example it reads something like:

audio = MP3("born.mp3")
data = audio.tags['APIC:'].data
monkey = pygame.image.load(data)

but pygame throws an exception error because the data type of the variable 'value' is raw image data as a byte string.

Question - Is it possible to convert the byte string to some sort of image format so pygame can use it?

For python documentation on images - http://www.pygame.org/docs/ref/image.html

I'm not sure if it is even possible to do what I am asking so please forgive me!

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

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

发布评论

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

评论(1

七月上 2024-11-17 18:12:08

您尝试过 pygame.image.fromstring() 吗?

注意:由于它是原始数据,因此您需要提前了解一些细节,例如分辨率和颜色深度。

Have you tried pygame.image.fromstring()?

note: since it's raw data, you'll need to know in advance a few details such as the resolution and colour depth.

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