gpt4 book ai didi

opencv - 在 OpenCV 中保存图像会增加图像大小

转载 作者:行者123 更新时间:2023-12-02 17:27:16 25 4
gpt4 key购买 nike

我已经问过这个问题我没有得到满意的答案,所以我做了更多的研究,我正在以更好的方式提出这个问题。我通过 OpenCV 读取图像imread方法。我使用 imwrite 保存了它方法。它将输出文件大小增加了一倍以上。

以下是我从 imagemagick 获得的图像的详细信息(我没有写下所有的细节,我附上了需要所有细节的图像):

输入图片:

  • Format: JPEG (Joint Photographic Experts Group JFIF format)
    • Mime type: image/jpeg
    • Class: DirectClass
    • Geometry: 1836x3264+0+0
    • Resolution: 72x72
    • Print size: 25.5x45.3333
    • Units: PixelsPerInch
    • Type: TrueColor
    • Endianess: Undefined
    • Colorspace: sRGB
    • Depth: 8-bit
    • Channel depth: red: 8-bit green: 8-bit blue: 8-bit
    • Channel statistics: Pixels: 5992704
    • Compression: JPEG
    • Quality: 75
    • Filesize: 267KB
    • Number pixels: 5.993M


输出图像:

  • Format: JPEG (Joint Photographic Experts Group JFIF format)
    • Mime type: image/jpeg
    • Class: DirectClass
    • Geometry: 1836x3264+0+0
    • Units: Undefined
    • Type: TrueColor
    • Endianess: Undefined
    • Colorspace: sRGB
    • Depth: 8-bit
    • Channel depth: red: 8-bit green: 8-bit blue: 8-bit
    • Channel statistics: Pixels: 5992704
    • Compression: JPEG
    • Quality: 95
    • Filesize: 611KB
    • Number pixels: 5.993M


如您所见,输入图像大小为 267KB,但输出图像大小结果为 611KB。现在我知道它的大小取决于编码器、压缩值、比特率等。我想知道无论如何我可以将图像保存为与输入图像完全相同的大小。

我还尝试使用 openCV 中的压缩像这样 cv2.imwrite('result.jpg', image, [int(cv2.IMWRITE_JPEG_QUALITY), 45])这里的图像质量为 45,但图像大小仍然大于输入图像(大约 300KB)。我不想低于 45,因为它会影响图像细节和数据丢失。

我尝试使用 scipy、matplotlib 等外部库。但无论如何,它们都会增加图像大小。

非常感谢有关输出图像增加的原因以及我们如何修复它的任何线索。谢谢。

这是一些供引用的代码
import cv2 #opencv library for image processing implementations 

def main():
im = cv2.imread('sample.jpg')
cv2.imwrite('result.jpg', im)
if(__name__=='__main__'):
main()

这是供引用的图像。
Sample Image

最佳答案

您的原始图像质量为 75。如果您未使用 OpenCV 定义保存质量,则默认为 95,因此您应该期望文件更大。

如果要将其设置为某个最大大小,可以使用 ImageMagick 像这样设置它,比如300kB:

convert sample.jpg -define jpeg:extent=300k a.jpg

这将为您提供质量为 83 的 299,810 字节文件。

如果您准备允许 400kB,则可以使用:
convert sample.jpg -define jpeg:extent=400k a.jpg

这将为您提供质量为 93 的 358,000 字节文件。

我在 Python 中写了一些类似的东西,可以让你使用二进制搜索以指定的大小保存 here .

关于opencv - 在 OpenCV 中保存图像会增加图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58375458/

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