小编Vic*_*gos的帖子

如何将Python/Pillow中的PNG图像调色板减少到真正使用的颜色?

在处理了具有透明度的先前优化的索引颜色PNG图像之后(参见此处针对某些背景,因为该问题涉及相同的图像文件),使用以下代码,PLTE块似乎扩展为具有比有效使用的颜色更多的颜色.

Mu当前代码:

#!/usr/bin/env/python3
import os

from PIL import Image


source_file = os.path.expanduser("~/Desktop/prob.png")
dest_file = os.path.expanduser("~/Desktop/processed_img.png")

img = Image.open(source_file)

# Convert all colors in the palette to grayscale and save the new palette
pal = img.getpalette()
for i in range(len(pal) // 3):
    # Using ITU-R 601-2 luma transform
    g = (pal[3*i] * 299 + pal[3*i+1] * 587 + pal[3*i+2] * 114) // 1000
    pal[3*i: 3*i+3] = [g, g, g]

img.putpalette(pal)

try:
    img.save(dest_file, optimize=True, format="PNG")
except IOError:
    ImageFile.MAXBLOCK = img.size[0] …
Run Code Online (Sandbox Code Playgroud)

png image-compression python-3.x pypng pillow

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

在Python中使用这两种方式建立Web连接的实际区别是什么?

我注意到有几种方法可以为Web报废提供http连接.我不确定某些是更近期和最新的编码方式,还是它们只是具有不同优点和缺点的不同模块.更具体地说,我试图了解以下两种方法之间的区别,你会推荐什么?

1)使用urllib3:

http = PoolManager()
r = http.urlopen('GET', url, preload_content=False)
soup = BeautifulSoup(r, "html.parser")
Run Code Online (Sandbox Code Playgroud)

2)使用请求

html = requests.get(url).content
soup = BeautifulSoup(html, "html5lib")
Run Code Online (Sandbox Code Playgroud)

是什么将这两个选项区分开来,除了它们需要导入不同模块的简单事实?

http urllib3 python-3.x python-requests bs4

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

如何在Python中显示系统通知?

使用Python显示系统通知的最佳方式是什么,最好使用tkinter进行跨平台实现(我在OS X上,所以我也愿意接受允许与Notification Center集成的实现)?

我想展示一个自我破坏的消息,我的意思是会在屏幕上停留几秒然后会消失,但不会干扰用户交互.我不想使用消息框,因为在要求用户单击按钮以关闭消息窗口.

您有什么推荐的吗?

python notifications tkinter

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

在 Python3/tkinter 中如何在用户单击 Toplevel 窗口上的关闭按钮时进行拦截

我想要一个主窗口和一个或多个Toplevel()可以按需打开的窗口。我能够创建窗户,甚至摧毁它们。

但是,我试图在主窗口中实现一个按钮,可以打开和关闭第二个窗口(第二个窗口应该始终是唯一的,即永远不会同时打开两次)。这就是我现在所拥有的,在摆弄了一下之后:

#!/usr/bin/python3

import tkinter as tk
from tkinter import ttk
import tkinter.font


class baseApp(ttk.Frame):
    def __init__(self,master,*args,**kwargs):
        super().__init__(master,*args,**kwargs)
        self.master = master

        self.mainframe = ttk.Frame(master)
        self.topframe = ttk.Frame(self.mainframe, padding="5 8 5 5")

        self.topframe.pack(side=tk.TOP, fill=tk.X)
        self.mainframe.pack(side=tk.TOP, expand=True, fill=tk.BOTH)


class App(baseApp):
    def __init__(self,master,*args,**kwargs):
        super().__init__(master,*args,**kwargs)
        self.master = master
        self.button1 = ttk.Button(self.topframe,text="One",command=self.button_one)
        self.btn_remessas = ttk.Button(self.topframe,text="Open/close Toplevel window",command=self.create_window1)
        self.button1.grid(row=0,column=0)
        self.btn_remessas.grid(row=0,column=1)
        self.topframe.pack(side=tk.TOP, fill=tk.X)
        self.mainframe.pack(side=tk.TOP, expand=True, fill=tk.BOTH)

    def create_window1(self):
        if current_state.window2_open == False:
            self.newWindow2 = tk.Toplevel(self.master)
            self.newWindow2.geometry('600x500+680+0')
            self.newWindow2.title('Second window')
            self.janela_remessas = SecondWindow(self.newWindow2)
            current_state.window2_open = True              
        else: …
Run Code Online (Sandbox Code Playgroud)

oop user-interface tkinter python-3.x

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

在Python中计算两个图像之间的绝对差之和的最快方法是什么?

我正在尝试比较使用 Pillow 和(可选)Numpy 的 Python 3 应用程序中的图像。出于兼容性原因,我不打算使用其他外部非纯Python包。我在 Roseta 代码中找到了这个基于 Pillow 的算法,它可能符合我的目的,但需要一些时间:

from PIL import Image

def compare_images(img1, img2):
    """Compute percentage of difference between 2 JPEG images of same size
    (using the sum of absolute differences). Alternatively, compare two bitmaps
    as defined in basic bitmap storage. Useful for comparing two JPEG images
    saved with a different compression ratios.

    Adapted from:
    http://rosettacode.org/wiki/Percentage_difference_between_images#Python

    :param img1: an Image object
    :param img2: an Image object
    :return: A float with the percentage of difference, or None …
Run Code Online (Sandbox Code Playgroud)

python arrays numpy image-processing python-imaging-library

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

为什么此 C 代码在 macOS 上生成分段错误,但在其他系统上却不会?

在尝试用 C 实现双向链表时,我注意到以下代码片段会在 macOS 10.11 El Capitan 上引发分段错误。然而,当在 Linux 或 Haiku 中测试时,它会愉快地运行,产生预期的结果。

#include <stdio.h>
#include <stdlib.h>

typedef struct node_structure {
    int data;
    struct node_structure *prev;
    struct node_structure *next;
} *node;

node createNode(int value) {
    node newNode = (node) malloc(sizeof(node));
    if (newNode != NULL) {
        newNode->data = value;
        newNode->prev = NULL;
        newNode->next = NULL;
    }
    return newNode;
}

void displayLinkedList(node linked_list) {
    node cursor = linked_list;
    while (cursor != NULL) {
        printf("DATA: %d \tTHIS:%p \tPREV:%p \tNEXT:%p\n", cursor->data, (void*)cursor, (void *)cursor->prev, (void …
Run Code Online (Sandbox Code Playgroud)

c macos segmentation-fault data-structures doubly-linked-list

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