2
$\begingroup$

Observe the following code:

plt = ComplexPlot[Zeta[z], {z, 0 - 160*I, 3 + 160*I}, 
  ColorFunction -> {Hue[#8 + 0.5] &, None}, Frame -> None]
Export["out.png", plt, ImageResolution -> 2400];

It exports the following image:

https://i.ibb.co/mNMBFBv/out4.png

As you can see, there is a white background around the edges:

enter image description here

I'm wanting to use this plot as a texture for a 3D model. I need the white edges trimmed off. How can I specify not to include the white border in the export?

Another problem is that if I increase the resolution higher, to 4800, the entire exported image becomes solid white. I would like a high resolution export, but I'm not sure if that's possible since the height of the image becomes so extremely tall.

So two questions:

  1. How do I remove the white border?
  2. How do I export at resolution 4800 or higher?
$\endgroup$
8
  • $\begingroup$ Have you tried ImageSize -> 2400 option in ComplexPlot? $\endgroup$
    – Somos
    May 5 at 20:36
  • $\begingroup$ @Somos, that gives me a distorted image. It isn't the correct aspect ratio when I use that option. See it here: i.ibb.co/Z6CCkGJ/out41.png $\endgroup$
    – jippyjoe4
    May 5 at 21:25
  • $\begingroup$ Then use AspectRatio option to compensate. $\endgroup$
    – Somos
    May 5 at 21:35
  • $\begingroup$ @Somos, please provide code that solves the issue. I don't know exactly how far off the ratio is. $\endgroup$
    – jippyjoe4
    May 5 at 21:47
  • $\begingroup$ Alternative is to use ImageSize -> {width,height} explicit dimensions. $\endgroup$
    – Somos
    May 5 at 22:15
1
$\begingroup$

Let me extend my comments to an answer.

How do I remove the white border?

This is the simplest part. You just need to add PlotRangePadding -> None. Since you want to use this plot as a texture for a 3D model, you probably need to add BoundaryStyle -> None, too.

I didn't have a specific target resolution, just big enough to not be blurry.

If you care about the resolution, then you should control it from the very beginning, because the default setting of ComplexPlot isn't capable of generating a high-resolution image in the range {z, 0 - 160*I, 3 + 160*I}:

Show[plt, PlotRange -> {{0, 3}, {-3, 3}}]

Mathematica graphics

Why? Because rasterization already happens in ComplexPlot. (If you check the InputForm of the plot generated by ComplexPlot by pressing Ctrl+Shift+i, You'll find a Image[…] therein. ) To guarentee a high enough resolution, the RasterSize option must be set both in ComplexPlot and Export. (RasterSize gives a more straightforward control for the image size in this case, see this post for more info. )

To sum up:

tst = ComplexPlot[Zeta[z], {z, 0 - 160 I, 3 + 160 I}, AspectRatio -> Automatic, 
   ColorFunction -> {Hue[#8 + 0.5] &, None}, RasterSize -> {3, 320} 30, 
   PlotRangePadding -> None, BoundaryStyle -> None, Frame -> None];

Export["out.png", tst, RasterSize -> 90]

enter image description here

How do I export at resolution 4800 or higher?

Please make sure you really need such a demanding resolution. (RasterSize -> {3, 320} 30 already freeze my laptop with 8GB memory. ) As shown above, adjusting ImageResolution in Export won't sharpen the image in your case.

$\endgroup$
0
$\begingroup$

UPDATE: As per comment, applying ImageCrop to the original png file will remove the white border and avoid a blurred image.

plt = ComplexPlot[Zeta[z], {z, 0 - 160*I, 3 + 160*I}, 
   ColorFunction -> {Hue[#8 + 0.5] &, None}, Frame -> None];
Export["out.png", plt, ImageResolution -> 2400];
Export["outCrop.png", ImageCrop@Import["out.png"]];

Original answer:

ImageCrop removes the white border. This image size is in the correct proportions and allows zooming to a reasonable size without pixelation. Of course it can be modified while maintaining the ratio.

plt = ComplexPlot[Zeta[z], {z, 0 - 160*I, 3 + 160*I}, 
    ColorFunction -> {Hue[#8 + 0.5] &, None}, Frame -> None]
Export["out.png", ImageCrop@plt, ImageSize -> {128, 13248}];
$\endgroup$
3
  • $\begingroup$ the crop seems to be applied at the lowest resolution, and then imagesize scales it up afterwards. I get a blurry mess that is useless. Did this work for you? $\endgroup$
    – jippyjoe4
    May 6 at 14:56
  • $\begingroup$ Sorry, I probably misunderstood what the goal was. Applying ImageCrop to your original png file will give a sharp result. I will add this to the answer. $\endgroup$ May 6 at 17:26
  • $\begingroup$ Thank you, this fixes the cropping issue. However, I am still unable to export the plot correctly if Increase the image resolution to 4800 (the whole thing becomes solid white). Do you know why this is? $\endgroup$
    – jippyjoe4
    May 6 at 21:45
0
$\begingroup$

I gave up and just decided to do it in Python using an export from Mathematica:

Mathematica code:

resolutionMultiplier = 4.0;
step = 150;
argresult = 
  ParallelTable[{x + 0.0, 
    Round[Arg[Zeta[x + I*y] + 0.0 + 0.0], 0.0001], y + 0.0}, {x, 
    0, 3, step/resolutionMultiplier}, {y, -160, 160, 
    step/resolutionMultiplier}];
Export["zeta_arg_out.txt", argresult]; 

Python code:

import json
import cv2
import colorsys
import math
import numpy as np

with open("zeta_arg_out.txt") as f:
  data = f.read()
  data = data.replace("{","[").replace("}","]").replace("\n",",")
  data = data.replace(".,",",").replace(".]","]")
  data = f'[{data}]'

b = json.loads(data)

xlen = len(b)
ylen = len(b[0])

img = np.array([[[0,0,0] for xx in range(xlen)] for yy in range(ylen)], dtype = np.uint8)

for xx in range(len(b)):
  print(f'{xx=}')
  for yy in range(len(b[xx])):
    val = b[xx][yy][1]/math.pi
    val = (val + 1)/2.0
    val = (val + 0.5) % 1.0
    color = colorsys.hsv_to_rgb(val,1.0,1.0)
    nc = [math.floor(ii*255) for ii in reversed(color)] # cv2 uses bgr, not rgb!
    img[yy][xx] = nc

cv2.imwrite("colormap.png", img)

Hopefully somebody else can post a proper Mathematica fix.

$\endgroup$

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.