调整 48 位 PNG 的大小保留其 48 位,而不将其拖放到 24 位文件

  • 本文关键字:拖放 文件 PNG 保留 调整 python
  • 更新时间 :
  • 英文 :


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

此 PNG 地面实况图像可在此处下载

我想知道是否有办法对其进行编码以保留 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:.vscodeextensionsms-python.python-2019.6.24221pythonFilesptvsd_launcher.py", line 43, in <module>    main(ptvsdArgs)  File "c:.vscodeextensionsms-python.python-2019.6.24221pythonFileslibpythonptvsd__main__.py", line 434, in main
run()
File "c:.vscodeextensionsms-python.python-2019.6.24221pythonFileslibpythonptvsd__main__.py", line 312, in run_file
runpy.run_path(target, run_name='__main__')
File "C:AppDataLocalProgramsPythonPython37librunpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:AppDataLocalProgramsPythonPython37librunpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:AppDataLocalProgramsPythonPython37librunpy.py", line 85, in _run_code
exec(code, run_globals)
File "c:DocumentsDeepLearningLearningCodeSandpitResize48bitKeeping48bit.py", line 10, in <module>
img_in_imageio = imageio.plugins.freeimage.FreeimagePngFormat.Reader._open(PNG_Location_Filepath)
File "C:AppDataLocalProgramsPythonPython37libsite-packagesfreeimage.py", line 221, in _open
return FreeimageFormat.Reader._open(self, flags)
File "C:AppDataLocalProgramsPythonPython37libsite-packagesimageiopluginsfreeimage.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/imageoio.imwrite中的format='PNG-FI'imageiofreeimage库一起使用。

根据imageio中 freeimage.py 源代码中的信息来安装freeimage库,您可以使用imageio

  • 命令行中(在 Linux 上,即使没有完整路径也可以工作(

    imageio_download_bin freeimage
    
  • 使用蟒蛇代码

    import imageio
    imageio.plugins.freeimage.download()
    

可能如果您直接从FreeImage网页安装库(.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 中,我可以使用程序"命令行中的文件"来检查文件是否使用 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问题的示例:无法正确读取多通道 16 位 png 文件

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

编辑:您的最后一个代码具有更易读的变量名称(lower_case_names(和很少的自由行,以使其也更具可读性。

在原始代码中,你遇到了混乱,所以最后你写了原始图像而不是调整大小的图像。

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')
# Resize 48bit PNG file and maintain 48bit PNG when saving to file WORKING
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')
Resized_Image = cv2.resize(img_in_imageio, (256,256))
Saved_Filename = Resized_Location_Filepath
imageio.imwrite(Saved_Filename, Resized_Image, format='PNG-FI')
#This works to resize the image, keeping 48bits

最新更新