当尝试使用Pillow在 tkinter 中显示图像时,我遇到了一个奇怪的问题。
我最初尝试以默认的 tkinter 方式显示图像,这对于 gif 效果很好:
import tkinter as tk
root = tk.Tk()
src = tk.PhotoImage(file = "C:\\Users\\Matt\\Desktop\\K8pnR.gif")
label = tk.Label(root, image = src)
label.pack()
Run Code Online (Sandbox Code Playgroud)
(K8pnR只是我在 imgur 上找到的随机 gif)
这很好用,但唯一的问题是我想显示其他文件类型。这让我想到了 Pillow,因为我正在使用 Python 3.4。我尝试从显示相同的文件开始,但使用 Pillow:
import tkinter as tk
from PIL import Image
root = tk.Tk()
src = Image.open("C:\\Users\\Matt\\Desktop\\K8pnR.gif")
img = tk.PhotoImage(file = src)
label = tk.Label(image = img, master = root)
label.pack()
Run Code Online (Sandbox Code Playgroud)
这会导致一个非常奇怪和丑陋的no such file or directory错误:
Traceback (most recent call last): …Run Code Online (Sandbox Code Playgroud) 问题:我无法在 tkinter.ttk.Treeview 中的根节点旁边显示图标图像。下面是我使用的测试代码。它执行时没有错误,但图像没有出现在根节点的左侧。我尝试过使用图像文件的完整路径名,但这不起作用。另外,我尝试使用 PIL.ImageTk.PhotoImage 打开图像文件,但这也不起作用。相反,出现了如下所示的错误。
问题:如何让图标图像出现在 tkinter.ttk.Treeview 的根节点(或任何节点)的左侧?
测试代码:
import os
import tkinter as tk
import tkinter.ttk as ttk
from PIL import Image, ImageTk
class App(ttk.Frame):
def __init__(self, master, path):
ttk.Frame.__init__(self, master)
self.tree = ttk.Treeview(self)
ysb = ttk.Scrollbar(self, orient='vertical', command=self.tree.yview)
xsb = ttk.Scrollbar(self, orient='horizontal', command=self.tree.xview)
self.tree.configure(yscroll=ysb.set, xscroll=xsb.set)
self.tree.heading('#0', text='Directory', anchor='w')
abspath = os.path.abspath(path)
i = './icon/Home-icon_16.gif'
root_pic = tk.PhotoImage(file=i)
#root_pic = ImageTk.PhotoImage(i)
root_node = self.tree.insert('', 'end', text=abspath, open=True, image=root_pic)
l1_node = self.tree.insert(root_node, 'end', text='level 1', open=True)
l2_node = …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 csv 文件转换为 RGB 文件,如下所示:
from PIL import Image, ImageDraw
from numpy import genfromtxt
g = open('myFile.csv','r')
temp = genfromtxt(g, delimiter = ',')
im = Image.fromarray(temp).convert('RGB')
pix = im.load()
rows, cols = im.size
for x in range(cols):
for y in range(rows):
print str(x) + " " + str(y)
pix[x,y] = (int(temp[x,y] // 256 // 256 % 256),int(temp[x,y] // 256 % 256),int(temp[x,y] % 256))
im.save(g.name[0:-4] + '.jpeg')
Run Code Online (Sandbox Code Playgroud)
问题是倒数第二行给了我一个“IndexError:图像索引超出范围”。当我打印出 x 和 y 时,我发现只要它们中的任何一个达到 5(我的文件的宽度,而不是高度),就会发生这种情况,无论我如何使用它,无论 x 或 y 达到 5,它都会引发错误这就是我不明白的。也欢迎任何更好的实现。
import PIL as Image
Image.fromarray(cv2.imread(link, cv2.IMREAD_GRAYSCALE))
Run Code Online (Sandbox Code Playgroud)
我目前正在尝试完成一个项目,但我的 Linux GPU 服务器上不断出现太多文件打开错误,导致服务器崩溃。
我正在使用如上所示的代码加载 3 个图像以进行 CNN 分类。任何面临同样问题的人都有解决方案吗?
谢谢。
我正在尝试修复对 Image 模块的调用(直到现在我才知道这个 Python 成像库),并且我需要升级Pillow,因为 中的文件/usr/lib/python3/dist-packages/PIL/Image.py声明版本为 1.1.7,但有更新的版本可用。
$ sudo pip3 install Pillow
[sudo] password di user:
The directory '/home/user/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled.
Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/user/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. …Run Code Online (Sandbox Code Playgroud) 我正在努力将图像裁剪为矩形大小,并将其放置在图像内部。这是原始图像:
到目前为止,我能够输入它,将其颜色转换为 HSV 颜色空间并对其应用阈值。到目前为止,这是我的代码:
import cv2
#Read input image
img = cv2.imread('rdm_generated_image.png')
#convert from BGR to HSV color space
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
#get the saturation plane - all black/white/gray pixels are zero, and colored pixels are above zero.
s = hsv[:, :, 1]
#apply threshold on s
ret, thresh = cv2.threshold(s, 8, 255, cv2.THRESH_BINARY)
#invert colors, so every dark spots are now white
image = cv2.bitwise_not(thresh)
cv2.imwrite("image.png", image)
Run Code Online (Sandbox Code Playgroud)
完成此操作后,程序将输出以下内容:
现在我只想将图像裁剪到中间的大框,但我无法检测到它的轮廓。
我尝试了它和cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)其他一些功能,但还没有成功。如果这是错误的做法,请纠正我。感谢帮助,因为我对 OpenCV 缺乏经验。
提前致谢!
不要在本地下载字体并将其链接到 ImageFont.truetyp() ,如下所示:
from pillow import ImageFont
font = ImageFont.truetype('Roboto-Regular.ttf', size=10)
Run Code Online (Sandbox Code Playgroud)
我可以做这样的事情吗:
from pillow import ImageFont
font = ImageFont.truetype('https://github.com/googlefonts/roboto/blob/master/src/hinted/Roboto-Regular.ttf', size=10)
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用在图像顶部添加叠加层cv2.addWeighted(...),但它会引发以下错误:
dst = cv2.addWeighted(logo, alpha, overlay, 1-alpha, 0)
cv2.error: OpenCV(4.4.0) /tmp/pip-req-build-99ib2vsi/opencv/modules/core/src/arithm.cpp:691: error: (-5:Bad argument) When the input arrays in add/subtract/multiply/divide functions have different types, the output array type must be explicitly specified in function 'arithm_op'
Run Code Online (Sandbox Code Playgroud)
这个错误对我来说没有意义,因为我检查了文档并且我的论点是适当的。这是我的代码。
def overlay(path):
logo = cv2.imread(path, cv2.IMREAD_UNCHANGED)
alpha = logo[:, :, 3]
overlay = np.zeros(logo.shape, dtype=np.uint8)
overlay[:, :, 2] = alpha
overlay[:, :, 3] = alpha
alpha = 0.5
dst = cv2.addWeighted(logo, alpha, overlay, 1-alpha, 0)
pil_image = Image.fromarray(dst).convert('RGBA')
return pil_image
Run Code Online (Sandbox Code Playgroud)
更新
所以覆盖现在可以工作,但存在一些问题。我对我的代码进行了以下更改: …
我刚刚在处理一个项目时遇到了 pythons PIL 库,所以对此非常陌生。
我的简单程序从目录导入一张图像,使用 PIL 应用所需的操作,然后将其保存到另一个文件夹中。
我的问题 - 我可以批量导入包含多个图像的目录并在 PIL 中对该目录中的所有图像运行所需的操作吗?
我试图使图像部分透明,然后将其叠加在第二个图像上。例如,获取此图像并将其透明度设置为 50%,然后将其粘贴到此图像上以创建此合成图像。我尝试使用以下代码来做到这一点:
from PIL import Image
background = Image.open("one.png")
foreground = Image.open("circle.png")
foreground.putalpha(120) # Sets the oval to 50% transparency
background.paste(foreground, (0, 0)) # Paste oval over the background
background.show()
Run Code Online (Sandbox Code Playgroud)
然而,当我尝试这个时,它只显示前景。是否可以通过这种方式使用 PIL/Pillow 调整透明度以获得所需的合成图像?
python ×7
opencv ×2
automation ×1
crop ×1
genfromtxt ×1
gif ×1
image ×1
jpeg ×1
numpy ×1
overlay ×1
pip ×1
png ×1
python-3.7 ×1
python-3.x ×1
pytorch ×1
treeview ×1
ttk ×1