Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 调整48位PNG的大小,保留其48位,而不将其放入24位文件_Python - Fatal编程技术网

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

Python 调整48位PNG的大小,保留其48位,而不将其放入24位文件,python,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 sci

我正在尝试将以下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:\.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'
这复制了文件,但没有调整大小


我希望uint48的文件格式能够得到维护,但是输出文件似乎是24位的

您可以使用
imageio.imread
/
imageoio.imwrite
中的
format='PNG-FI'
使用
freeimage

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

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

  • 使用python代码

    import imageio
    
    imageio.plugins.freeimage.download()
    
如果您直接从中安装库(
.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
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

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


如果您有
RGBA
,则它将使用64位颜色

来自
imageio
问题的示例:

输出:

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
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')

在imageio发行版中,您可以看到如何与FreeImage插件一起使用它来创建甚至64位png(RGBA)。它在Linux.Furas上对我有效,我在调查之前的一个挑战时读过这篇文章。我在这里看到了这段代码:-但是我不清楚如何使用freeimage.py读取图像文件,你知道怎么做吗?你不必使用
freeimage.py
。您必须在系统中安装(我假设您使用Windows)并在
imageio.imread
/
imageio.imwrite
-就像link.in插件代码中的示例一样,我看到您可以使用python代码
imageio.plugins.freeimage.download()安装DLL。或者在命令行
imageio\u download\u bin freeimage
中,上一个代码中的变量太多,所以最后您编写的是原始图像,而不是大小调整后的图像。@将来,您可以将代码放在反勾(`)中,这将使
code
。注释不支持换行符,但这仍然使其更易于阅读。因此,我认为我需要使用OpenCV resize方法,但它似乎不起作用,您可以看到我在上面的尝试。在第一个示例中,我使用了您的图像,它保留了48位。第二个例子只适用于那些不想下载文件但仍想测试它的人。我看到的唯一问题是,它的大小从大变小,因此它必须删除一些像素,而不太频繁的像素会消失,
max()
会给出更小的值。但图像仍然是48位。为什么必须使用“从OpenCV调整大小”?如果在
imwrite
-
调整大小的\u图像中使用正确的变量,而不是在\u图像IO中使用
img\u,则第二个版本可以正常工作。您编写了原始图像,而不是调整了大小的图像。在函数名
\u open()开头的char
\u
表示它是私有函数,其他人不应该使用它。当然,如果你知道它是如何工作的,你可以使用它。但是这个函数并不是为了直接使用它而创建的,它也不能像您期望的那样工作。它似乎不需要文件名,但需要一些不同的东西。但是您可能找不到该函数的文档,使用它可能需要更多的工作。
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')
# 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