Checking for corrupted files in directory with hundreds of thousands of images gradually slows down

If you have that many images, I would suggest you use multiprocessing. I created 100,000 files of which 5% were corrupt and checked them like this: #!/usr/bin/env python3 import glob from multiprocessing import Pool from PIL import Image def CheckOne(f): try: im = Image.open(f) im.verify() im.close() # DEBUG: print(f”OK: {f}”) return except (IOError, OSError, Image.DecompressionBombError): … Read more

Detecting thresholds in HSV color space (from RGB) using Python / PIL

Ok, this does work (fixed some overflow errors): import numpy, Image i = Image.open(fp).convert(‘RGB’) a = numpy.asarray(i, int) R, G, B = a.T m = numpy.min(a,2).T M = numpy.max(a,2).T C = M-m #chroma Cmsk = C!=0 # Hue H = numpy.zeros(R.shape, int) mask = (M==R)&Cmsk H[mask] = numpy.mod(60*(G-B)/C, 360)[mask] mask = (M==G)&Cmsk H[mask] = (60*(B-R)/C … Read more

img = Image.open(fp) AttributeError: class Image has no attribute ‘open’

You have a namespace conflict. One of your import statements is masking PIL.Image (which is a module, not a class) with some class named Image. Instead of … from PIL import Image try … import PIL.Image then later in your code… fp = open(“/pdf-ex/downloadwin7.png”,”rb”) img = PIL.Image.open(fp) img.show() When working with a LOT of imports, … Read more

Python/PIL Resize all images in a folder

#!/usr/bin/python from PIL import Image import os, sys path = “/root/Desktop/python/images/” dirs = os.listdir( path ) def resize(): for item in dirs: if os.path.isfile(path+item): im = Image.open(path+item) f, e = os.path.splitext(path+item) imResize = im.resize((200,200), Image.ANTIALIAS) imResize.save(f + ‘ resized.jpg’, ‘JPEG’, quality=90) resize() Your mistake is belong to full path of the files. Instead of item … Read more