简体   繁体   中英

Python urlretrieve downloading corrupted images

I am downloading a list of images (all .jpg) from the web using this python script:

__author__ = 'alessio'

import urllib.request

fname = "inputs/skyscraper_light.txt"

with open(fname) as f:
    content = f.readlines()


for link in content:
    try:
        link_fname = link.split('/')[-1]
        urllib.request.urlretrieve(link, "outputs_new/" + link_fname)
        print("saved without errors " + link_fname)
    except:
        pass

In OSX preview I see the images just fine, but I can't open them with any image editor (for example Photoshop says "Could not complete your request because Photoshop does not recognize this type of file."), and when i try to attach them to a word document, the files are not even showed as picture files in the dialog for browsing for image.

What am i doing wrong?

As JF Sebastian suggested me in the comments, the issue was related to the newline in the filename.

To make my script work, you need to replace

link_fname = link.split('/')[-1]

with

link_fname = link.strip().split('/')[-1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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