我目前正在用Python编写一个简单的棋盘游戏,我刚刚意识到垃圾收集不会在重新加载图像时从内存中清除丢弃的位图数据.它只在游戏启动或加载或分辨率发生变化时发生,但它会使所消耗的内存倍增,所以我不能让这个问题得不到解决.
当重新加载图像时,所有参考都被转移到新的图像数据,因为它被绑定到与原始图像数据绑定到的相同的变量.我试图通过使用强制垃圾收集,collect()但它没有帮助.
我写了一个小样本来证明我的问题.
from tkinter import Button, DISABLED, Frame, Label, NORMAL, Tk
from PIL.Image import open
from PIL.ImageTk import PhotoImage
class App(Tk):
def __init__(self):
Tk.__init__(self)
self.text = Label(self, text = "Please check the memory usage. Then push button #1.")
self.text.pack()
self.btn = Button(text = "#1", command = lambda : self.buttonPushed(1))
self.btn.pack()
def buttonPushed(self, n):
"Cycle to open the Tab module n times."
self.btn.configure(state = DISABLED) # disable to prevent paralell cycles
if n == 100:
self.text.configure(text = "Overwriting …Run Code Online (Sandbox Code Playgroud) This is my "Image Pasting" Program, which is designed to take one image (here named product) and paste it over another image (here named background). Before, I simply had the program get images from my computer. But I decided to add another feature where you can get them from a url. The computer part still works great.
from PIL import Image, ImageFilter
import urllib.request,io
print("ALL IMAGES MUST BE PNG FORMAT")
ext=input("Get Image From Computer or Internet?(c or i)")
if ext …Run Code Online (Sandbox Code Playgroud) 当我尝试安装Pillow 2.5.3时,我收到一个错误:命令'x86_64-linux-gnu-gcc'失败,退出状态为1
我需要这个库作为另一个python项目的一部分.我经历了很多解决方案,但没有人帮助我
我在ubuntu 14.4上安装了python 3.4和2.7.我想在3.4上安装PIL(或枕头,如果你愿意...)但是当我尝试时:
$ sudo pip install pillow
Run Code Online (Sandbox Code Playgroud)
它安装在python 2.7上如何在python3.4上安装它?
我有一些简单的代码来加载图像文件然后显示它的大小,使用Pillow fork及其文档来了解如何查找图像属性.
这是一个从较大的文件中获取的代码片段,但我不认为任何其他部分应该对PIL有任何影响.我需要获取图像头文件,特别是大小,因此我可以将其转换为特定格式,用于需要特定输入数组的机器学习项目.以下是我理解要告诉我要做的文档.
from PIL import Image
im = Image.open("test.jpg")
print im
print im.size()
Run Code Online (Sandbox Code Playgroud)
这是我运行时遇到的错误
File "DataStorage.py", line 31, in <module>
print im.size()
TypeError: 'tuple' object is not callable
Run Code Online (Sandbox Code Playgroud) 我正在尝试将上传的图像显示到我想象中的蔬菜目录的模板中.
我有一个页面添加一个新的蔬菜和上传图像.主模板是一个蔬菜列表,稍后会有缩略图.
现在,当查看蔬菜的详细页面时,我想显示其图像,但模板只显示字符串'VegetableImage Object'代替图像,即使使用img标签.
我很困惑,因为模板显然已经找到了图像,但它们只是作为通用字符串显示.
models.py
class Vegetable(models.Model):
title = models.CharField(max_length=0)
category = models.CharField(max_length=50,
choices=CATEGORY_CHOICES)
thumbnail = models.ImageField(upload_to = 'uploaded_images/')
class VegetableImage(models.Model):
vegetable = models.ForeignKey(Vegetable, default=None, related_name='images')
image = models.ImageField(upload_to='images/vegetable',
verbose_name='image',)
Run Code Online (Sandbox Code Playgroud)
查看主模板(所有蔬菜的列表)和详细视图
class VegetableView(generic.ListView):
template_name = 'vegetable/vegetable.html'
context_object_name = 'vegetable_list'
def get_queryset(self):
return Vegetable.objects.all()
class DetailView(generic.DetailView):
model = Vegetable
template_name = 'vegetable/detail.html'
Run Code Online (Sandbox Code Playgroud)
显示菜细节的细节模板
{% if view %}
<h1>Title: {{ vegetable.title }}</h1>
<h2>Category: {{ vegetable.category }}</h2>
<p>Thumbnail: {{ vegetable.thumbnail }}</p>
<p>Images:
{% for vegetable in vegetable.images.all %}
{{ vegetable …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过序列图像制作一个3x3网格,但似乎无法做到正确.图像在文件夹中,命名为0 - 8(共9个图像),最终一个图像网格3x3的输出应如下
image0 image1 image2
image3 image4 image5
image6 image7 image8
Run Code Online (Sandbox Code Playgroud)
我试图关注如何使用PIL/Pillow将图像合并到画布中?但无法使其正常工作.
无需更改图像中的任何内容,只需将它们合并并制作3x3网格即可
当我在Python3上使用Pillow(版本3.3.0,通过pip安装)将图像数据加载到numpy数组时,我的单元测试报告了一个ResourceWarning.例如,当我运行以下脚本时会发出警告:
#! /usr/bin/env python3
import unittest
import numpy as np
import PIL.Image
def load_image():
with PIL.Image.open('test.tif') as im:
return np.array(im)
class TestData(unittest.TestCase):
def test_PIL(self):
im = load_image()
print(im.shape)
unittest.main()
Run Code Online (Sandbox Code Playgroud)
输出是
./err.py:14: ResourceWarning: unclosed file <_io.BufferedReader name='test.tif'>
im = load_image()
(420, 580)
.
----------------------------------------------------------------------
Ran 1 test in 0.012s
OK
Run Code Online (Sandbox Code Playgroud)
(如果我没有将图像包装成numpy数组,警告就会消失.)
此资源警告是否表明我的代码存在泄漏(例如,除了使用该with语句之外,我是否需要以某种方式"关闭"图像文件)?或者,如果警告是虚假的,我该如何禁用它?
不确定是什么导致这个......
我使用"Pillow-3.3.0.win32-py34.exe"安装文件运行Python 3.4.3.
导入图像时带
from PIL import Image
Run Code Online (Sandbox Code Playgroud)
我尝试使用"a.jpg"作为我的图像
img = Image.open("a.jpg")
Run Code Online (Sandbox Code Playgroud)
但是我收到这个错误,
>>> img = Image.open("a.jpg")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python34\lib\site-packages\PIL\Image.py", line 2289, in open
preinit()
File "C:\Python34\lib\site-packages\PIL\Image.py", line 365, in preinit
from PIL import JpegImagePlugin
File "C:\Python34\lib\site-packages\PIL\JpegImagePlugin.py", line 40, in <modu
le>
from PIL import Image, ImageFile, TiffImagePlugin, _binary
File "C:\Python34\lib\site-packages\PIL\TiffImagePlugin.py", line 50, in <modu
le>
from fractions import Fraction
File "C:\Python34\lib\fractions.py", line 6, in <module>
from decimal import Decimal …Run Code Online (Sandbox Code Playgroud) 从PIL文档中:
PIL.ImageDraw.Draw.line(xy,fill = None,width = 0)
在xy列表中的坐标之间绘制一条线。
参数:
- xy –由[[x,y),(x,y),...]等2元组或[x,y,x,y,...]等数值组成的序列。
- fill –用于行的颜色。
- width –线宽,以像素为单位。请注意,线连接处理不当,因此宽的折线看起来不太好。
我正在寻找解决此问题的方法。对我来说,一个好的解决方案是使线由PIL.ImageDraw圆角的末端(capstylein TKinter)绘制。有对应的PIL.ImageDraw吗?
最小的工作示例:
from PIL import Image, ImageDraw
WHITE = (255, 255, 255)
BLUE = "#0000ff"
MyImage = Image.new('RGB', (600, 400), WHITE)
MyDraw = ImageDraw.Draw(MyImage)
MyDraw.line([100,100,150,200], width=40, fill=BLUE)
MyDraw.line([150,200,300,100], width=40, fill=BLUE)
MyDraw.line([300,100,500,300], width=40, fill=BLUE)
MyImage.show()
Run Code Online (Sandbox Code Playgroud)
MWE的结果: