EPS to PNG web application

In [1]:
from ipywidgets import interactive
In [2]:
import ipywidgets as widgets
from IPython.display import display, clear_output
In [3]:
from PIL import Image
In [4]:
from subprocess import run
In [5]:
w= widgets.IntSlider(
            value=600, 
            min=0, 
            max=1024,
            step=1
          )
h=widgets.IntSlider(
            value=150, 
            min=0, 
            max=1024,
            step=1
          )
In [6]:
import requests
import io
In [7]:
url = widgets.Text(
    value='https://tessarinseve.pythonanywhere.com/staticweb/fp.eps',
    placeholder='Figure URL',
    description='URL',
    disabled=False
)
In [8]:
saveas = widgets.Button(
    description='Save as png',
    disabled=False,
    button_style='',
    tooltip='Save figure as png',
    icon='save' 
)
In [9]:
def Update_figure(w,h,url):

    TARGET_BOUNDS = (1024, 1024)
    # adapted from 
    # https://stackoverflow.com/questions/45828514/how-to-convert-an-eps-file-into-a-png-in-python-3-6#45829598

    response = requests.get(url)
   
    pic = Image.open(io.BytesIO(response.content))
    pic.load(scale=10)

    if pic.mode in ('P', '1'):
        pic = pic.convert("RGB")
    new_size=(w,h)
    # Resize 
    pic = pic.resize(new_size, Image.ANTIALIAS)
    pic.save("tmp.png")
    return pic
    
In [10]:
pic=widgets.interact(Update_figure,w = w,h = h, url = url)
In [11]:
display(saveas)
In [12]:
def on_button_clicked(b):
 
    run("C:\\Users\\Seve\\bin\\saveas_dialog.exe")
In [13]:
saveas.on_click(on_button_clicked)
In [ ]: