3dmol / 3Dmol.js

WebGL accelerated JavaScript molecular graphics library
https://3dmol.org/
Other
812 stars 194 forks source link

high quality figure #269

Closed eguarnera closed 7 years ago

eguarnera commented 7 years ago

Hi there,

Is it possible within py3dmol to save to high resolution figures, such as pdf, eps, tifff…

Best, Enrico

dkoes commented 7 years ago

You can do this programatically use the native methods of the canvas object. The resulting image will be the same size as the canvas. For example, the following code make right-clicking on the 3Dmol viewer return an image:

          $('canvas').bind('contextmenu',function(e) {
              var dt = e.currentTarget.toDataURL('image/png');
              window.location = dt;
          });

Would you prefer a simpler interface? What are your suggestions?

eguarnera commented 7 years ago

How can I use your code in py3dmol? This is the code that I use to visualize a local pdb in ipython, it would be great to have a command to simply save the image on screen...

cut=1 pdbdata=open('outfile.pdb', 'r').read() view=py3Dmol.view() view.addModel(pdbdata,'pdb') view.zoomTo() view.setBackgroundColor('white') view.addSurface(py3Dmol.SAS,{'opacity':1,'colorscheme':{'prop':'b','gradient':'rwb','min':-cut,'max':cut}}) view

dkoes commented 7 years ago

Integration with py3Dmol requires modifying that package. Which I have (and published the changes on PyPI). view.png() will now embed a png of the current state of the viewer in the notebook. However, I suspect that this is not what you want since 1) the image data is not available in python (e.g. you can't save the image to a file), 2) if you use this call non-interactively, you get a white image since it snapshots the viewer before anything is rendered, and 3) since a save does a re-render, saved notebook have white images.

Basically, the only thing it's good for is you can manually extract the image from the notebook (e.g. right-click or drag). Since the image is the same size of the viewer, this doesn't give you anything higher resolution than a screenshot though (but might be a bit more convenient).

Can you describe your desired use case more?

eguarnera commented 7 years ago

Hi,

thanks for updating py3Dmol to 0.5.3. I tried the command view.png() in this way

view.show() figfile=open('fig.png','w') view.png(figfile)

which gives the error

---> 17 view.png(figfile) TypeError: png() takes exactly 1 argument (2 given)

I'm using py3Dmol interactively with ipython to visualize protein surfaces colored according to a certain model. So I'm primarily using py3Dmol for the visual inspection of my model. However, since the rendered images look very nice, I was considering to use them for the figures in the paper I'm writing. I tried to use the right-click to save the image but it simply doesn't work, that is right-click over the image doesn't open any menu.

dkoes commented 7 years ago

The png method doesn't take any arguments. Like I said previously, we cannot import the image data into python and so can't save files directly.

If you are right-clicking on the 3D viewer, you won't get a menu. If you right-click on the image generated by view.png() you will get a menu.

eguarnera commented 7 years ago

I see. I followed your instructions, I'm attaching two screenshots, not using and using view.png() respectively. When I use view.png() I get the error shown in figure.

3dmol1 3dmol2

eguarnera commented 7 years ago

What am I doing wrong, any suggestion? Thanks for you time!

Best, Enrico

dkoes commented 7 years ago

Perhaps if you uploaded the notebook?

eguarnera commented 7 years ago

matplotlib inline %config InlineBackend.figure_format='retina'

from pylab import * import py3Dmol

cut=1 pdbdata=open('outfilepdb.txt', 'r').read() view=py3Dmol.view() view.addModel(pdbdata,'pdb') view.zoomTo() view.setBackgroundColor('white') view.addSurface(py3Dmol.SAS,{'opacity':1,'colorscheme':{'prop':'b','gradient':'rwb','min':-cut,'max':cut}}) view.show()

view.png()

eguarnera commented 7 years ago

below the pdb outfilepdb.txt

eguarnera commented 7 years ago

outfilepdb.txt

dkoes commented 7 years ago

No, I meant upload the actual .ipynb file. Your code works fine for me. I want to see what is different in your environment.

eguarnera commented 7 years ago

test3Dmol.ipynb.zip

dkoes commented 7 years ago

I can't reproduce the problem or see any reason for it. I've tried with Safari, Firefox and Chrome. I'm using ipython 5.1 with notebook server 4.2.3. What is your setup?

eguarnera commented 7 years ago

I have ipython 5.1.0 and notebook 4.3.1… I don't understand

eguarnera commented 7 years ago

notebook running on Firefox 50.1

dkoes commented 7 years ago

I upgraded to 4.3.1 and Firefox 51.0.1 - still no problem. Have you try restarting your browser, restarting the ipython kernel, and/or creating a new notebook?

eguarnera commented 7 years ago

Ok, giving the command view.png() in a new cell works! Giving the command right after view.show() in the same cell gives me the error. But fine, thanks!

dkoes commented 7 years ago

Yes, the viewer has to be instantiated for the command to work.

eguarnera commented 7 years ago

Sorry, had misunderstood how it worked. Thanks for your patience and time!

miRastic commented 1 year ago

The below sample code worked for me:

      $('canvas').bind('contextmenu',function(e) {
          var dt = e.currentTarget.toDataURL('image/png');
          var a = document.createElement("a"); //Create <a>
          a.href = dt;
          a.download = "Image.png"; //File name Here
          a.click(); //Downloaded file
          a.remove();
      });