5

I am using a subreddit scraper to download images from wallpaper subreddits. The problem I am having is that some images have a small resolution causing them to look awful when used as wallpapers. I have found that the minimum resolution needed for a good looking wallpaper is 1920x1080. I now need to make a constantly running script that scans the image folder, looks at each images resolution and decides whether to delete it or move on to the next image. I've been tinkering about in Python for an hour or so but feel I am going nowhere fast as I am only a beginner and have not used Python for a few months. Any help on this project would be awesome ;)! Cheers.

UPDATE: I am now stuck on how to get the program to run through a folder and look at each picture. Currently my code is;

import os
from PIL import Image
while True:
    for file in os.listdir(r"C:\\Users\\Barney\\Documents\\sam"):
        im = Image.open(file)
        x, y = im.size
        totalsize = x*y
        if totalsize < 2073600:
            os.remove(file)

But this returns the error;

Traceback (most recent call last):
  File "C:\Users\Barney\Desktop\imagefilter.py", line 7, in <module>
    im = Image.open(file)
  File "C:\Python34\lib\site-packages\PIL\Image.py", line 2251, in open
    fp = builtins.open(fp, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'Car - 1971 Ford Mustang Mach 1 351 [2560X1600].jpg'

Looking on the internet I see that there may be a problem from where I am opening the program?? Very confused as the program is looking into this folder and reading the contents as the file it says doesnt exist is in that folder? Any help?

user2885647
  • 855
  • 2
  • 9
  • 10
  • Some solutions here for finding the image size. http://stackoverflow.com/questions/8032642/how-to-obtain-image-size-using-standard-python-class-without-using-external-lib It might be easier to check file size rather than image resolution, however. For that, see http://stackoverflow.com/questions/2104080/how-to-check-file-size-in-python – Stuart Nov 30 '14 at 10:14
  • FWIW, small images in a wallpaper collection may be intended to be tiled rather than stretched to fit. – PM 2Ring Nov 30 '14 at 11:04

2 Answers2

7

You can use Pillow or PIL:

from PIL import Image

with Image.open(image_file_path) as im:
    x, y = im.size
if x < 1920 or y < 1080:
    ....

UPDATE

os.listdir returns a list of filenames, not a list of filepaths. Unless you run the program in the image directory, it cannot open the file.

Run your program in the image directory, or convert the filenames to filepaths so they can be accessed.

import os
from PIL import Image

img_dir = r"C:\Users\Barney\Documents\sam"
for filename in os.listdir(img_dir):
    filepath = os.path.join(img_dir, filename)
    with Image.open(filepath) as im:
        x, y = im.size
    totalsize = x*y
    if totalsize < 2073600:
        os.remove(filepath)
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Sorry to bother you again, but now im getting a new error: PermissionError: [WinError 32] The process cannot access the file because it is being used by another process. Do u know a fix? – user2885647 Nov 30 '14 at 16:03
  • @user2885647, You need to close the image. I updated the answer accordingly. Could you accept my answer again if this helped you? – falsetru Nov 30 '14 at 23:07
1

If you have many subfolders under a main folder you can use the following code:

[for directory_path in glob.glob(r'F:\Data\Main_folder\*'):
    print(directory_path)
    for filename in os.listdir(directory_path):
        filepath = os.path.join(directory_path, filename)
        print(filepath)
        with Image.open(filepath) as im:
            x, y = im.size
        totalsize = x*y
        if totalsize < 50176:
            os.remove(filepath)][1]