3

I want to draw a rectangle on a picture and save it as a new file. what I'm doing is below:

from PIL import Image
from PIL import ImageChops
from PIL import ImageDraw

im = Image.open('the animal picture.jpg')
draw = ImageDraw.Draw(im)
draw.rectangle((69, 17, 418, 107))
im = im.convert('RGB')
im.save('new.jpg')

It gives an error message:

Traceback (most recent call last):
  File "C:\Python27\draw_re.py", line 9, in <module>
    im.save('new.jpg')
  File "C:\Python27\lib\PIL\Image.py", line 1439, in save
    save_handler(self, fp, filename)
  File "C:\Python27\lib\PIL\JpegImagePlugin.py", line 471, in _save
    ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)])
  File "C:\Python27\lib\PIL\ImageFile.py", line 494, in _save
    for e, b, o, a in tile:
ValueError: Not a valid number of quantization tables. Should be between 1 and 4.

It looks like the problem in PIL - Not a valid numbers of quantization tables. Should be between 2 and 4, but the tip doesn't solve the problem. It makes batch processing impossible.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mark K
  • 8,767
  • 14
  • 58
  • 118
  • 1
    Does this happen with all images? Can you link to a file that reproduces the problem? What version of PIL are you using? – Paul Nov 11 '14 at 04:31
  • thanks, Paul. you are hitting the point. please see my update. – Mark K Nov 11 '14 at 04:43

1 Answers1

4

I worked it out. The problem caused by the Image and PIL libraries I am using.

I uninstalled and removed all previous installed PIL and Image libraries (there were confusion before and difficulties in original installations) so I have cross files and folders for the libraries.

I did the uninstallations through pip, and "Control Panel\All Control Panel Items\Programs and Features" in Windows as well. Also have manually removed the residues folders and files.

Pillow is the one shall be used. I downloaded a MS Windows installer from https://pypi.python.org/pypi/Pillow/2.6.1 and installed it. Run the script and it's working fine.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mark K
  • 8,767
  • 14
  • 58
  • 118
  • 1
    Do not delete the question. I'm sure you won't be the last person with a corrupted PIL installation, and if this helps at least one person on the internet it will be worth it. – Mark Ransom Nov 11 '14 at 05:06
  • Very similarly - I hit this after having PIL and Pillow installed at the same time. I uninstalled both using pip, then just installed Pillow. See also http://stackoverflow.com/a/20236094/552793 for the import statement tweak that helped – jlb83 Dec 23 '14 at 21:19