1

I'm getting an error for a program that used to work without any problem. The folder xxx_xxx_xxx contains a lot of image files in jpeg format.

I'm trying to run through each image and retrieve hue values for each pixel on each image.

I've tried the solutions proposed here: Python - AttributeError: 'tuple' object has no attribute 'read' and here: AttributeError: 'tuple' object has no attribute 'read' with no success.

Code:

from PIL import Image
import colorsys
import os

numberofPix = 0
list = []
hueValues = 361
hueRange = range(hueValues)

for file in os.walk("c:/users/xxxx/xxx_xxx_xxx"):
    im = Image.open(file)
    width, height = im.size
    rgb_im = im.convert('RGB')

    widthRange = range(width)
    heightRange = range(height)

    for i in widthRange:
        for j in heightRange:
            r, g, b = rgb_im.getpixel((i, j))
            if r == g == b:
                continue
            h, s, v = colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0)
            h = h * 360
            h = int(round(h))
            list.append(h)
            numberofPix = numberofPix + 1

for x in hueRange:
    print "Number of hues with value " + str(x) + ":" + str(list.count(x))

print str(numberofPix)

Here's the error I'm getting:

AttributeError: 'tuple' object has no attribute 'read'

1 Answer 1

1

I have no idea how this code worked previously (especially if the line - for file in os.walk("c:/users/nathan/New_Screenshots_US/Dayum"): was there before as well) , that line is the main reason for your issue.

os.walk returns a tuple of the format - (dirName, subDirs, fileNames) - where dirName is the name of the directory currently being walked, fileNames is the list of files in that particular directory.

In the next line you are doing - im = Image.open(file) - this would not work, because file is a tuple (of the above format) . You need to iterate over each file name and if the file is a .jpeg then you need to use os.path.join to create the path to the file and use it in Image.open() .

Example -

from PIL import Image
import colorsys
import os
import os.path

numberofPix = 0
list = []
hueValues = 361
hueRange = range(hueValues)

for (dirName, subDirs, fileNames) in os.walk("c:/users/nathan/New_Screenshots_US/Dayum"):
    for file in fileNames:
        if file.endswith('.jpeg'):
            im = Image.open(os.path.join(dirName, file))
            . #Rest of the code here . Please make sure you indent them correctly inside the if block.
            .
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.