2017-02-07 8 views
1

MomentumDash(教育目的のみ)からいくつかの画像をダウンロードしようとしています。 私は、次のPythonコードを書かれている:ダウンロードしたイメージが常に背景として設定されていませんか?

import urllib 
import os 
import random 


#Chooses an image between 1 to 14 
choice=random.randint(01,14) 
print choice 

#Downloads images 
a=urllib.urlretrieve("https://momentumdash.com/backgrounds/"+"%02d" % (choice,)+".jpg", str(choice)+".jpg") 
print a #Tells the image 

#Getting the location of the saved image 
cwd = os.getcwd() 
random=random.choice(os.listdir(cwd)) 
file =cwd+ '\\' +random 

#Making the image to desktop image 
import ctypes 
SPI_SETDESKWALLPAPER = 20 
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER , 0, file, 3) 

ものは画像を設定するには、このprogrmの確率は1/7っぽいです。
ほとんどの場合、黒い背景画面が表示されます。
どこが間違っていますか?

+0

'os.listdir()'は、指定されたディレクトリ内のすべてのファイルのリストを表示します。作業ディレクトリにダウンロードしているイメージ以外のファイルはありますか? (ヒント:このpythonスクリプト) – asongtoruin

+0

まず、 'random'に別の変数名を使用して、ライブラリ名を上書きします。第2に、 'os.listdir(cwd)'のために得たものを出力します。その中には適切なイメージファイルではないものがあります。 –

答えて

2

以下を試してください。これにより、ディレクトリ一覧がフィルタリングされ、jpg個のファイルのみが提供されます。ランダムなエントリは、これらから取得されます。また、パスと名前を安全に結合するためにos.path.join()が使用されます。

import urllib 
import os 
import random 
import ctypes 

#Chooses an image between 1 to 14 
choice = random.randint(1, 14) 

#Downloads images 
download_name = "{:02}.jpg".format(choice) 
a = urllib.urlretrieve("https://momentumdash.com/backgrounds/{}".format(download_name), download_name) 

#Getting the location of the saved image 
cwd = os.getcwd() 

#Filter the list to only give JPG image files 
image_files = [f for f in os.listdir(cwd) if os.path.splitext(f)[1].lower() == ".jpg"] 
random_image = random.choice(image_files) 
full_path = os.path.join(cwd, random_image) 

#Making the image to desktop image 
SPI_SETDESKWALLPAPER = 20 
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER , 0, full_path, 3)   

ファイルのリストは、Pythonのlist comprehension機能を使用してフィルタリングされます。これは、既存のアイテムから新しいリストを作成する方法です。この場合、オプションのifステートメントを使用して、拡張子が.jpgの新しいファイルのみをインクルードします。

+0

は拡張子を '.jpg'に強制することを考えていませんでした。+1 – asongtoruin

+0

ワンダフルな答え!簡単にこの構文を詳しく教えてください。 'os.listdir(cwd)のos.path.splitext(f)[1] .lower()==" .jpg "]' –

+1

の画像のfのfのためのf Python言語の私は答えにリンクを追加しました。 –

1

は、以下のことを試してみてください。

import urllib 
import os 
import random 
import ctypes 

# Set up an output folder 
out_folder = os.path.join(os.getcwd(), 'Backgrounds') 

# Make it if it doesn't exist 
if not os.path.isdir(out_folder): 
    os.mkdir(out_folder) 

# Loop through all values between 1 and 15 
for choice in range(1,15): 
    #Downloads images 
    a = urllib.urlretrieve("https://momentumdash.com/backgrounds/" + "%02d" % (choice,)+".jpg", 
          os.path.join(out_folder, "{}.jpg".format(choice)) 
          ) 

selected_wallpaper = random.choice(os.listdir(out_folder)) 

#Making the image to desktop image 
SPI_SETDESKWALLPAPER = 20 
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, os.path.join(out_folder, selected_wallpaper), 3) 

これは、あなたの現在の作業ディレクトリにBackgroundsというフォルダを作成し、そこにすべての画像を保存した後、ランダムに1つを選びます。

+0

新しいフォルダを作成することは考えられませんでした。 –

関連する問題