【问题标题】:How to pixelate a square image to 256 big pixels with python?如何使用 python 将方形图像像素化为 256 个大像素?
【发布时间】:2018-04-18 23:50:01
【问题描述】:

我需要找到一种方法,使用 python 将方形图像缩小到 256 个大像素,最好使用 matplotlib 和枕头库。

有什么想法吗?

【问题讨论】:

  • 什么是“大像素”?您是否只想将图像大小调整为 16x16 像素?
  • 使用默认插值将其缩小到 16x16,使用最近邻插值将其缩放回原始大小。

标签: python image matplotlib pillow


【解决方案1】:

九个月过去了,我现在可以拼凑一些 Python - 根据您关于如何使用 Python 和 PIL/Pillow 对图像进行像素化的原始请求。

#!/usr/local/bin/python3
from PIL import Image

# Open Paddington
img = Image.open("paddington.png")

# Resize smoothly down to 16x16 pixels
imgSmall = img.resize((16,16),resample=Image.BILINEAR)

# Scale back up using NEAREST to original size
result = imgSmall.resize(img.size,Image.NEAREST)

# Save
result.save('result.png')

原图

结果


帕丁顿太可爱了——他只需要被像素化!

如果你把他缩小到 32x32 像素(而不是 16x16)然后重新调整大小,你会得到:

关键字

像素化、像素化、像素化、像素化、Paddington、Python、Pillow、PIL、图像、图像处理、最近邻插值。

【讨论】:

【解决方案2】:

另一种选择是使用PyPXL

在 Lab 颜色空间中使用 K-Means 聚类对图像和视频进行像素化的 Python 脚本。视频像素化支持多处理以获得更好的性能。

使用 Paddington 图像作为您可以运行的源:

python pypxl_image.py -s 16 16 paddington.png paddington_pixelated.png

这给出了这个结果

当然,如果您希望它具有 256 x 256 像素,而不仅仅是 256 大像素,您可以运行

python pypxl_image.py -s 256 256 paddington.png paddington_pixelated.png

这给出了这个结果

与其他解决方案相比,这两个结果都具有更复古的 8 位外观,这可能符合您的需求。

【讨论】:

    【解决方案3】:

    抱歉,我无法为您提供 Python 解决方案,但我可以向您展示技术和结果,只需在命令行中使用 ImageMagick

    从这里开始:

    首先,使用普通三次或双线性插值将图像缩小到 16x16 像素,然后使用 “最近邻”插值将图像缩放回其原始大小:

    convert paddington.png -resize 16x16 -scale 400x400 result.png
    

    关键字

    像素化、像素化、像素化、像素化、帕丁顿、ImageMagick、命令行、命令行、图像、图像处理、最近邻插值。

    【讨论】:

      【解决方案4】:

      这是我的解决方案

      import numpy as np
      import matplotlib.pyplot as plt
      
      def pixelate_rgb(img, window):
          n, m, _ = img.shape
          n, m = n - n % window, m - m % window
          img1 = np.zeros((n, m, 3))
          for x in range(0, n, window):
              for y in range(0, m, window):
                  img1[x:x+window,y:y+window] = img[x:x+window,y:y+window].mean(axis=(0,1))
          return img1
      
      img = plt.imread('test.png')
      
      fig, ax = plt.subplots(1, 4, figsize=(20,10))
      
      ax[0].imshow(pixelate_rgb(img, 5))
      ax[1].imshow(pixelate_rgb(img, 10))
      ax[2].imshow(pixelate_rgb(img, 20))
      ax[3].imshow(pixelate_rgb(img, 30))
      
      # remove frames
      [a.set_axis_off() for a in ax.flatten()]
      plt.subplots_adjust(wspace=0.03, hspace=0)
      

      还有一个思路是处理灰度图:

      def pixelate_bin(img, window, threshold):
          n, m = img.shape
          n, m = n - n % window, m - m % window
          img1 = np.zeros((n,m))
          for x in range(0, n, window):
              for y in range(0, m, window):
                  if img[x:x+window,y:y+window].mean() > threshold:
                      img1[x:x+window,y:y+window] = 1
          return img1
      
      # convert image to grayscale
      img = np.dot(plt.imread('test.png'), [0.299 , 0.587, 0.114])
      
      fig, ax = plt.subplots(1, 3, figsize=(15,10))
      
      plt.tight_layout()
      ax[0].imshow(pixelate_bin(img, 5, .2), cmap='gray')
      ax[1].imshow(pixelate_bin(img, 5, .3), cmap='gray')
      ax[2].imshow(pixelate_bin(img, 5, .45), cmap='gray')
      
      # remove frames
      [a.set_axis_off() for a in ax.flatten()]
      plt.subplots_adjust(wspace=0.03, hspace=0)
      

      记住:png 的值介于 0 和 1 之间,而 jpg 的值介于 0 和 255 之间

      【讨论】:

        猜你喜欢
        • 2016-02-09
        • 2013-12-23
        • 2019-08-25
        • 2012-08-17
        • 2019-08-25
        • 1970-01-01
        • 1970-01-01
        • 2020-06-03
        • 2015-11-27
        相关资源
        最近更新 更多