An image is created with plotly and after Pillow is used to increase its quality, intending to save the image on the same path with the same name and format. However, it only works on the first time or when the file does not exists. Once the file is created, the image.save() method from Pillow and fig.write_image() don´t work again when the code is re-run.
I´ve tried to create a try catch but none error is presented. Besides that, closing the first file before trying to save the second time had beed tryed to re-start the pointers.
In addition, i had run the same code on other computer and it worked there. This is why i believe could also be related with the PC configuration but i don´t know how to proceed.
Thanks in advance, regards
Code
import plotly.graph_objs as go
from PIL import Image, ImageEnhance
original_file_path = 'Images/1.png'
# CREATE CHART
fig = go.Figure()
fig.add_trace(go.Scatter(x=x1,y=y1, mode = 'markers+text', text = t, name= 'Tickers'))
fig.add_trace(go.Scatter(x=x2, y=y2, name=' M Bonds'))
fig.update_xaxes( title_text='Duration',range = [0,3])
#fig.update_yaxes( title_text ='Yield to Maturity')
fig.update_layout(title= 'Mex Corps AAA Fixed Dur. 3 yrs- 5 yrs', xaxis_title= 'Duration',
yaxis_title = 'Yield to Maturity')
# save image created with PLOTLY
fig.write_image(original_file_path,width=1920, height=1080)
# read image with PILLOW and increase quality and save on the same format, path and name
try:
im = Image.open(original_file_path)
enhancer = ImageEnhance.Sharpness(im)
factor = 2
im_s_1 = enhancer.enhance(factor)
im_s_1.save(original_file_path, quality =95)
except IOError:
print("cannot create thumbnail for", original_file_path)