旋转图像会增加尺寸吗? [英] Rotating image increases its size?

查看:372
本文介绍了旋转图像会增加尺寸吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些宽度大于高度的图像绕左上角旋转90度.我已经写了这个:

I am trying to rotate some images whose width is more than height about the left-top corner, by 90 degrees. I have written this :

from PIL import Image
import sys, csv, os, traceback, glob
import shutil, math

src_im = Image.open("Test.png")
print src_im.size[0] , ',' , src_im.size[1]

src_im = src_im.transpose(Image.ROTATE_90) 
src_im = src_im.transpose(Image.FLIP_LEFT_RIGHT) 
src_im = src_im.transpose(Image.FLIP_TOP_BOTTOM) 

src_im.save("TestResult.png")
print src_im.size[0] , ',' , src_im.size[1]

生成的输出符合我的预期,但是大小发生了巨大变化.有什么想法可能会出错吗?

Output generated is as I expect, but there is a huge change in size. Any ideas where I might be going wrong ?

它存储了相同的像素信息,只是旋转了,为什么图像尺寸应该改变?

Its the same pixel information being stored, just rotated, why should there be a change in the image size ?

例如 (936 x 312)155KB

eg. (936 x 312) 155KB

(312 x 936)342KB

(312 x 936) 342KB

好,所以我尝试使用Windows的内置图像查看器旋转图像,并且这种情况也有所增加.因此,它本身并不是真正针对Python的.有关压缩的更多信息.仍然不清楚为什么旋转时压缩性较低?我正在尝试的所有图像都在发生这种情况,而不是特定的图像.相应地更新标签.

Ok, so I tried rotating the images with the inbuilt image viewer of windows, and there is an increase in that case as well. So its not really specific to Python per se. More about compression. Am still not clear why would it be less compressible on rotation ? And this is happening for all images I am trying, not this particular one. Updating the tags accordingly.

推荐答案

PNG通过过滤"每行来压缩图像,尝试根据过去"邻居(上一行和/或或列),然后使用ZLIB(Deflate)压缩预测误差. 这里的问题似乎是这样的:垂直图像几乎具有垂直条纹;沿行扫描时,它具有相当可预测的中等范围图案(大约8种相似的颜色,随后是一小段浅色).这表明,虽然短程预测不会非常成功,但预测误差将获得高度重复的模式,应该相对容易压缩.旋转图像时不会发生这种情况.

PNG compress the image by "filtering" each line, trying to predict the values for each pixel as a function of the "past" neighbours (previous row and/or column), and then compressing the prediction error by using ZLIB (Deflate). The issue here seems to be this: the vertical image has almost vertical stripes; when scanned along the rows, it has a fairly predictable medium-range pattern (about 8 similar colors followed by a short burst of lighter colour). This suggest that, while the short-range prediction will not be very successful, the prediction error will get a highly repetitive pattern, that should be relatively easy to compress. This does not happen when the image is rotated.

我验证了不同的水平/垂直尺寸不是问题:我通过重复原始图像9次来制作一个更大的正方形(900x900).具有准垂直条纹的PNG图像的尺寸大约是另一幅图像的一半.

I verified that the different horizontal/vertical sizes were not the problem: I made a bigger square (900x900) by repeating the original image 9 times. The PNG image with quasi vertical stripes has roughly half the size than the other one.

另一个证实以上内容的实验:将两个图像都保存为灰度BMP(这是一种未压缩的格式,它沿行每像素存储一个字节).您将获得两个293.110字节的图像.使用标准的ZIP压缩器(与ZLIB的deflate相同的系列)压缩它们两者.同样,垂直图像的大小约为另一幅图像的一半.

Another experiment that confirms the above: save both images as grayscale BMP (this is an uncompressed format, it stores one byte per pixel, along the rows). You get two images of 293.110 bytes. Compress both of them with a standard ZIP compressor (same family as ZLIB's deflate). The vertical image, again, gets about half the size than the other one.

这篇关于旋转图像会增加尺寸吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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