简体   繁体   English

使用 PIL 保存图像

[英]Saving Image with PIL

I am trying to save an image that I created from scratch with PIL我正在尝试保存我用 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回溯(最近一次调用):文件“”,第 1 行,在文件“C:\\Python27\\lib\\site-packages\\spyderlib\\widgets\\externalshell\\sitecustomize.py”中,第 523 行,在运行文件 execfile(filename, namespace ) 文件 "C:\\Python27\\Lib\\site-packages\\xy\\pyimgmake.py",第 125 行,在 newImg1.PIL.save("img1.png") 文件 "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).我需要帮助解释此错误以及如何将图像正确保存为“img1.png”(我可以将图像保存到默认保存点)。


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 ... newImg1.save("img1.png") 文件“C:\\Python27\\lib\\site-packages\\PIL\\Image.py”,第 1439 行,在 save_handler(self, fp, filename) 文件“C: \\Python27\\lib\\site-packages\\PIL\\PngImagePlugin.py”,第 572 行,在 _save ImageFile._save(im, _idat(fp, chunk), [("zip", (0,0)+im.size, 0, rawmode)]) 文件 "C:\\Python27\\lib\\site-packages\\PIL\\ImageFile.py", line 481, in _save e = Image._getencoder(im.mode, e, a, im.encoderconfig) 文件"C:\\Python27\\lib\\site-packages\\PIL\\Image.py", line 399, in _getencoder return apply(encoder, (mode,) + args + extra) TypeError: an integer is required

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. PIL 不是 newImg1 的属性,但 newImg1 是 PIL.Image 的一个实例,所以它有一个保存方法,因此以下应该工作。

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.请注意,仅调用文件 .png 并不能使其成为文件格式,因此您需要将文件格式指定为第二个参数。

try:尝试:

type(newImg1)
dir(newImg1)

and

help(newImg1.save)

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:要在图像上使用数组样式寻址而不是 putpixel,请转换为 numpy 数组:

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

Try this:尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM