python-wechaty icon indicating copy to clipboard operation
python-wechaty copied to clipboard

怎么存储语音和图片等文件信息?

Open huangmu168 opened this issue 3 years ago • 5 comments

if msg.type()==MessageType.MESSAGE_TYPE_AUDIO: #语音 audio_file_box = await msg.to_file_box() print(audio_file_box.type()) print(f'保存语音文件<{audio_file_box.name}>') await audio_file_box.to_file('./files/audio_fils/'+audio_file_box.name)

if msg.type()==MessageType.MESSAGE_TYPE_IMAGE: #图片 img_file = msg.to_image() img_name = img_file.image_id print(f'保存图片文件<{img_name}>') try: img_file_box = await img_file.hd() #高清图,是filebox对象 其他还有thumbnail缩略图和 artwork print(img_file_box.type()) await img_file_box.to_file(img_name+'.png') except Exception as e: print(e)

测试:语音文件用to_file_box() 出错 图片文件,我看有to_image()方法,能获取图片id,但是没有save选项,但是可以转化为filebox来to_file。但是,nothing happened

huangmu168 avatar Mar 24 '21 07:03 huangmu168

补充:用img_file_box = await img_file.hd() 会导致整个程序卡住,慎用

huangmu168 avatar Mar 24 '21 07:03 huangmu168

I know that you have a great solution for audio message and parse it to mp3 type file. It's a great contribution If you share it at this issue comment.

Hope your sharing.

wj-Mcat avatar May 12 '21 07:05 wj-Mcat

怎样判断图片类型是GIF或png?

JoffreyN avatar Aug 04 '21 07:08 JoffreyN

怎样判断图片类型是GIF或png?

file_box = await msg.to_file_box()
if file_box:
    await file_box.to_file(file_box.name)

JoffreyN avatar Aug 04 '21 08:08 JoffreyN

Yes, as your code showing, you can get the type of image depending on the file.

wj-Mcat avatar Aug 04 '21 11:08 wj-Mcat