【问题标题】:Resizing a 48bit PNG retaining its 48bits, without dropping it to a 24bit file调整 48 位 PNG 的大小,保留其 48 位,而不将其放入 24 位文件
【发布时间】:2019-08-13 20:55:55
【问题描述】:

我正在尝试将以下 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 问题中,您可以看到如何使用 FreeImage 插件来创建 64 位 png (RGBA)。 Unable to properly read multi-channel 16-bit png files。它适用于我在 Linux 上。
  • Furas,我实际上是在调查之前的挑战时阅读了这篇文章。我在这里看到这段代码:- github.com/imageio/imageio/blob/master/imageio/plugins/… 但是我不清楚如何使用 freeimage.py 读取图像文件,你知道怎么做吗?
  • 您不必使用freeimage.py。您必须在系统中安装freeimage.dll(我假设您使用Windows)并在imageio.imread/imageio.imwrite 中使用format='PNG-FI' - 就像链接中的示例一样。
  • 在插件代码中我看到你可以使用python代码imageio.plugins.freeimage.download()安装DLL。或在命令行imageio_download_bin freeimage
  • 在最后的代码中你有太多的变量,所以最后你写的是原始图像而不是调整大小的图像。

标签: python


【解决方案1】:

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

根据imageiofreeimage.py源代码中的信息安装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

编辑:您的最后一个代码具有更易读的变量名称 (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')

【讨论】:

  • @CogT 以后,您可以将代码放在反引号(`)中,这将使code。注释不支持换行符,但仍会使其更易于阅读。
  • 所以我觉得需要使用OpenCV的resize方法,但是好像不行,可以看我上面的尝试。
  • 在第一个示例中,我使用了您的图像,它保持 48 位。第二个示例仅适用于那些不想下载文件但仍想测试它的人。我看到的唯一问题是因为它的大小从大变小,所以它必须删除一些像素并且不太频繁的像素消失 - 而max() 给出的值更小。但图像仍然是 48 位。
  • 为什么你必须使用 OpenCV 的 resize ?如果您在 imwrite - Resized_Image 而不是 img_in_imageio 中使用正确的变量,则第二个版本可以正常工作。您写的是原始图像而不是调整大小的图像。
  • char _ 在函数名称开头_open() 表示它是私有函数,其他人不应该使用它。当然,如果你知道它是如何工作的,你就可以使用它。但是这个函数并不是为了直接使用它而创建的,它不能像你期望的那样工作。似乎它不需要文件名,但需要一些不同的东西。但是您可能找不到此功能的文档,并且可能需要做更多的工作才能使用它。
【解决方案2】:
# 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-12
    • 2011-02-04
    • 1970-01-01
    • 2013-12-12
    • 2017-03-11
    • 2017-09-01
    • 2019-12-10
    • 1970-01-01
    相关资源
    最近更新 更多