Click here to Skip to main content
15,891,850 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I want to create JPEG files which requires to be fixed size, e.g 500KB. But I have no idea how to achieve that. Who can teach me how to make it?? or like use some tools?

P.S How to get the smallest size file of JPEG? What is the minimum JPEG files?
Posted
Updated 27-Mar-11 17:44pm
v3
Comments
DaveAuld 25-Mar-11 5:27am    
Why do you need a fixed sized (file size) jpeg file. This is probably impossible to do due to the compression algo's used in jpeg.
Espen Harlinn 25-Mar-11 5:33am    
Possibly the most reasonable "Quick answer" possible :)
DaveAuld 25-Mar-11 5:34am    
in that case.........
Sergey Alexandrovich Kryukov 25-Mar-11 13:14pm    
Absolutely agree. I'm sure the requirement makes no sense.
--SA
Sergey Alexandrovich Kryukov 25-Mar-11 13:17pm    
I added my Answer...
--SA

Write the file, check its size and if less than 500K add the requisite number of zero bytes to the end. If greater than 500K you have a problem.
 
Share this answer
 
Comments
[no name] 25-Mar-11 6:52am    
Oops, it took me so long to write my answer that you posted the short version before I was done :)
Espen Harlinn 27-Mar-11 10:26am    
It may be possible to do something by defining custom metatdata for the jpeg image file, as OP specified the jpeg format.

I've not conducted any tests to check if this would work for jpeg files, but searching google for "jpeg padding" seems to indicate that this isn't a very good idea.
Richard MacCutchan 27-Mar-11 12:58pm    
I agree. However the OP asked how to do it so I offered a solution. If this means his Jpegs won't render correctly that is no concern of mine.
Espen Harlinn 27-Mar-11 16:03pm    
All right :)
Whatever you say, I'm sure that the mistake is the requirement to have 500KB and should be removed.
I already commented that this is impossible; your can pad smaller file but never guarantee smaller file.

In general case this is theoretically impossible.

Try to prove me wrong.

—SA
 
Share this answer
 
Comments
Richard MacCutchan 25-Mar-11 13:38pm    
To be fair the OP only suggested 500KB as an example. The requirement seems to be to take an image file and pad it out to some fixed size (which could be 500KB). And we know that this is possible.
Sergey Alexandrovich Kryukov 25-Mar-11 14:16pm    
I don't think I should be tolerant to the attitude: take any requirement, even stupid, and solve a problem. Even if it was perfect solution, I would vote against it. Padding makes no sense. The requirements should be changes. I know what I'm talking about. This is Dilbertization, real thing that plaques real company. We need programmers who can turn back wrong requirements. I welcome it even I was the one who created wrong requirements. Solving a problem based on a wrong requirement without critical look at it is a form of sabotage!
Sorry, I can't agree with you. This is not even about technical detail.
About technical aspect: how can you guarantee <=500KB right after compression? You can't.
--SA
Richard MacCutchan 25-Mar-11 15:51pm    
I agree with you completely. However, since the OP did not explain why he wants these fixed size files I see no reason not to help. Others had already pointed out that it seemed a silly thing to do.
Emilio Garavaglia 26-Mar-11 3:23am    
SA, the OP didn't specify why such a requirement. It is not even evident if such a file will be stored on a file system, or, for example, in a space of embedded memory where no "acess by name" is possible and data require to be somehow indexed. May be he just want to prepare a file to be moved up there.
At the point, inflating a short file into a file system looks a nonsense. But the real fact is that we have not enough information about what will be the use of the file and its final destination. So we cannot authoritatively judge if it make sense or not.
Sergey Alexandrovich Kryukov 26-Mar-11 14:32pm    
Maybe you're right about judgment, strictly speaking. In real life, I have certain confidence that this is no more than sloppiness. As inflating looks nonsense, as you point out yourself, and the guarantee to get the file below 500KB still does not exist (nobody argued it is possible; and this is the main argument against requirement: impossibility), assuming there is a good reason for the requirement is counter-productive. The fact OP didn't specify why such a requirement also contributed to sloppiness assumption.
--SA
This is probably impossible to do due to the compression algo's used in jpeg.
 
Share this answer
 
v2
Comments
Espen Harlinn 25-Mar-11 5:35am    
5ed! :)
Pravin Patil, Mumbai 25-Mar-11 5:51am    
I agree with you..
My 5.
Sergey Alexandrovich Kryukov 25-Mar-11 13:13pm    
Of course it is impossible in general case.
More importantly, it makes no sense.
--SA
Sergey Alexandrovich Kryukov 25-Mar-11 13:17pm    
I added my Answer, pleas see.
5 for yours.
--SA
I don't agree with the statements that it's not possible, you can safely pad an existing JPEG file which is under 500Kb with zeros to increase the length to 500Kb. It works for GIF files too.

#include <stdio.h>
#include <stdlib.h>

void main(void)
{
	FILE *fp = fopen("C:\\myimage.jpg","ab");
	
	fseek(fp,0,SEEK_END);
	long size = ftell(fp);

	char *buffer = (char*)calloc(500*1024-size,1);
	fwrite(buffer,500*1024-size,1,fp);

	fclose(fp);
}


Note that I consider this quite an ugly hack, but it's up to you to decide if you wish to use this.
 
Share this answer
 
v2
Comments
Richard MacCutchan 25-Mar-11 7:02am    
My answer was tongue in cheek, yours was far superior technically.
DaveAuld 25-Mar-11 9:58am    
Ok, i also agree, that its not impossible, you've proved that, but i fail to see why the hell you would want to pad out an image file just to fill space....must be a strange requirement to require a fixed size storage size.
Is there a risk that any apps reading this modified image file would fail because the image data length versus file length are different?
[no name] 25-Mar-11 10:15am    
This is a good question, I think it's certainly a risk. On the bright side, I tried a padded JPEG and GIF file with a few image reading programs (Adobe Photoshop 11.0, MSPaint XP, Paint Shop Pro 5.0, Firefox 3.6.15) and they all worked fine.
Sergey Alexandrovich Kryukov 25-Mar-11 13:13pm    
It is impossible. Is is only possible if compressed size is less then 500kb, but it is impossible to guarantee.
--SA
[no name] 25-Mar-11 13:21pm    
I mentioned specifically in my answer that it only works on files which are under 500Kb in size, and claiming something is not impossible is not the same as claiming something is always, under any circumstances, possible.
Personally I feel that offering a solution which works with a restriction, which is mentioned, is not deserving of a 1-vote.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900