gpt4 book ai didi

python - 为什么我的numpy文件比使用同一数组生成的PNG大?

转载 作者:行者123 更新时间:2023-12-02 16:50:40 24 4
gpt4 key购买 nike

我需要找到一种可以容纳我的数组而不占用太多内存的文件类型。我以为npy文件会小于PNG,但我想我错了。

            depth_img = np.uint16(depth * 256)

cv2.imwrite(name_dest_im, depth_img)
np.save(name_dest_npy, depth_img)
enter image description here
是因为我在导出数组时做错了什么,还是PNG是存储uint16的最有效方法?
编辑:这是 https://github.com/nianticlabs/monodepth2/blob/master/test_simple.py的修改的版本
input_image = pil.open(image_path).convert('RGB')
original_width, original_height = input_image.size
input_image = input_image.resize((feed_width, feed_height), pil.LANCZOS)
input_image = transforms.ToTensor()(input_image).unsqueeze(0)

# PREDICTION
input_image = input_image.to(device)
features = encoder(input_image)
outputs = depth_decoder(features)

disp = outputs[("disp", 0)]
disp_resized = torch.nn.functional.interpolate(
disp, (original_height, original_width), mode="bilinear", align_corners=False)

# Saving numpy file
output_name = os.path.splitext(os.path.basename(image_path))[0]
name_dest_npy = os.path.join(output_directory, "{}_disp.npy".format(output_name))
scaled_disp, _ = disp_to_depth(disp, 0.1, 100)


name_dest_im = os.path.join(output_directory, "{}_disp.png".format(output_name))
# Saving colormapped depth image
map_resized_np = scaled_disp.squeeze().cpu().numpy()
map_resized_np = cv2.resize(map_resized_np, (original_width, original_height))
depth = 5.4 / map_resized_np
depth = np.clip(depth, 0, 80)
depth_img = np.uint16(depth * 256)

cv2.imwrite(name_dest_im, depth_img)
np.savez_compressed(name_dest_npy, depth_img)

最佳答案

PNG被压缩,但是np.save不使用压缩。您可以使用np.savez_compressed保存压缩数组。如果您的数据允许,您也可以另存为uint8,如@vlovero在评论中建议的那样。

import cv2
import numpy as np

img = np.random.randint(0, 256, size=(128, 128, 3)).astype(np.uint16)

cv2.imwrite("random.png", img)
np.save("random.npy", img)
np.savez_compressed("random.npz", img=img)

f = np.load("random.npz")
img_loaded = f["img"]
# The loaded array is equal to the original.
np.testing.assert_array_equal(img, img_loaded)
文件大小如下:
$ du -h random*
100K random.npy
64K random.npz
64K random.png

关于python - 为什么我的numpy文件比使用同一数组生成的PNG大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63103569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com