0
$\begingroup$

I have to duplicate a picture several times – and each time the duplicate shall have another name. I do this with mathematica 10 with the following easy code:

pic = Import["…\\TestFoto.jpg"]; (*Import of picture*)
namesList = {52543, 14745, 746364,…}; (*names*)
For[i = 1, i <= 3, i++, 
Export[ToString[namesList[[i]]] <> ".jpg", pic]];

This works quite well.

But: the exported picture is only 6 MB big although the original picture is about 50 MB big. Out of some reasons the exported pictures should be also 50 MB big.

What do I have to change in my code to achieve that the exported picture is as big as the imported?

I tried to find something in the Mathematica help – no result. I tried to find something here, but all I found was this and this which did not help me well. So, I hope some of you can help me! I would be happy about every hint and answer!

$\endgroup$
  • 1
    $\begingroup$ Have a look at the Options in the JPG documentation. "CompressionLevel" and ImageSize could be relevant. $\endgroup$ – Karsten 7. Oct 19 '15 at 16:10
  • 5
    $\begingroup$ JPEG is a lossy format. Every time you re-encode an image into JPEG, the data changes (and deteriorates) slightly. Is there any reason for using For instead of the more convenient, more readable, safer and faster Do? Do[Export[ToString[name] <> ".jpg", ...], {name, nameList}]. $\endgroup$ – Szabolcs Oct 19 '15 at 16:13
  • 2
    $\begingroup$ For more info see What Lossless File Formats Are & Why You Shouldn’t Convert Lossy to Lossless $\endgroup$ – Chris Degnen Oct 19 '15 at 16:15
6
$\begingroup$

If you want an exact copy, don't import and re-export the files. Just use file copying functionality instead:

namesList = {52543, 14745, 746364};
CopyFile["original.jpg", ToString[#] <> ".jpg"] & /@ namesList

In the code above you just need to replace the name and path of the original file you want to copy, and input the complete list of names. The copies will all be positioned in the same directory as the original in this case. You could of course extend the code to put the copies in different directories if interested.

This approach also has the advantage that it doesn't require a loop, using a more idiomatic Mathematica style.

$\endgroup$
  • $\begingroup$ Thank you MarcoB for your quick reply. I tried: original=Import["...\\TestFoto.jpg"]; namesList={52543,14745,746364}; CopyFile["original.jpg", ToString[#] <> ".jpg"] & /@ namesList This results in three times $Failed. When I use the pic itself instead of "original.jpg", I get many error messages. So, either there is a mistake in your code or I totally misunderstand the code. I would be glad when you can help again! $\endgroup$ – partial81 Oct 19 '15 at 17:08
  • 1
    $\begingroup$ @partial81 "original.jpg" in my code is the path to the file from which you want to generate copies. In your case, you should use the fully qualified path to your TestFoto.jpg, i.e. something like "c:\\some\\path\\to\\TestFoto.jpg" instead. You can check which is the current directory by evaluating Directory[]. You can use SetDirectory["c:\\path\\to\\some\\directory] to change the currently selected directory. $\endgroup$ – MarcoB Oct 19 '15 at 17:14
  • $\begingroup$ Great! Thanks for clarifying this so quickly! Your answer solves my problem well. And @Szabolcs comment is useful too because I agree with him: JPEG is a lossy format! Thanks for all the support! $\endgroup$ – partial81 Oct 19 '15 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.