标签: pillow

在Python枕头/ PIL中彼此顶部绘制透明度不同的多边形

我有以下代码:

im = Image.new("RGBA", (800,600))
draw = ImageDraw.Draw(im,"RGBA")
draw.polygon([(10,10),(200,10),(200,200),(10,200)],(20,30,50,125))
draw.polygon([(60,60),(250,60),(250,250),(60,250)],(255,30,50,0))
del draw 
im.show()
Run Code Online (Sandbox Code Playgroud)

但多边形之间的alpha /透明度没有任何差异。是否可以使用这些多边形执行此操作,或者Alpha级别仅适用于合成图像(我知道此解决方案,但仅查看基于PIL的注释,并认为我已经在Pillow中看到了此问题)。

如果没有这样的东西,是否有一种很好,简单,有效的方式将类似这样的东西放入库中?

python python-imaging-library pillow

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

Django - 保存上传的图像

我有一个表格,我应该在其中上传图片,但我无法保存图片。除了图像之外,表单中的其他所有内容都可以正常工作。

我确信我的addGame方法存在一些问题,但我已经尝试了几十种不同的方法,但都没有成功。

我已经阅读了文档,但似乎我仍然做错了什么,因为图像永远不会被保存。

(作为一个旁注:我正在使用 Pillow 来裁剪图像,我也不确定我是否做得正确,但我最近才添加了它,并且由于图像没有保存我有无法知道这是否正确实施。当我尝试让上传工作时,我将裁剪部分注释掉。)

forms.py

class GameForm(forms.ModelForm):

    image = forms.ImageField()
    code = forms.Textarea()
    deleteGame = forms.BooleanField(required=False, widget=forms.HiddenInput())

    class Meta:
        model = Game
        fields = ('title', 'image', 'description', 'requirements', 'code', 'deleteGame')
Run Code Online (Sandbox Code Playgroud)

views.py

@login_required
def add_game(request):
    user = request.user

    if request.method == 'POST':
        form = GameForm(request.POST, request.FILES)
        if form.is_valid():
            form = form.save(commit=False)
            image = request.FILES['image']
            box = (200, 200, 200, 200)
            cropped = image.crop(box)
            form.image = cropped
            form.user = request.user
            form.save()
            return HttpResponseRedirect('/userprofile')
    else:
        form …
Run Code Online (Sandbox Code Playgroud)

python django pillow

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

无法使用 PIL 和 python 2.7 将文本写入灰度 PNG

我正在尝试使用 PIL 并按照此线程在灰度 png 上写一些文本。这看起来很简单,但我不确定我做错了什么。

使用 PIL 在图像上添加文本

然而,当我尝试这样做时,draw.text函数就死了:

from PIL import Image, ImageDraw, ImageFont
img = Image.open("test.png")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("open-sans/OpenSans-Regular.ttf", 8)
# crashes on the line below:
draw.text((0, 0), "Sample Text", (255, 255, 255), font=font)
img.save('test_out.png')
Run Code Online (Sandbox Code Playgroud)

这是错误日志:

"C:\Python27\lib\site-packages\PIL\ImageDraw.py", line 109, in _getink
ink = self.draw.draw_ink(ink, self.mode)
TypeError: function takes exactly 1 argument (3 given)
Run Code Online (Sandbox Code Playgroud)

谁能指出我的问题?

python-imaging-library python-2.7 pillow

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

显示导入错误:在使用 PIL 导入图像时无法导入名称“模板”

我目前正在开发图像识别程序使用:python 3.5 PyCharm Community Edition 2016.3 作为 IDE,我使用pip install Pillow安装了 Pillow 3.1.2

我的代码是:

from PIL import Image
im = Image.open('images/dot.png')
im.load()
Run Code Online (Sandbox Code Playgroud)

RUN 时显示错误

Traceback (most recent call last):
  File "/home/harry/PycharmProjects/python study/image.py", line 1, in <module>
    from PIL import Image
  File "/usr/lib/python3/dist-packages/PIL/Image.py", line 31, in <module>
    import logging
  File "/usr/lib/python3.5/logging/__init__.py", line 28, in <module>
    from string import Template
ImportError: cannot import name 'Template'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils …
Run Code Online (Sandbox Code Playgroud)

python-imaging-library pillow python-3.5

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

Tkinter 图像是空白的

我有以下python代码:

from tkinter import *
from PIL import ImageTk, Image
import sys, os
height = 5
width = 8

# window = Tk()


class NSUI(Frame):
    def reload(self):
        os.execl(sys.executable, sys.executable, *sys.argv)
    def __init__(self, master=None):
        """
        Initialise process application
        """
        Frame.__init__(self, master)
        self.grid()
        self.master.title('PROGProject')
        # Configure columns
        for r in range(7):
            self.master.rowconfigure(r, weight=1)
        # Create columns
        for c in range(7):
            self.master.columnconfigure(c, weight=1)
            Button(master, text = "Actuele reistijden", bg = '#005ca0', fg = 'white', font = "Verdana 10", width = width, height = …
Run Code Online (Sandbox Code Playgroud)

python tkinter pillow

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

使用 Pillow Image.open 遍历文件夹

我正在尝试遍历 .png 文件的文件夹并对它们进行 OCR。迭代有效,但是一旦我尝试使用 PIL 打开图像,它就会出错。

import pytesseract
from PIL import Image
import os

for filename in os.listdir('C:/Users/Artur/Desktop/Sequenz_1'):
    if filename.endswith('.png'):
        print(filename)
Run Code Online (Sandbox Code Playgroud)

这工作得很好。它打印文件夹中的每个 .png 文件名。但是当我尝试 OCR 时:

import pytesseract
from PIL import Image
import os

for filename in os.listdir('C:/Users/Artur/Desktop/Sequenz_1'):
    if filename.endswith('.png'):
        print(pytesseract.image_to_string(Image.open(filename)))
Run Code Online (Sandbox Code Playgroud)

输出:

Traceback (most recent call last):
  File "C:\Users\Artur\Desktop\Pytesseract_test.py", line 8, in <module>
    print(pytesseract.image_to_string(Image.open(filename)))
  File "C:\Users\Artur\AppData\Local\Programs\Python\Python36\lib\site-packages\PIL\Image.py", line 2580, in open
    fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'frame_0000.png'
Run Code Online (Sandbox Code Playgroud)

编辑:

多亏了 Benehiko,它现在工作正常。

代码:

import pytesseract
from …
Run Code Online (Sandbox Code Playgroud)

python iteration pillow

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

如何用PIL画一个三角形?

我正在尝试用 PIL ImageDraw 绘制一个三角形,这是我拥有的代码

    t1 = int(tri[0])
    t2 = int(tri[1])
    t3 = int(tri[2])
    t4 = int(tri[3])
    t5 = int(tri[4])
    t6 = int(tri[5])
    t7 = int(tri[6])
    t8 = int(tri[7])
    t9 = int(tri[8])
    t10 = int(tri[9])
    draw.polygon((t1,t2),(t3,t4),(t5,t6), fill=(t7,t8,t9,t10))
Run Code Online (Sandbox Code Playgroud)

我收到错误

类型错误:polygon() 为参数“fill”获得了多个值

有什么方法可以制作三角形而不会出现此错误

蟒蛇 2.7

image python-2.7 pillow

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

after_cancel用作停止方法

我正在尝试使用after_cancel来停止简单图像查看器中的动画循环.我已经阅读了关于Tcl的文档,在这里搜索并google,并探索了python subreddits.我的错误是:

TclError: wrong # args: should be "after cancel id|command"
Run Code Online (Sandbox Code Playgroud)

这发生在以下代码的最后一行(请不要因为使用全局变量而杀了我,这个项目只是一个图像查看器来显示我们办公室的天气预报产品):

n_images = 2
images = [PhotoImage(file="filename"+str(i)+".gif") for i in range(n_images)]
current_image = -1

def change_image():
    displayFrame.delete('Animate')
    displayFrame.create_image(0,0, anchor=NW,
                        image=images[current_image], tag='Animate')
    displayFrame.update_idletasks() #Force redraw

callback = None

def animate():
    forward()
    callback = root.after(1000, animate)

def forward():
    global current_image
    current_image += 1
    if current_image >= n_images:
        current_image = 0
    change_image()

def back():
    global current_image
    current_image -= 1
    if current_image < 0:
        current_image = n_images-1
    change_image()

def stop():
    root.after_cancel(callback)
Run Code Online (Sandbox Code Playgroud)

如果有更合适的方法来停止Tkinter中的动画循环,请告诉我!

python tkinter python-2.7 pillow

0
推荐指数
2
解决办法
2727
查看次数

如何使用Python枕头加载图像?

我以这种方式加载图像:

from PIL import Image
im = Image.open('test.png')
Run Code Online (Sandbox Code Playgroud)

给我以下错误:

IOError: [Errno 2] No such file or directory: 'test.png'
Run Code Online (Sandbox Code Playgroud)

我已经将图像“ test.png”保存在桌面上。

那我应该在哪里保存图像?

python pillow

0
推荐指数
1
解决办法
3431
查看次数

在Mac上查找和卸载PIL

我想安装Pillow并在许多地方阅读,只有在删除PIL时才能使用.

在某处我安装了PIL,但我无法找到它或记住它是如何安装的.当我通过终端安装东西时,我几乎是一个ctrl + c和ctrl + v家伙,所以我想我在安装它时遇到了一些问题.

我试过了

pip uninstall PIL
easy_install uninstall PIL
brew uninstall PIL
Run Code Online (Sandbox Code Playgroud)

并且没有想法.我甚至找不到任何名为"PIL"的文件和聚光灯.

只是想找到摆脱PIL并安装Pillow的方法,这样我就可以为几百张图片添加文字了.

在El Capitan上运行python 2.7

macos python-imaging-library python-2.7 pillow

0
推荐指数
1
解决办法
1984
查看次数