简体   繁体   English

无法使用python 2.7下载图像

[英]Can't download images with python 2.7

I'm kinda new to Python. 我对Python有点陌生。 I want to download and rescale many images at once so I can use them as negative samples to train a haar cascade. 我想一次下载并重新缩放许多图像,以便可以将它们用作负样本来训练haar级联。 Maybe I mixed some python 3.0 code in mine. 也许我在我的里面混了一些python 3.0代码。 I got the following error: 我收到以下错误:

Traceback (most recent call last):
File "D:/!Servici/Python/Remake/negative_grab.py", line 26, in <module> store_raw_images()
File "D:/!Servici/Python/Remake/negative_grab.py", line 7, in store_raw_images
neg_image_urls = urllib2.urlopen(neg_images_link).read().decode()
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4837: ordinal not in range(128)
Process finished with exit code 1

My code: 我的代码:

import urllib2
import cv2
import os

def store_raw_images():
    neg_images_link = 'http://image-net.org/api/text/imagenet.synset.geturls?wnid=n04335209'
    neg_image_urls = urllib2.urlopen(neg_images_link).read().decode()

    if not os.path.exists('neg_sample'):
        os.makedirs('neg_samples')

    pic_num = 1

    for i in neg_image_urls.split('\n'):
        try:
            print(i)
            urllib2.urlretrieve(i, "neg/"+str(pic_num)+'.jpg')
            img = cv2.imread("neg/"+str(pic_num)+'.jpg', cv2.IMREAD_GRAYSCALE)
            resize_image = cv2.resize(img, (100,100))
            cv2.imwrite(("neg/")+str(pic_num)+'.jpg', resize_image)
            pic_num += 1

        except Exception as e:
            print (str(e))

    store_raw_images()

Your decode is running into errors. 您的解码出现错误。 Try with decode('utf-8') 尝试使用decode('utf-8')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM