1

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)
Hector
  • 31
  • 3
  • Do you maybe have the sticky bit set on your directory `Images`? That would mean only the user who created a file can alter it. Maybe compare permissions between the system where it works and where it doesn't. Or create a brand new directory for your images and see if it works in there. – Mark Setchell Dec 16 '21 at 21:43
  • One way to solve this is to not save the file the first time (remove`fig.write_image(original_file_path,width=1920, height=1080)`). Instead extract the image data (from the ***fig*** variable) and apply sharpening filter to it. Then save the file. This overcomes any restrictions on **file overwrite**. – Vasu Deo.S Dec 21 '21 at 16:34

0 Answers0