Skip to content

Instantly share code, notes, and snippets.

@MrCrap
Created January 15, 2018 06:27
Show Gist options
  • Save MrCrap/b6202e5ee70750b96368253454b5d39c to your computer and use it in GitHub Desktop.
Save MrCrap/b6202e5ee70750b96368253454b5d39c to your computer and use it in GitHub Desktop.

Revisions

  1. MrCrap created this gist Jan 15, 2018.
    33 changes: 33 additions & 0 deletions ImgDownloader.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    def imgDownloader(self, url, filename):
    name_replce = filename.replace(' ', '-')
    fileext = os.path.splitext(url)[1]
    full_name = name_replce+fileext
    if not url:
    return ""
    try:
    filepath = os.path.join("img",full_name)
    if not os.path.exists("img"):
    os.makedirs("img")
    urllib.urlretrieve(url,filepath)
    if fileext in ('.jpg','.jpeg','.gif','.png','.svg'):
    full_name = full_name

    except Exception as e:
    # print("fetch {} error:{}".format(url,e),sys.stderr)
    full_name = ""

    if full_name:
    import PIL
    from PIL import Image
    path = os.getcwd()+"/img/"+full_name
    basewidth = 320
    img = Image.open(path)
    wpercent = (basewidth / float(img.size[0]))
    hsize = int((float(img.size[1]) * float(wpercent)))
    img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
    img.save(os.getcwd()+"/img/"+name_replce+'-thumb'+fileext)
    thumb = name_replce+'-thumb'+fileext
    else:
    thumb = ""

    return full_name, thumb