Can't save Matplotlib images to a readable .tif format - python

I'm having trouble saving my Matplotlib plots to a readable .tiff format. The file itself does save, but when I double-click on it on the folder, I get the following error in Windows Photo Viewer:
"Windows Photo Viewer can't open this picture because either Photo Viewer doesn't support this file format, or you don't have the latest updates to Photo Viewer."
But attempted workarounds such as trying to load the file with Paint or Photo do not work. I get the feeling that the file is not saving correctly. (The image displays just fine and can be saved as a .gif just fine.) How do I resolve this?

Related

Exporting and downloading images from Azure Blob Storage

I am exporting some matplotlib plots from Databricks to Blob Storage. No problem here, just using:
plt.savefig('/dbfs/my_plot.png')
dbutils.fs.cp('dbfs:my_plot.jpg', blob_container)
The problem is that when I download and open the image locally, it is blank. I am not sure if I have to transform it or at some point specify the metadata, cause it appears as application/octet-stream in the Blob.
any ideas?
• When you are trying to download and open the image locally, you are still seeing the image as blank because you are using the command as posted by you for saving them in readable image format in the Azure blob storage, which is correct but when you are showing the plot or downloading it, you should use the command ‘plt.show('/dbfs/my_plot.png')’ before the below commands as shown: -
plt.show('/dbfs/my_plot.png')
plt.savefig('/dbfs/my_plot.png')
dbutils.fs.cp('dbfs:my_plot.jpg', blob_container)
• If you are not using the above command as said, you will only get the image as blank only. Also, if you don’t want undesirable white space around your image, you can remove that using the command as below as this white space may also be the reason behind your image showing as blank as the space might have consumed greater area than the actual plot.
plt.savefig('/dbfs/my_plot.png', bbox_inches='tight')
For more information and details regarding the solution for this problem, kindly refer to the below community thread: -
Save plot to image file instead of displaying it using Matplotlib

Open JPEG file with PIL and OpenCV fails

I have lots of images saved with no file extension.
I am trying to convert those images and do some work with PIL:
for img_path in list_imgs:
with Image.open(img_path) as img:
# more code
For some files this works ok but for others it shows PIL.UnidentifiedImageError. I tried to handle the exception by opening the file with OpenCV:
img = cv2.imread(img_path)
But I get the following error:
[ WARN:0#2.312] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\grfmt_tiff.cpp (462) cv::TiffDecoder::readData OpenCV TIFF: TIFFRGBAImageOK: Sorry, requested compression method is not configured
If I manually changed the file extension to .jpg for the specific file that files, I can open it using Windows Photos or any other app, even Microsoft Paint, so I know that the file is not damaged. However, both OpenCV and PIL have problems reading said file.
How can I open the image? Are there any other parameters that can be configured? Or can it be read directly into a NumPy array and then handled to PIL?

OSError: cannot identify image file (issue with certain images)

I have a script that is downloading and performing certain operations on files from a website. A couple of times a week some of the images will throw this error (edit for bump):
OSError: cannot identify image file '57343948435235236aede7dceb672559.png'
Even when manually running code as simple as the following, it produces the error:
imagefile = Image.open('57343948435235236aede7dceb672559.png')
Here are the important facts:
If I redownload the file using requests.get() I get the same error when attempting to open it.
If I redownload the file by saving it using a web browser, I instead get the error PIL.UnidentifiedImageError: cannot identify image file when attempting to open it using PIL
I can view the files (using Window Photo Viewer, a Linux image viewer, etc.) so they aren't completely corrupted.
If I open the image in Paint and save it under the same name, I can then open it using PIL.
Given all this, it appears to be an issue with specific image files hosted on the site; they are viewable in applications, but not openable using PIL. I'm assuming something is wrong with the byte structure or headers in the image, and saving overwrites those issues. Is there some library or automatic process I can use to 'fix' these images when the error occurs?
Below, I am including a minimal, reproducible example. PLEASE NOTE: THE EXAMPLE URL IN THIS CASE IS NSFW IN NATURE, AND THEREFORE HAS BEEN REDACTED. SEE MY COMMENT TO THIS POST FOR A PASTEBIN LINK TO THE FULL CODE.
from PIL import Image
url = 'SEE ABOVE NOTE'
file_name =(url).split('/')[-1:][0]
# Downloading the image file data.
file_data = get(url).content
# Write the data to a file.
with open(file_name, 'wb+') as file_object:
file_object.write(file_data)
# Opening the image with PIL
with Image.open(file_name) as image_file:
# "OSError: cannot identify image file" happens here
pass

Saved image using PIL but image doesn't open

I want my python script so save the python window as a .JPG, which it does. But when I try and open that .jpg with any application it tells me the file type is unsupported.
When I save my file as a .eps file it opens correctly but when I try and save as any other file type the file will not open.
win.postscript(file="image.jpg", colormode='color')
from PIL import Image as NewImage
img = NewImage.open("image.jpg")
Tkinter canvas object can only be saved as a postscript file, which is actually a postscript printer language text file.
Thus if you call:
win.postscript(file="image.jpg", colormode='color')
It will still write and create a postscript file, that is why it works when you rename the extension [or simply append] with .ps
Check this blog for a better implementation: https://www.daniweb.com/programming/software-development/code/216929/saving-a-tkinter-canvas-drawing-python

SVG Images not linked properly inside Master SVG

Through a python program, i am generating some SVG images. Each of this SVG Image has an external PNG Image attached to it.
Individually all these SVG images are good and look perfect.
But then i am creating a master SVG, which contains all these previously created SVG images (linked via image tag). When i view the master SVG in inkscape (on ubuntu), the PNG images are not displaying.
Can anyone suggest what is the problem?
NOTE: All Images (SVG and PNG) are linked by absolute paths on the system.
If you're using an SVG image via the <image> tag then it must be complete in a single file i.e. it can't link to an external png file.
You could convert the png file to a data URL and embed it in the SVG image file.

Categories