0

I'm trying to convert my image from rgba to rgb using PIL then detect the most used colors using Color-Thief-Py. However when I try to pass the rgb converted image into to the colorthief function, I get the following error: AttributeError: read.

Here's the code:

logo = request.FILES.get('logo')
rgba_img = Image.open(logo)
rgb_img = rgba_img.convert('RGB')
color = io.BytesIO(rgb_img.read()) #AttributeError: read
color_thief = ColorThief(color)

What am I doing wrong & How can I resolve it?

martineau
  • 119,623
  • 25
  • 170
  • 301
Horai Nuri
  • 5,358
  • 16
  • 75
  • 127
  • 1
    That's badly phrased and i'm not sure what you really want. But ```rgb_img``` is of type *Image*. There is no read()-method. You can use ```rgb_img.save()``` to write an image (for example into BytesIO). But it's not guaranteed that your next pipeline-step is expecting this (probably won't take a bytestream which is actually jpeg2000). Maybe it requires uncompressed pixel-data and you will need to prepare it using ```rgb_img.getdata()```. But we don't know... – sascha Jul 14 '17 at 13:12
  • Just pass the filepath to [ColorThief](https://github.com/fengsp/color-thief-py/blob/18a099ba3d9f649406ab1cfa75990df1cf658f03/colorthief.py#L35) ?? – wwii Jul 14 '17 at 13:43
  • @wwii I'd like to convert my image to rgb first, if the background of my image is transparent it won't take it into account... – Horai Nuri Jul 14 '17 at 13:44
  • 1
    Did you read the PIL docs?? Are you getting an error with ```rgb_image.read()```? – wwii Jul 14 '17 at 13:47
  • maybe this - https://stackoverflow.com/a/33117447/2823755 – wwii Jul 14 '17 at 13:53
  • 1
    You're **not** getting the error when you try to pass the rgb converted img into the colorthief function. `PIL.Image.Image` instances don't have a `read()` method, so that's why you're getting the `AttributeError`. See the [documentation](http://pillow.readthedocs.io/en/4.0.x/reference/Image.html#the-image-class) of the `Image` class. – martineau Jul 14 '17 at 13:56
  • Instead of using `rgb_img.read()`, try using `rgb_img.tobitmap()`, `rgb_img.tobytes()`, or `rgb_img.tostring()` depending on what `ColorThief()` accepts/expects as an argument. As always, consult the [documentation](http://pillow.readthedocs.io/en/4.0.x/reference/Image.html#PIL.Image.Image.tobitmap) to see what's available. – martineau Jul 14 '17 at 14:12

0 Answers0