46

I am trying to save an image that I created from scratch with PIL

newImg1 = PIL.Image.new('RGB', (512,512))
pixels1 = newImg1.load()

...

for i in range (0,511):
    for j in range (0,511):
       ...
            pixels1[i, 511-j]=(0,0,0)
        ...

newImg1.PIL.save("img1.png")

and I get the following error:

Traceback (most recent call last): File "", line 1, in File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 523, in runfile execfile(filename, namespace) File "C:\Python27\Lib\site-packages\xy\pyimgmake.py", line 125, in newImg1.PIL.save("img1.png") File "C:\Python27\lib\site-packages\PIL\Image.py", line 512, in getattr raise AttributeError(name) AttributeError: PIL

I need help interpreting this error and how to save the image properly as "img1.png" (I am fine with the image being saved to the default save spot).


UPDATE:

from PIL import Image as pimg
...
newImg1 = pimg.new('RGB', (512,512))
...
newImg1.save("img1.png")

and I get the following error:

... newImg1.save("img1.png") File "C:\Python27\lib\site-packages\PIL\Image.py", line 1439, in save save_handler(self, fp, filename) File "C:\Python27\lib\site-packages\PIL\PngImagePlugin.py", line 572, in _save ImageFile._save(im, _idat(fp, chunk), [("zip", (0,0)+im.size, 0, rawmode)]) File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 481, in _save e = Image._getencoder(im.mode, e, a, im.encoderconfig) File "C:\Python27\lib\site-packages\PIL\Image.py", line 399, in _getencoder return apply(encoder, (mode,) + args + extra) TypeError: an integer is required

3
  • 3
    remove PIL. from newImg1.PIL.save("img1.png"), and try. Oct 29, 2013 at 6:19
  • Downvoted for the creative usage of new API methods or trying something without having checked any documentation. Oct 29, 2013 at 6:26
  • that was my final attempt... I have updated the post with the error that the above suggestion produces
    – Kyle Grage
    Oct 29, 2013 at 7:01

3 Answers 3

68

PIL isn't an attribute of newImg1 but newImg1 is an instance of PIL.Image so it has a save method, thus the following should work.

newImg1.save("img1.png","PNG")

Note that just calling a file .png doesn't make it one so you need to specify the file format as a second parameter.

try:

type(newImg1)
dir(newImg1)

and

help(newImg1.save)
5
  • Extended answer. Note that the parameter list for save varies with the format. Oct 29, 2013 at 21:36
  • You still left in the .PIL in the save call that was causing the original error. Oct 29, 2013 at 21:40
  • Darn thats what I get for editing under the influence of a general anaesthetic. Oct 30, 2013 at 3:39
  • Thanks for the new answer. Time ran out though, so I actually just passed the PIL into a matplotlib function. For some reason, there was no trouble with saving it this way... I guess whatever works.
    – Kyle Grage
    Oct 30, 2013 at 7:23
  • 2
    The docs now state that the format is determined from the filename, so newImg1.save('img1.png') should work now. Jun 30, 2021 at 16:08
6

As I hate to see questions without a complete answer:

from PIL import Image
newImg1 = Image.new('RGB', (512,512))
for i in range (0,511):
    for j in range (0,511):
        newImg1.putpixel((i,j),(i+j%256,i,j))
newImg1.save("img1.png")

which yields a test pattern.

To use array style addressing on the image instead of putpixel, convert to a numpy array:

import numpy as np
pixels = np.asarray(newImg1)
pixels.shape, pixels.dtype
-> (512, 512, 3), dtype('uint8')
3

Try this:

newImg1 = pimg.as_PIL('RGB', (512,512))
...
newImg1.save('Img1.png')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.