试图在python中下载图像,但它不起作用

时间:2018-05-30 16:25:24

标签: python python-3.x http

我正在尝试在python中创建一个脚本,从discord服务器下载图像,但它不起作用

我的代码

import http.client, json, calendar, os, time, base64
from datetime import datetime


app_token = "token"
channel_id = "id"
web_hk = "api/webhooks/my webhook"
latest_timestamp = ""

def query_server():
    global app_token, channel_id, latest_timestamp

    response = 'Processing...'


    conn = http.client.HTTPSConnection("discordapp.com")
    headers = {"authorization": "Bot " + app_token }
    conn.request("GET", "/api/channels/" + channel_id +"/messages", "", headers)
    r1 = conn.getresponse()

    status = r1.reason
    print(status)

    r = r1.read()
    print(r)
    conversation = json.loads(r.decode('utf-8'))

#    print(json.dumps(conversation, indent=4, sort_keys=True))

    i = 0
    while i < len(conversation) :
        comment = conversation[i]
        i += 1

        timestamp = comment["timestamp"]

        if(timestamp <= latest_timestamp) :
            break

        print(comment)

        if(comment['content'] == 'Go!') :

            print('parsing command')

            style_comment = conversation[i]
            if style_comment['attachments'] == [] :
                response = 'Missing style image'
                break

            content_comment = conversation[i + 1]
            if content_comment['attachments'] == []:
                response = 'Missing content image'
                break


            conn = http.client.HTTPSConnection("cdn.discordapp.com")
            current_time_int = str(int(time.mktime(datetime.utcnow().timetuple())))


            # download style image
            url = style_comment['attachments'][0]['url']
            img_path = url.split("https://cdn.discordapp.com")[1]

            t = url.split("/")
            style_img_filename = current_time_int + "-" + t[-1]

            conn.request("GET", img_path, "", headers)
            r1 = conn.getresponse().read()

            style_file = open(style_img_filename, "wb")
            style_file.write(base64.encodebytes(r1))
            style_file.close()
            os.chmod(style_img_filename, 0o777)

            # download content image
            url = content_comment['attachments'][0]['url']
            img_path = url.split("https://cdn.discordapp.com")[1]

            t = url.split("/")
            content_img_filename = current_time_int + "-" + t[-1]

            conn.request("GET", img_path, "", headers)
            r1 = conn.getresponse().read()

            content_file = open(content_img_filename, "wb")
            content_file.write(base64.encodebytes(r1))
            content_file.close()
            os.chmod(content_img_filename, 0o777)

            output_img_filename = current_time_int + "-output.jpg"


            cmd = "python neural_style.py --content {} --styles {} --output {} --width 500".format(content_img_filename, style_img_filename, output_img_filename)

            print(cmd)
            os.system(cmd)

            break

    print(response)

query_server()

我得到了什么

Traceback (most recent call last):
  File "neural_style.py", line 216, in <module>
    main()
  File "neural_style.py", line 119, in main
    content_image = imread(options.content)
  File "neural_style.py", line 201, in imread
    img = scipy.misc.imread(path).astype(np.float)
  File "C:\Users\Baxter\AppData\Local\Programs\Python\Python35\lib\site-packages\numpy\lib\utils.py", line 101, in newfunc
    return func(*args, **kwds)
  File "C:\Users\Baxter\AppData\Local\Programs\Python\Python35\lib\site-packages\scipy\misc\pilutil.py", line 164, in imread
    im = Image.open(name)
  File "C:\Users\Baxter\AppData\Local\Programs\Python\Python35\lib\site-packages\PIL\Image.py", line 2585, in open
    % (filename if filename else fp))
OSError: cannot identify image file '1527709726-madelbrot.jpg'


它技术上下载了一些东西,因为我在文件夹中看到了文件名,但它说它无法识别它。我甚至无法打开它。

0 个答案:

没有答案