标签: python-imaging-library

将jpg转换为灰度

我正在尝试将图像转换为灰度,作为我遵循的一组说明的一部分。但是,在将其设为灰度后,它不会让我保存。

错误:

    img2.save("img.jpg")
  File "/Library/Python/2.7/site-packages/PIL/Image.py", line 1698, in save
    save_handler(self, fp, filename)
  File "/Library/Python/2.7/site-packages/PIL/JpegImagePlugin.py", line 586, in _save
    raise IOError("cannot write mode %s as JPEG" % im.mode)
IOError: cannot write mode LA as JPEG
Run Code Online (Sandbox Code Playgroud)

代码:

img = Image.open(fname)
img2 = img2.convert('LA')
img2.save("img.jpg")
Run Code Online (Sandbox Code Playgroud)

python python-imaging-library

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

PIL 将透明 PNG 加载为 RGB?

我正在尝试使用 PIL 合成一些航拍图像并遇到一些麻烦。我使用 PIL用这段代码加载这个图像

composite = Image.new('RGBA', (256, 256))
url = 'http://...'
resp = requests.get(url)
content = StringIO(resp.content)
image = Image.open(content)
composite.paste(image, (0, 0), image)
Run Code Online (Sandbox Code Playgroud)

当我调用 时composite.paste(),PIL 给我错误“ValueError: bad transparent mask”。当我打印时image.mode,果然它只是RGB而不是预期的RGBApaste()需要)。

我下载的 PNG 中的 alpha 通道在哪里?

python python-imaging-library

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

PhotoImage 实例没有属性“resize”

尽管多个在线资源表明这是使用 PIL 调整图像大小的方法,但我还是收到了错误“PhotoImage 实例没有属性 'resize'”。有任何想法吗?

相关代码:

Deathwing = ImageTk.PhotoImage(Image.open('Deathwing.PNG'))

Deathwing2=Deathwing.resize((100,50),Image.ANTIALIAS)

picture1=Label(pictures_frame,image=Deathwing2)

picture1.grid(column=1,row=1)
Run Code Online (Sandbox Code Playgroud)

和 PIL 被导入为:

from PIL import Image,ImageTk
Run Code Online (Sandbox Code Playgroud)

tkinter python-imaging-library python-2.7

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

'numpy.ndarray' 对象没有属性 'mode'

这段代码出现错误:

import PIL.ImageOps
inverted_image = PIL.ImageOps.invert(image)
Run Code Online (Sandbox Code Playgroud)

回溯是

Traceback (most recent call last):
  File "filter.py", line 32, in <module>
    inverted_image = PIL.ImageOps.invert(denoise)
  File "/usr/lib/python3/dist-packages/PIL/ImageOps.py", line 367, in invert
    return _lut(image, lut)
  File "/usr/lib/python3/dist-packages/PIL/ImageOps.py", line 48, in _lut
    if image.mode == "P":
AttributeError: 'numpy.ndarray' object has no attribute 'mode'
Run Code Online (Sandbox Code Playgroud)

我该如何解决?

python numpy python-imaging-library

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

PIL.Image.fromarray 在 np.reshape 后产生扭曲的图像

我有一个形状为 (10000,1296) 的数组 (numpy),我想将其保存为 10,000 张 36x36 大小的图像。数组中的所有值都在范围内(0,255)。所以我用这段代码来做到这一点(效果很好):

for i, line in enumerate(myarray):
    img = Image.new('L',(36,36))
    img.putdata(line)
    img.save(str(i)+'.png')
Run Code Online (Sandbox Code Playgroud)

我想使用该Image.fromarray方法替换此代码,但是与原始方法相比,生成的图片失真得无法识别。首先我试过这个:

myarray = myarray.reshape(10000,36,36)
for i, line in enumerate(myarray):
    img = Image.fromarray(line, 'L')
    img.save(str(i)+'.png')
Run Code Online (Sandbox Code Playgroud)

这没有用。所以为了调试,我想我只会尝试一个项目并这样做:

Image.fromarray(myarray[0].reshape(36,36), 'L').save('test.png')
Run Code Online (Sandbox Code Playgroud)

再次 - 乱码扭曲的图像。

所以我想要么fromarray没有像我想象的那样工作,要么我reshape太天真并且弄乱了数据,但我无法解决这个问题。欢迎任何想法。

python numpy python-imaging-library

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

使用 PIL 或 Scipy 将 Python 图像从 RGB 转换为单通道

是否有一种已知的解决方案可以使用 PIL(或 Scipy)将图像从具有 3 个通道(RGB)转换为只有一个通道

我尝试Grayscale按照下面的代码将图像转换为 png 并保存为 png,图像仍然有 3 个颜色通道。

from glob import glob
import os
import os.path
from PIL import Image

SIZE = 32, 32

# set directory
# os.chdir('..data/unprocessed_cats')

# filter all jpg and png images
IMAGE_FILES = glob('../data/validation/cats/*.jpg')

IMAGE_COUNTER = 1
print IMAGE_FILES
# iterate over files
for image_file in IMAGE_FILES:

    # open file and resize
    try:
        im = Image.open(image_file)
    except:
        pass
    im = im.resize(SIZE, Image.ANTIALIAS)

    # save locally
    output_filename = "%s.png" …
Run Code Online (Sandbox Code Playgroud)

python python-imaging-library python-2.7

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

如何在 Python 3 中将 QImage(QPixmap) 转换为 PIL Image?

我想将图像从 QImage 或 Qpixmap 类转换为 PIL Image。我发现了这个:Convert PyQt to PIL image

但它似乎在 Python3 中不起作用。是否存在将其实现到 Pyhton3 或更简单地使用新方法的方法?

python pyqt pyqt4 python-imaging-library pyqt5

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

如何将从元组列表创建的 png 图像转换回该元组列表?

tupledlist=[(71, 146, 110), (71, 146, 98), (71, 146, 99), (71, 146, 109), (71, 146, 26), (71, 146, 99), (71, 146, 109), (71, 146, 26), (71, 146, 91), (71, 146, 26), (71, 146, 110), (71, 146, 95), (71, 146, 109), (71, 146, 110), (71, 146, 26), (71, 146, 103), (71, 146, 95), (71, 146, 109), (71, 146, 109), (71, 146, 91), (71, 146, 97), (71, 146, 95), (71, 146, 26), (71, 146, 91), (71, 146, 104), (71, 146, 94), (71, 146, …

python python-imaging-library python-3.x

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

Python Tkinter Treeview 添加图像作为列值

我正在尝试将图像添加到树视图上每一行的第一列,但无论我做什么,总是以显示的对象名称“pyimage1”而不是实际图像结束。 正如这张图片所示

我正在使用的代码是这样的。

    from tkinter import PhotoImage.
    self._img = PhotoImage(file="resources\information_picto.gif")
    self.tree.insert('', 'end', values= self._image,self.name, self.status, self.cores, self.turn, self.added_time)
Run Code Online (Sandbox Code Playgroud)

我用 png 尝试过,结果相同,我知道我的图像对象已正确创建,因为在调试时我可以看到图像的属性,但我不能让它显示在树视图行上。

编辑:

 def __init__(self, master, **kw):
    self.SortDir = True
    f = ttk.Frame(master)
    f.pack(fill=BOTH, expand=True)
    self.dataCols = ('Project Name', 'Status', 'Cores', 'Turn', 'Added date/time')
    self.tree = ttk.Treeview(columns=self.dataCols,
                             show='headings')
    self.tree.column("Project Name", anchor="center")

    self.tree.grid(in_=f, row=0, column=0, sticky=NSEW)
    f.rowconfigure(0, weight=1)
    f.columnconfigure(0, weight=1)
    style = ttk.Style(master)
    style.configure('Treeview', rowheight=38)

    self._img = PhotoImage(file="resources\information_picto.gif")  
    self.tree.insert('', 'end', text="#0's text", image=self._img,
                     value=("A's value", "B's value"))
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用上面的代码,它与您的非常相似,但我找不到我的错误,但是我看到“文本”或“图像”字段出现在行上,只是我的值列表作为“价值”传递,有什么想法吗?

python treeview tkinter python-imaging-library photoimage

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

像素坐标与绘图坐标

在下面的代码片段中,传递 x 和 y 值会将点放在 (y,x) 坐标中,而绘图是在 (x,y) 中完成的。设置绘图缓冲区以便在同一坐标系中放置像素和绘图的正确方法是什么?

from PIL import Image, ImageDraw

def visual_test(x, y):
    grid = np.zeros((100, 100, 3), dtype=np.uint8)
    grid[:] = [0, 0, 0]
    grid[x, y] = [255, 0, 0]
    img = Image.fromarray(grid, 'RGB')
    draw = ImageDraw.Draw(img)
    draw.line((x, y, x, y-5), fill=(255,255,255), width=1)
    img.show()
Run Code Online (Sandbox Code Playgroud)

python numpy python-imaging-library pillow

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