1
$\begingroup$

I need to compress the TIFF figure "p5"(originally around 25 mb) in to less than 1 mb or as small as possible with constant resolution of 600 dpi.

ClearAll[a1, plot1, b1, plot2, f]
a1 = {{0.82, -109}, {0.98, -75}, {1.12, -57}, {1.17, -52}, {1.30,-43}, 
{1.37, -38}, {1.45, -34}, {1.54, -30}, {1.64, -26}, {1.76,-22.9}, {1.95, 
-18.6}, {2.06, -16.6}, {2.24, -13.8}, {2.47, -11.2},{2.74, -8.9}, {3.09, 
-6.8}, {3.53, -5}, {3.80, -4.17}, {4.12, -3.4},{4.59, -2.55}, {4.94, -2.06}, 
{5.26, -1.70}, {5.62, -1.37}, {5.83,-1.20}, {6.18, -0.96}, {6.51, -0.77}, 
{6.87, -0.58}};
plot1 = ListLinePlot[a1, PlotStyle -> Blue,PlotRange -> {{0, 7}, {15,-115}}, 
FrameLabel -> {{"ka",None}, {"Photon energy (eV)", "Ag"}}, Frame -> {{True, 
False}, {True, True}},ImagePadding -> 70, ImageSize -> Large, FrameStyle -> 
{{Blue, Black}, {Black, Black}},LabelStyle -> {14, GrayLevel[0]},FrameTicks 
-> {{Transpose[{#, Map[PaddedForm[#, {3, 1}] &, #]}]&[Range[-120, 10, 20]], 
Automatic}, {Transpose[{#, Map[PaddedForm[#, {2, 1}] &, #]}] &[Range[0, 7, 
1.0]], Automatic}}]
b1 = {{0.82, 12.33}, {0.98, 7.06}, {1.12, 4.81}, {1.17, 4.19}, {1.30, 3.10}, 
{1.37, 2.66}, {1.45, 2.22}, {1.54, 1.87}, {1.64, 1.53}, {1.76, 1.24}, {1.95, 
0.92}, {2.06, 0.78}, {2.24, 0.60}, {2.47, 0.45}, {2.74, 0.33}, {3.09, 0.23}, 
{3.53, 0.15}, {3.80, 0.12}, {4.12, 0.098}, {4.59, 0.071}, {4.94, 0.056}, 
{5.26, 0.047}, {5.62, 0.038}, {5.83, 0.034}, {6.18, 0.029}, {6.51, 0.024}, 
{6.87, 0.021}};
plot2 = ListLinePlot[b1, PlotRange -> {{0, 7}, {-2.0, 14}}, PlotStyle -> 
Red, ImagePadding -> 70, ImageSize -> Large, FrameLabel -> {{None, "kb"}, 
{"Photon energy (eV)", None}}, Frame -> {{False, True}, {False, False}}, 
FrameStyle -> {{Black, Red}, {Black, Black}}, LabelStyle -> {14, 
GrayLevel[0]}, FrameTicks -> {{Automatic, Transpose[{#, Map[PaddedForm[#, 
{3, 1}] &, #]}] &[Range[-2, 14, 2.0]]}, {Automatic, Automatic}}]
f = Overlay[{plot1, plot2}]  
Export["p5.tiff", Import[Export["p5.pdf", f, ImageSize -> 10,ImageResolution 
-> 600]],ImageResolution -> 600]
$\endgroup$
  • $\begingroup$ Why are you exporting as PDF, then reimporting, then exporting as TIFF? $\endgroup$ – MarcoB May 23 '18 at 14:45
  • $\begingroup$ @MarcoB, if i don't export as pdf and only export as a tiff file with image resolution 600 dpi. Then the export tiff file will loss all the tricks along the axis. I need to have a tiff files with 600 dpi $\endgroup$ – rabink May 23 '18 at 15:08
  • $\begingroup$ @MarcoB, the codes "ZIP" when used, the graph losses all the frame ticks. Could you help me for retaining the frameticks please. $\endgroup$ – rabink May 23 '18 at 15:15
  • $\begingroup$ Please see the edit to my answer to included export / and re-import. $\endgroup$ – MarcoB May 23 '18 at 15:18
  • $\begingroup$ @MarcoB, Thank you so much. It works perfectly. The top x-axis has more frame ticks than the bottom x-axis. Would you suggest me how to get the top x-axis frameticks (without numbers labels) similar to the bottom x-axis ticks . Thank you once again $\endgroup$ – rabink May 23 '18 at 16:15
2
$\begingroup$

Using your code to generate the f image, then specify an image encoding when you export as TIFF, e.g. ZIP:

Export["p5ZIP.tiff", f, ImageResolution -> 600, "ImageEncoding" -> "ZIP"]

The unencoded file is roughly 40 MB on my computer, whereas the ZIP-encoded one is 147 kB (!), at the same resolution (i.e. both images are 4800 x 3412 pixels).

The docs for the TIFF format indicate that you can use:

None        no compression                         (40  MB  for f)
"JPEG"      lossy JPEG compression                 (FAILED  for f)
"LZW"       lossless LZW compression               (160 kB  for f)
"PackBits"  PackBits run-length encoding method    (1.5 MB  for f)
"ZIP"       lossless ZIP image data compression    (143 kB  for f)

PackBits is not the most efficient compression method, but it has the advantage that any TIFF reader is required to handle that format, according to the standard (see e.g. https://en.wikipedia.org/wiki/TIFF).


To fix the problem that the axis ticks go missing when exporting to TIFF directly, which must be a bug, you already had found the workaround to go through the PDF format. I'd only suggest to do that using ExportString and ImportString, so you avoid generating the useless auxiliary files:

Export[
  "p5.tif",
  ImportString[ExportString[f, "PDF"], "PDF"],
  ImageResolution -> 600, "ImageEncoding" -> "ZIP"
]

The generated TIFF file (3458x2475 pixels) is small in file size (116 kB), has resolution of 600 dpi as reported by e.g. Windows Explorer, and the ticks are retained.

$\endgroup$
  • $\begingroup$ Thank you so much. It works perfectly. The top x-axis has more frame ticks than the bottom x-axis. Would you suggest me how to get the top x-axis frameticks (without numbers) similar to the bottom x-axis ticks. Thank you once again MarcoB $\endgroup$ – rabink May 23 '18 at 15:37

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.