2016年6月28日火曜日

開発環境

  • OS X El Capitan - Apple (OS)
  • Emacs (Text Editor)
  • Python 3.5 (プログラミング言語)

実践 コンピュータビジョン (Jan Erik Solem(著)、相川 愛三(翻訳)、オライリージャパン)の付録B(画像のデータセット)、B.1(Flickr)を取り組んでみる。

B.1(Flickr)

コード(Emacs)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# flickdr.py の修正と、urllib の python2 から python3 への変更点を反映
import flickr
import urllib
from urllib.request import URLopener
import os
import sys

if len(sys.argv) > 1:
    tag = sys.argv[1]
else:
    print('no tag specified')
    sys.exit(1)

f = flickr.photos_search(tags=tag)
urllist = []

for i, k in enumerate(f):
    url = k.getURL(size='Medium', urlType='source')
    urllist.append(url)
    image = URLopener()
    image.retrieve(url, os.path.basename(urllib.parse.urlparse(url).path))
    print('downloading: {0}'.format(url))
    if i == 9:
        break

with open('urllist.txt', 'w') as fl:
    for url in urllist:
        print(url, file=fl)

入出力結果(Terminal, IPython)

$ 2to3-3.5 --no-diffs -w flickr.py
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored flickr.py
RefactoringTool: Files that were modified:
RefactoringTool: flickr.py
RefactoringTool: Warnings/messages while refactoring:
RefactoringTool: ### In file flickr.py ###
RefactoringTool: Line 1400: You should use a for loop here
$ ./tagdownload.py goldengatebridge
downloading: https://farm8.staticflickr.com/7656/27915684436_9d9ebcd395.jpg
downloading: https://farm8.staticflickr.com/7454/27666970780_b5f8f04474.jpg
downloading: https://farm8.staticflickr.com/7416/27911835266_40c9e513df.jpg
downloading: https://farm8.staticflickr.com/7265/27332098343_a5fa54fc3a.jpg
downloading: https://farm8.staticflickr.com/7646/27324894703_16f79f4a6b.jpg
downloading: https://farm8.staticflickr.com/7725/27324894393_8edaeb8cc1.jpg
downloading: https://farm8.staticflickr.com/7384/27323625454_889118019a.jpg
downloading: https://farm8.staticflickr.com/7282/27316898543_f6a0fd4088.jpg
downloading: https://farm8.staticflickr.com/7214/27651421120_12c9ee312e.jpg
downloading: https://farm8.staticflickr.com/7718/27317310954_9a3226ce8b.jpg
$ cat urllist.txt 
https://farm8.staticflickr.com/7656/27915684436_9d9ebcd395.jpg
https://farm8.staticflickr.com/7454/27666970780_b5f8f04474.jpg
https://farm8.staticflickr.com/7416/27911835266_40c9e513df.jpg
https://farm8.staticflickr.com/7265/27332098343_a5fa54fc3a.jpg
https://farm8.staticflickr.com/7646/27324894703_16f79f4a6b.jpg
https://farm8.staticflickr.com/7725/27324894393_8edaeb8cc1.jpg
https://farm8.staticflickr.com/7384/27323625454_889118019a.jpg
https://farm8.staticflickr.com/7282/27316898543_f6a0fd4088.jpg
https://farm8.staticflickr.com/7214/27651421120_12c9ee312e.jpg
https://farm8.staticflickr.com/7718/27317310954_9a3226ce8b.jpg
$ ls *.jpg
27316898543_f6a0fd4088.jpg 27332098343_a5fa54fc3a.jpg
27317310954_9a3226ce8b.jpg 27651421120_12c9ee312e.jpg
27323625454_889118019a.jpg 27666970780_b5f8f04474.jpg
27324894393_8edaeb8cc1.jpg 27911835266_40c9e513df.jpg
27324894703_16f79f4a6b.jpg 27915684436_9d9ebcd395.jpg
$  

0 コメント:

コメントを投稿