如何使用numpy调整图像数据的大小?

izj3ouym  于 2023-04-12  发布在  其他
关注(0)|答案(4)|浏览(198)

我想在python中调整图像数据的大小,但简单的numpy.resize似乎不起作用。
我读取图像,并尝试使用以下脚本调整其大小。为了检查,我写入了导致意外结果的文件。

from PIL import Image
import numpy as np

# Read image data as black-white image
image = Image.open(input_image).convert("L")
arr = np.asarray(image)

# resize image by factor of 2 (original image has shape (834, 1102) )
image = np.resize(arr, (417, 551))

# same resized image to check if it worked
im = Image.fromarray(image)
im.save("test.jpeg")

原始图像:

缩放后的图像:

我希望看到同样的图像(一架飞机),但只是更小,分辨率更小。
我做错了什么?

k75qkfdt

k75qkfdt1#

有多个答案,并且“所有”看起来都有效。我想再添加一个并比较它们:

答案

您可以使用PIL的resize方法:

from PIL import Image
image = Image.open(input_image)
w, h = image.size
resized_image = image.resize((int(w * 0.5), int(h * 0.5)))

对比

我以为PIL会更快。
下面是比较这些方法的脚本
1.方法1:@asds_asds的numpy方法。
1.方法2:我提供的PIL的调整大小方法。
1.方法3:@Mark Setchell的方法。
剧本:

from PIL import Image, ImageOps
import numpy as np
from time import perf_counter
from matplotlib import pyplot as plt

data = []

for i in range(100, 5000, 200):
    print(i)
    zeros = np.zeros((i, i))
    IMAGE = Image.fromarray(zeros)

    np_start = perf_counter()
    a = np.array(IMAGE)
    _ = a[::2, ::2]
    np_end = perf_counter()

    pil_res_start = perf_counter()
    width, height = IMAGE.size
    IMAGE.resize((int(width * 0.5), int(height * 0.5)))
    pil_res_end = perf_counter()

    pil_scl_start = perf_counter()
    _ = ImageOps.scale(IMAGE, 0.5)
    pil_scl_end = perf_counter()

    data.append([i, np_end - np_start, pil_res_end - pil_res_start, pil_scl_end - pil_scl_start])

data = np.array(data)
plt.plot(data[:, 0], data[:, 1], label="numpy")
plt.plot(data[:, 0], data[:, 2], label="PIL Resize")
plt.plot(data[:, 0], data[:, 3], label="PIL Scale")
plt.legend()
plt.xlabel(r"$\sqrt{n_{pix}}$")
plt.ylabel(r"perf_counter (sec)")
plt.title("Numpy vs PIL resize")
plt.show()

这里的结果:

TL;DR

虽然numpy更快,但PIL提供的代码更干净,差距也不是那么明显。所以PIL可能看起来更好...

mnemlml8

mnemlml82#

from PIL import Image
import numpy as np

# Read image data as black-white image
image = Image.open(input_image).convert("L")
arr = np.asarray(image)

# resize image by factor of 2 (original image has shape (834, 1102) )
image = arr[::2, ::2]

# same resized image to check if it worked
im = Image.fromarray(image)
im.save("test.jpeg")

你可以在谷歌上搜索的术语是上采样和下采样。

gg58donl

gg58donl3#

我想就像这个例子我的代码

from PIL import Image
import numpy as np

input_image = r"airplane.jpg"

# Read image data as black-white image
image = Image.open(input_image).convert("L")
arr = np.asarray(image)

# calculate new height and width based on resize factor
resize_factor = 0.5
height, width = arr.shape
new_height = int(height * resize_factor)
new_width = int(width * resize_factor)

# resize image using numpy
resized_image = arr[::2, ::2]

# save resized image
im = Image.fromarray(resized_image)
im.save("resized_image.jpeg")

# show resized image
im.show()

输出:

标签:

um6iljoc

um6iljoc4#

如果你想缩放图像,根本不需要使用Numpy。已经安装的PIL可以做到这一点:

from PIL import Image, ImageOps
im = Image.open('w3IAi.jpg').convert('L')
ImageOps.scale(im, 0.5).save('result.png')

相关问题