从图像中删除抗锯齿功能

2024-01-10

我想从图像中删除抗锯齿功能。此代码将从图像中获取 4 种主要颜色,将每个像素与 4 种主要颜色进行比较并分配最接近的颜色。

import numpy as np
from PIL import Image

image = Image.open('pattern_2.png')
image_nd = np.array(image)
image_colors = {}

for row in image_nd:
    for pxl in row:
        pxl = tuple(pxl)
        if not image_colors.get(pxl):
            image_colors[pxl] = 1
        else:
            image_colors[pxl] += 1

sorted_image_colors = sorted(image_colors, key=image_colors.get, reverse=True)
four_major_colors = sorted_image_colors[:4]


def closest(colors, color):
    colors = np.array(colors)
    color = np.array(color)
    distances = np.sqrt(np.sum((colors - color) ** 2, axis=1))
    index_of_smallest = np.where(distances == np.amin(distances))
    smallest_distance = colors[index_of_smallest]
    return smallest_distance[0]


for y, row in enumerate(image_nd):
    for x, pxl in enumerate(row):
        image_nd[y, x] = closest(four_major_colors, image_nd[y, x])

aliased = Image.fromarray(image_nd)
aliased.save("pattern_2_al.png")

This is the result:
enter image description here

正如您所看到的,颜色之间的边界并不完美。

这就是我追求的结果:

(图像托管网站似乎压缩了图像,并且不会正确显示“锯齿”图像)


这里的主要问题在于你的closest method:

def closest(colors, color):
    colors = np.array(colors)
    color = np.array(color)
    distances = np.sqrt(np.sum((colors - color) ** 2, axis=1))

Both colors and color成为 NumPy 类型的数组uint8。现在,当减去uint8值,你不会得到负值,但会发生整数下溢,导致值接近255。因此,随后计算出distances都是错误的,最终导致颜色选择错误。

因此,最快的解决方法是将这两个变量转换为int32:

def closest(colors, color):
    colors = np.array(colors).astype(np.int32)
    color = np.array(color).astype(np.int32)
    distances = np.sqrt(np.sum((colors - color) ** 2, axis=1))

此外,利用 NumPy 的矢量化功能可能很有用。为您考虑以下方法closest method:

def closest(colors, image):
    colors = np.array(colors).astype(np.int32)
    image = image.astype(np.int32)
    distances = np.argmin(np.array([np.sqrt(np.sum((color - image) ** 2, axis=2)) for color in colors]), axis=0)
    return colors[distances].astype(np.uint8)

因此,不要迭代所有像素

for y in np.arange(image_nd.shape[0]):
    for x in np.arange(image_nd.shape[1]):
        image_nd[y, x] = closest(four_major_colors, image_nd[y, x])

你可以简单地传递整个图像:

image_nd = closest(four_major_colors, image_nd)

使用给定的图像,我的机器速度提高了 100 倍。当然,查找 RGB 直方图值也可以进行优化。 (不幸的是,我对 Python 字典的体验还不是很好......)

不管怎样——希望有帮助!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从图像中删除抗锯齿功能 的相关文章

随机推荐

  • Linq:选择属性集合

    我有两节课 public class Person public int Id get set public string Name get set public List
  • 是什么原因导致错误“_pickle.UnpicklingError:无效的加载密钥,''。”?

    我正在尝试在数组中存储 5000 个数据元素 这 5000 个元素存储在现有文件中 因此它不为空 但我收到错误 IN def array name puntos df4 m open name rb v 5000 m seek 5000 i
  • “NoneType”对象没有属性“remove_roles”Discord.py

    Keep getting an error for the reaction remove just copy pasted my whole code minus the client id cause it might help I h
  • 为什么我的 Promise 数组在调用 Promise.all() 之前运行?

    我正在尝试创建 Promise 数组 然后使用 Promise all 解析它们 我正在使用 got 它返回一个承诺 我的代码可以工作 但我不完全理解如何工作 这里是 const got require got const url myUr
  • 在 AngularJS 中执行 ng-repeat 内的函数

    我想在 ng repeat 中执行一个函数来检索一些其他数据来显示 例如 我有一份公寓列表 我使用以下方式显示此列表ng repeat 比我想向业主展示的每套公寓 这不是u Apartments So my getInq函数调用服务来获取指
  • Android 驱动程序 JDBC PostgreSQL [重复]

    这个问题在这里已经有答案了 我正在尝试使用 JDBC 驱动程序将我的 Android 应用程序连接到服务器 PostgreSQL 但出现以下错误 java lang ClassNotFoundException org postgresql
  • python 和 networkX keyerror

    我在 python 中遇到这个问题 python 不断给我一个关键错误 重量 g add edge 1 3 weight 2 5 g 1 2 weight 1 5 for n1 n2 attr in g edges data True pr
  • 将 PILLOW 图像转换为 StringIO

    我正在编写一个程序 它可以接收各种常见图像格式的图像 但需要以一种一致的格式检查它们 什么图像格式并不重要 主要是它们都是相同的 由于我需要转换图像格式然后继续处理图像 因此我不想将其保存到磁盘 只需转换它并继续 这是我使用 StringI
  • 编写一个简单的 cron 作业来运行 Java 类

    如何从头开始编写一个 cron 作业来运行 java 类 或者编写一个嵌入 Java 代码来运行的 cron 作业类 以及如何设置计时器每隔一分钟 例如 运行该 cron 作业 注意 完全是 Linux 初学者 这是运行测试作业的示例 sh
  • JNA:结构类中 getFieldOrder() 的用途是什么

    我正在尝试调用 dll 文件中存在的 C 函数 C 函数通过引用将结构对象作为参数 并且函数将在该函数中赋值 因此 在我的 java 应用程序中 为了将结构对象传递给函数 我确实这样写 interface SomeInterface ext
  • 应用顺序/按值调用和正常顺序/按名称调用差异

    背景 我正在根据在线课程学习 sicp 并对其讲义感到困惑 在讲义中 应用顺序似乎等于 cbv 正常顺序等于 cbn 困惑 But the wiki http en wikipedia org wiki Evaluation strateg
  • 使用自定义 std::set 比较器

    我正在尝试将一组整数中项目的默认顺序更改为字典顺序而不是数字顺序 但我无法使用 g 编译以下内容 文件 cpp bool lex compare const int64 t a const int64 t b stringstream s1
  • Rails ActionMailer 与 Devise + Google Apps 处于开发模式

    我正在尝试将 ActionMailer 配置为使用我的 Google Apps 帐户在开发模式下从 Devise 发送邮件 我已将以下内容添加到我的 config environments development rb 文件中 但看起来邮件
  • 动态获取 Javascript 变量中的数据库值

    我多次研究过这个主题 但找不到我的问题的正确答案 让我解释一下 我正在使用 Google Maps API 创建一个应用程序 我希望根据我的数据库值在地图上显示多个位置 我的 javascript 中有一个名为 Locations 的对象
  • 如果缺少 http:// 前缀,请添加到 URL

    你好 我有一个非常简单的代码 a href target self div class callButton Website div a 问题是 如果用户不输入 http 链接将指向我的网站 而不是应有的外部网站 如何在 PHP 中检查用户
  • 动画因过渡而暂停

    我有一个动画移动background position带有关键帧的图像 效果很好 虽然 当用户单击按钮时 我想暂停背景的动画 但要进行过渡 减慢速度然后停止background position从移动 我搜索了类似的东西 但没有找到任何东西
  • 基于 GUI 或基于 Web 的 JSON 编辑器,其工作方式类似于属性资源管理器 [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 即使获得焦点并且 CSS 规则生效,HTML 按钮也不会单击

    看看这个按钮 http jsfiddle net vtortola Dnxpe http jsfiddle net vtortola Dnxpe 在Chrome中 如果点击顶部边框 即使 hover 和 active css规则触发 事件也
  • 根据另一个选择选项从数据库提供选择选项

    我有一个表格 有两个选择字段 一个代表地区 一个代表城市 村庄 等的名称 我有一个数据库 其中包含以下形式的所有这些条目 id int 11 ai region varchar 50 city varchar 50 我在这里找到了一个脚本
  • 从图像中删除抗锯齿功能

    我想从图像中删除抗锯齿功能 此代码将从图像中获取 4 种主要颜色 将每个像素与 4 种主要颜色进行比较并分配最接近的颜色 import numpy as np from PIL import Image image Image open p