I have this block of code which successfully crops the images I want to save. But I also open the images with a .Convert("LA") which should ensure the image is Greyscaled. I am using the PIL library and not matplotlib.
Yet when I look at the images saved, they are all the correct size but some of them are still showing in colour. How do I fix this?
def resizeImage(self, filePath):
# [left, up, right, bottom]
img = Image.open(filePath).convert('LA') # 'LA' set image to greyscale
width, height = img.size
print("-" * 100)
message = f"Image Size = {width} x {height} image = {filePath}"
print(message)
croppedIm = img
if width == 640 and height == 480:
return img # Image the correct size, so let through
if width == 640 and height == 490:
croppedIm = img.crop((0, 0, 640, 480))
elif width == 720 and height == 480:
croppedIm = img.crop((40, 0, 680, 480))
else:
try:
raise Exception("Unexpected " + message)
except Exception as inst:
print(inst) # __str__ allows args to be printed directly
raise
width, height = croppedIm.size
print(f"Cropped Image Size = {width} x {height} image = {filePath}")
print("-" * 100)
grayIm = croppedIm.convert("L")
return grayIm.save(filePath)