相关疑难解决方法(0)

如何使用PIL获取PNG图像的alpha值?

如何使用PIL检测PNG图像是否具有透明的alpha通道?

img = Image.open('example.png', 'r')
has_alpha = img.mode == 'RGBA'
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,我们知道PNG图像是否具有alpha通道,但是如何获得alpha值?

我没有在PIL网站上描述的img.info字典中找到"透明度"键

我正在使用Ubuntu和zlib1g,已经安装了zlibc软件包.

python png image transparent python-imaging-library

21
推荐指数
2
解决办法
3万
查看次数

PIL将带透明度的PNG或GIF转换为JPG而不用

我正在使用PIL1.1.7在Python 2.7中对图像处理器进行原型设计,我希望所有图像都以JPG结尾.输入文件类型将包括透明和没有的tiff,gif,png.我一直在尝试组合两个脚本,我发现1.将其他文件类型转换为JPG和2.通过创建空白图像并将原始图像粘贴到白色背景上来消除透明度.我的搜索是垃圾邮件,人们试图产生或保持透明度,而不是相反.

我正在使用这个:

#!/usr/bin/python
import os, glob
import Image

images = glob.glob("*.png")+glob.glob("*.gif")

for infile in images:
    f, e = os.path.splitext(infile)
    outfile = f + ".jpg"
    if infile != outfile:
        #try:
        im = Image.open(infile)
        # Create a new image with a solid color
        background = Image.new('RGBA', im.size, (255, 255, 255))
        # Paste the image on top of the background
        background.paste(im, im)
        #I suspect that the problem is the line below
        im = background.convert('RGB').convert('P', palette=Image.ADAPTIVE)
        im.save(outfile)
        #except IOError:
           # print "cannot convert", …
Run Code Online (Sandbox Code Playgroud)

python image-processing python-imaging-library

17
推荐指数
3
解决办法
3万
查看次数

使用Python Imaging Library(PIL),如何使用alpha通道组合图像而不是另一个图像?

我有两个图像,都有alpha通道.我想将一个图像放在另一个图像上,从而生成带有alpha通道的新图像,就像它们在图层中呈现一样.我想用Python Imaging Library做到这一点,但是其他系统中的建议会很棒,即使原始数学也是一个好处; 我可以使用NumPy.

python image-processing python-imaging-library

14
推荐指数
3
解决办法
2万
查看次数

Django图像在上传之前调整大小和转换

我在这个问题上搜索了很多,但却找不到我需要的东西.我会解释我的问题:

在我的网站上,用户可以上传图像.我需要调整此图像的大小并上传之前将其转换为JPEG .

我在Views.py中看到了一些使用方法的解决方案,但我想通过覆盖Models.py中的save()方法来实现.问题是,我发现在save方法中调整图像大小的所有解决方案都是在第一次保存(调用超级函数)之后执行的,这意味着它会使用带宽(如果我使用CDN)什么都没有(我是对的吗?点?).

谢谢您的帮助

python django django-models

9
推荐指数
5
解决办法
2万
查看次数

使用PIL从任何图像中删除透明度/ alpha

如何用指定的背景颜色替换任何图像(png,jpg,rgb,rbga)的alpha通道?它还必须适用于没有Alpha通道的图像.

python alphablending python-imaging-library pillow

9
推荐指数
2
解决办法
8520
查看次数

为什么在使用 PIL 时出现“无法保存模式 RGBA”错误?

我正在尝试将图像转换为 PDF。我有这个代码:

def convertToPDF(folder, PDFname, deleteLast=False):
    '''
    converts all images in a folder to a PDF. 
    '''
    imageList = []
    for filename in glob.glob(folder + f'/*'):
        image=Image.open(filename)
        image.convert('RGB') # convert to RGB
        imageList.append(image)

    imageList[0].save(f'./' + PDFname + '.pdf',save_all=True, append_images=imageList[1:]) # take the first image and add everything else
Run Code Online (Sandbox Code Playgroud)

有时我会收到此错误:

 File "c:\Users\felix\OneDrive\Desktop\Programmieren\TelegrammBotProjekt\manganeloAPI.py", line 195, in convertToPDF
   imageList[0].save(f'./' + PDFname + '.pdf',save_all=True, append_images=imageList[1:]) # take the first image and add everything else
 File "C:\Users\felix\Anaconda3\lib\site-packages\PIL\Image.py", line 2151, in save
   save_handler(self, fp, filename)
 File …
Run Code Online (Sandbox Code Playgroud)

python python-imaging-library

8
推荐指数
1
解决办法
7663
查看次数

使用 PIL 保存模式错误将 png 转换为 pdf

我正在尝试将 png 文件转换为 pdf 文件。PIL似乎是这样做的方法,但我cannot save mode RGBA在运行时遇到错误 ( )

代码:

import os
import PIL
from PIL import Image

path = 'E:\path_to_file'
filename = '15868799_1.png'
fullpath_filename = os.path.join(path, filename)
im = PIL.Image.open(fullpath_filename)
newfilename = '15868799.pdf'
newfilename_fullpath = os.path.join(path, newfilename)
PIL.Image.Image.save(im, newfilename_fullpath, "PDF", resoultion=100.0)
Run Code Online (Sandbox Code Playgroud)

错误:

 File "C:\Python\lib\site-packages\PIL\PdfImagePlugin.py", line 148, in _save
 raise ValueError("cannot save mode %s" % im.mode)
 ValueError: cannot save mode RGBA
Run Code Online (Sandbox Code Playgroud)

python-imaging-library python-3.x

6
推荐指数
1
解决办法
5294
查看次数