读取.JPG图像并保存而不更改文件大小 [英] Reading a .JPG Image and Saving it without file size change

查看:130
本文介绍了读取.JPG图像并保存而不更改文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个读取.jpg图片的python代码,更改其某些RBG组件并再次保存,而无需更改图片大小.

I want to write a python code that reads a .jpg picture, alter some of its RBG components and save it again, without changing the picture size.

我尝试使用OpenCV和PyGame加载图片,但是,当我尝试使用三个不同功能的简单加载/保存代码时,生成的图像在尺寸上要大于初始图像.这是我使用的代码.

I tried to load the picture using OpenCV and PyGame, however, when I tried a simple Load/Save code, using three different functions, the resulting images is greater in size than the initial image. This is the code I used.

>>> import cv, pygame # Importing OpenCV & PyGame libraries.
>>> image_opencv = cv.LoadImage('lena.jpg')
>>> image_opencv_matrix = cv.LoadImageM('lena.jpg')
>>> image_pygame = pygame.image.load('lena.jpg')
>>> cv.SaveImage('lena_opencv.jpg', image_opencv)
>>> cv.SaveImage('lena_opencv_matrix.jpg', image_opencv_matrix)
>>> pygame.image.save(image_pygame, 'lena_pygame.jpg')

原始大小为48.3K,结果为75.5K,75.5K,49.9K.

The original size was 48.3K, and the resulting are 75.5K, 75.5K, 49.9K.

所以,我不确定是否丢失了使图片原始尺寸发生变化的内容,尽管我是否只进行了加载/保存?

So, I'm not sure I'm missing something that makes the picture original size changes, although I only made a Load/Save, or not?

还有比OpenCV或PyGame更好的库吗?!

And is there a better library to use rather than OpenCV or PyGame ?!

推荐答案

JPEG是一种有损图像格式.当您打开并保存一个图像时,您将再次对整个图像进行编码.您可以调整质量设置以接近原始文件的大小,但是无论如何都会损失一些图像质量.没有通用的方法可以知道原始质量设置是什么,但是如果文件大小很重要,您可以猜测一下直到关闭文件.

JPEG is a lossy image format. When you open and save one, you’re encoding the entire image again. You can adjust the quality settings to approximate the original file size, but you’re going to lose some image quality regardless. There’s no general way to know what the original quality setting was, but if the file size is important, you could guess until you get it close.

这篇关于读取.JPG图像并保存而不更改文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆