gpt4 book ai didi

python - 调整 48 位 PNG 的大小,保留其 48 位,而不将其删除为 24 位文件

转载 作者:行者123 更新时间:2023-11-28 17:57:44 29 4
gpt4 key购买 nike

我正在尝试将以下 48 位 PNG 从 1242 x 375 调整为 256 x 256但保留其 48 位。

此 PNG 地面实况图像可用于 download here

我想知道是否有一种编码方式可以保留 48 位?

我尝试了几个不同的库,但是生成的文件变成了 24 位 PNG。

# Resize 48bit PNG file and maintain 48bit PNG when saving to file

from PIL import Image
from numpngw import write_png
import cv2
import scipy
import imageio
import skimage

PNG_Location_Filepath = "..\\..\\000000_10.png"
out = "output_images\\"
#The Pillow way
im = Image.open(PNG_Location_Filepath)
PIL_imResized = im.resize((256,256), Image.ANTIALIAS)
libraryname = "Pillow"
savedfilename = out + libraryname + '.png'
PIL_imResized.save(savedfilename)

#The numpngw way
im = cv2.imread(PNG_Location_Filepath, cv2.IMREAD_UNCHANGED)
cv2_imResized = cv2.resize(im, (256,256), interpolation=cv2.INTER_AREA)
libraryname = "numpngw"
savedfilename = out + libraryname + '.png'
write_png(savedfilename, cv2_imResized)

#The Scipy way / ImageIOSkimage way
#im = scipy.misc.imread(PNG_Location_Filepath,mode='RGB')
im = imageio.imread(PNG_Location_Filepath)
#Scipy_imResized = scipy.misc.imresize(im, [256, 256])
Skimage_imResized = skimage.transform.resize(im, (256, 256))
libraryname = "ImageIoSkimage"
savedfilename = out + libraryname + '.png'
#scipy.misc.imsave(savedfilename, Scipy_imResized)
imageio.imwrite(savedfilename, Skimage_imResized)

# `imread` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
# Use ``imageio.imread`` instead
# `imresize` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
# Use ``skimage.transform.resize`` instead
# `imsave` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0
# Use ``imageio.imwrite`` instead.

我也试过这段代码,但是收到了一条错误信息

import cv2
import imageio
imageio.plugins.freeimage.download()
PNG_Location_Filepath = "..\\..\\000000_10.png"
Resized_Location_Filepath = "..\\..\\000000_10_resized.png"

imageio.plugins.freeimage.FreeimagePngFormat.Reader._open
(PNG_Location_Filepath)
img_in_imageio = imageio.imread(PNG_Location_Filepath, format='PNG-FI')
Resized_Image = cv2.resize(img_in_imageio, (256,256))
Saved_Filename = Resized_Location_Filepath
imageio.imwrite(Saved_Filename, Resized_Image, format='PNG-FI')

错误:

Traceback (most recent call last):  File "c:\.vscode\extensions\ms-python.python-2019.6.24221\pythonFiles\ptvsd_launcher.py", line 43, in <module>    main(ptvsdArgs)  File "c:\.vscode\extensions\ms-python.python-2019.6.24221\pythonFiles\lib\python\ptvsd\__main__.py", line 434, in main
run()
File "c:\.vscode\extensions\ms-python.python-2019.6.24221\pythonFiles\lib\python\ptvsd\__main__.py", line 312, in run_file
runpy.run_path(target, run_name='__main__')
File "C:\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "c:\Documents\DeepLearning\Learning\Code\Sandpit\Resize48bitKeeping48bit.py", line 10, in <module>
img_in_imageio = imageio.plugins.freeimage.FreeimagePngFormat.Reader._open(PNG_Location_Filepath)
File "C:\AppData\Local\Programs\Python\Python37\lib\site-packages\freeimage.py", line 221, in _open
return FreeimageFormat.Reader._open(self, flags)
File "C:\AppData\Local\Programs\Python\Python37\lib\site-packages\imageio\plugins\freeimage.py", line 81, in _open
self._bm = fi.create_bitmap(self.request.filename, self.format.fif, flags)
AttributeError: 'str' object has no attribute 'request'

所以我尝试了这段代码

import cv2
import imageio
imageio.plugins.freeimage.download()
PNG_Location_Filepath = "..\\..\\000000_10.png"
Resized_Location_Filepath = "..\\..\\000000_10_resized.png"
img_in_imageio = imageio.imread(PNG_Location_Filepath, format='PNG-FI')
#img_in_imageio.resize((256,256,3))
Resized_Image = cv2.resize(img_in_imageio, (256,256))
Saved_Filename = Resized_Location_Filepath
imageio.imwrite(Saved_Filename, img_in_imageio, format='PNG-FI')

这复制了文件,但没有调整它的大小

我希望保持 uint48 的文件格式,但输出文件似乎是 24 位

最佳答案

您可以在 imageio.imread 中使用 format='PNG-FI'imageiofreeimage 库一起使用/imageoio.imwrite.

基于 freeimage.py 源代码中的信息在 imageio 中安装 freeimage 库你可以使用 imageio:

  • 在命令行中(在 Linux 上即使没有完整路径也能正常工作)

    imageio_download_bin freeimage
  • 使用python代码

    import imageio

    imageio.plugins.freeimage.download()

可能如果您直接从 FreeImage webpage 安装库 (.dll/.so)那么它也会起作用。


必须复制图像(img.copy())。因为缩小图像会删除具有最大值的像素,所以我处理图像的一部分并使其变大。

# read 48bit color
img = imageio.imread("..\\..\\000000_10.png", format='PNG-FI')

# max values in image
print('shape:', img.shape)
print('max R:', img[:,:,0].max())
print('max G:', img[:,:,1].max())
print('max B:', img[:,:,2].max())
print('---')

# cut-off part of image (with)
img = img.copy()
img = img[370:375,1020:1025,:]
img = img.copy()
img.resize((256,256,3))

print('shape:', img.shape)
print('max R:', img[:,:,0].max())
print('max G:', img[:,:,1].max())
print('max B:', img[:,:,2].max())
print('---')

# find X,Y for first max red value
print('max X:', img[:,:,0].max(axis=0).argmax())
print('max Y:', img[:,:,0].max(axis=1).argmax())
print(' flat:', img[:,:,0].argmax())
print('---')

# find X,Y for all max red values
max_r = img[:,:,0].max()

for y, row in enumerate(img[:,:,0]):
for x, it in enumerate(row):
if it == max_r:
print('value/x/y:', max_r, x, y)

# write 48bit color
imageio.imwrite('output_48bit.png', img, format='PNG-FI')

输出:

shape: (375, 1242, 3)
max R: 40827
max G: 36674
max B: 1
---
shape: (256, 256, 3)
max R: 40827
max G: 36506
max B: 1
---
max X: 14
max Y: 0
flat: 14
---
value/x/y: 40827 14 0

在 Linux 中,我可以在命令行中使用程序 `file 来检查文件是否使用 48 位颜色(每种颜色 16 位)

$ file 000000_10.png

000000_10.png: PNG image data, 1242 x 375, 16-bit/color RGB, non-interlaced


$ file output_48bit.png

output_48bit.png: PNG image data, 256 x 256, 16-bit/color RGB, non-interlaced

如果你有 RGBA 那么它将使用 64 位颜色。

imageio 问题示例:Unable to properly read multi-channel 16-bit png files

import imageio
import numpy as np

img_out = np.zeros((256, 256, 4), dtype=np.uint16)
color_grad = np.reshape(np.arange(2**16), (256,-1))
img_out[:, :, 0] = color_grad
img_out[:, :, 1] = np.rot90(color_grad, 1)
img_out[:, :, 2] = np.rot90(color_grad, 2)
img_out[:, :, 3] = np.rot90(color_grad, 3)

print('Write unique values: R={}, G={}, B={}, A={}'.format(
len(set(img_out[:, :, 0].flatten().tolist())),
len(set(img_out[:, :, 1].flatten().tolist())),
len(set(img_out[:, :, 2].flatten().tolist())),
len(set(img_out[:, :, 3].flatten().tolist()))))
imageio.imwrite('64bit_imageio.png', img_out, format='PNG-FI')

img_in_imageio = imageio.imread('64bit_imageio.png', format='PNG-FI')
print('imageio PNG unique values: R={}, G={}, B={}, A={}'.format(
len(set(img_in_imageio[:, :, 0].flatten().tolist())),
len(set(img_in_imageio[:, :, 1].flatten().tolist())),
len(set(img_in_imageio[:, :, 2].flatten().tolist())),
len(set(img_in_imageio[:, :, 3].flatten().tolist()))))

输出:

Write unique values: R=65536, G=65536, B=65536, A=65536
imageio PNG unique values: R=65536, G=65536, B=65536, A=65536

output_48bit.png: PNG image data, 5 x 5, 16-bit/color RGB, non-interlaced

编辑:您的最后一个代码具有更易读的变量名称(小写字母名称)和少量空闲行以使其也更具可读性。

在原始代码中,你搞得一团糟,所以最后你写了原始图像而不是调整大小的图像。

import cv2
import imageio

# need it only once
#imageio.plugins.freeimage.download()

input_filename = "..\\..\\000000_10.png"
output_filename = "..\\..\\000000_10_resized.png"

input_image = imageio.imread(input_filename, format='PNG-FI')
output_image = cv2.resize(input_image, (256, 256))

imageio.imwrite(output_filename, output_image, format='PNG-FI')

关于python - 调整 48 位 PNG 的大小,保留其 48 位,而不将其删除为 24 位文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57440861/

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