简体   繁体   English

如何在python中的文件夹中使用PIL调整图像大小

[英]How to resize Images using PIL in a folder in python

I am using PIL/Python to resize the images in a folder . 我正在使用PIL / Python来调整文件夹中的图像大小。 After resizing the images I want to save them again in the same folder with the same name but I am having error cannot write mode RGBA as JPEG 调整图像大小后,我想再次将它们保存在同一个文件夹中,但是我有错误,无法将RGBA模式写为JPEG

there are multiple formats of images inside the folder 文件夹中有多种格式的图像

this is the code which is used to resize the images 这是用于调整图像大小的代码

path = "data/images/"
dirs = os.listdir( path )

def resize():
    for item in dirs:
        if os.path.isfile(path+item):
            im = Image.open(path+item)
            f, e = os.path.splitext(path+item)
            imResize = im.resize((200,200), Image.ANTIALIAS)
            imResize.save(f, 'JPEG', quality=90)

resize()

this is the error I am having 这是我遇到的错误

cannot write mode RGBA as JPEG

Try:- 尝试:-

f = "apple.png"
im = Image.open(path+item)
im = im.convert("RGB") 
imResize = im.resize((200,200), Image.ANTIALIAS)
imResize.save(f, quality=90)

We removed the alpha channel from the image, and converted the color space to RGB only. 我们从图像中删除了Alpha通道,并将颜色空间仅转换为RGB As most file formats support a image with RGB color space, I don't think errors will still continue to exist. 由于大多数文件格式支持具有RGB色彩空间的图像,我不认为错误仍将继续存在。

Don't explicitly define the output format of all input images as .jpeg , rather let PIL determine it by looking at the file's extension. 不要将所有输入图像的输出格式明确定义为.jpeg ,而是让PIL通过查看文件的扩展名来确定它。

PS:- Don't use this method if transparency of the image is taken into account(alpha channel), as this method will get rid of the alpha channel of the image. PS: -如果考虑图像的透明度(alpha通道),请不要使用此方法,因为此方法将摆脱图像的Alpha通道。

To resize images I always have used .thumbnail . 为了调整图像大小,我总是使用.thumbnail It's easy to use and it simply works. 它易于使用,而且很简单。 To keep the data of the colors and the alpha, the solution brought by Vasu Deo.S works well and it's the one I am using on my script. 为了保留颜色和alpha的数据, Vasu Deo.S带来的解决方案运行良好,这是我在脚本上使用的解决方案。

You need to save your images in .png instead of .jpeg to avoid the error of the alpha channel. 您需要将图像保存为.png而不是.jpeg以避免Alpha通道的错误。

So here's your code edited that actually works. 所以这里编辑的代码实际上是有效的。

path = "data/images/"
dirs = os.listdir( path )

def resizeImages():
    for item in dirs:
        if os.path.isfile(path+item):
            im = Image.open(path+item)
            im = im.convert('RGBA')

            size = 200, 200

            imResize.thumbnail(size)

            f, e = os.path.splitext(path+item)
            imResize.save(f+'.png')

Note that this, will need to be saved as .png . 请注意,这需要保存为.png If you want to save as a .jpeg image, you must get rid of the alpha channel using this: 如果要保存为.jpeg图像,则必须使用以下方法删除alpha通道:

path = "data/images/"
dirs = os.listdir( path )

def resizeImages():
    for item in dirs:
        if os.path.isfile(path+item):
            im = Image.open(path+item)
            im = im.convert('RGBA')

            data = np.array(im)
            red, green, blue, alpha = data.T

            alpha_areas = (red == 0) & (blue == 0) & (green == 0) & (alpha == 0) # set alpha to 0
            data[..., :-1][alpha_areas.T] = (255,255,255)
            im = Image.fromarray(data)

            size = 200, 200

            imResize = im.thumbnail(size)

            f, e = os.path.splitext(path+item)
            imResize.save(f+'.png')

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

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