How can I get HDR image dimensions through Python? - python

Is there a python module that will allow me to read an HDR image's dimensions?
Standard python PIL library can't read HDR images.
HDR references high dynamic range images. Photoshop can read this file format, but I have not been able to find any python module that can read this file format.
Update:
PIL Image.open returns "IOError: cannot identify image file"
imghdr returns None when reading an HDR image file format.

There is a python module called smc.freeimage that can read HDR images. Works well, but the trick is getting the module installed successfully. Make sure you install the cython module first... then try easy_install instead of pip install.
import smc.freeimage as fi
filename = "myimage.hdr"
img = fi.Image(filename)
print "width =",img.width
print "height =",img.height

Related

copy an PIL image to the macOS clipboard with Python

I am trying to copy a image created in PIL to the macOS clipboard.
I found 2 ways:
Using Pasteboard (pypi.org/project/pasteboard) but that one does not seem to work with Python 3.10 (I get a wheels error when trying to install it).
The other way is from here (Copy an image to MacOS clipboard using python) and uses the following code:
import subprocess
subprocess.run(["osascript", "-e", 'set the clipboard to (read (POSIX file
'image.jpg') as JPEG picture)'])
I tried playing around with this second approach but I cannot get it to work with an image created in Pillow. For example:
import subprocess
from PIL import Image
image = Image.open('img.png')
subprocess.run(["osascript", "-e", 'set the clipboard to (read (POSIX file '???') as JPEG picture)'])
In the original post the '???' was an image.jpg, so how could I insert the PIL data in there? I tried some variations but kept on getting the same error:
CFURLGetFSRef was passed an URL which has no scheme (the URL will not work with other CFURL routines)
22:67: 2023-01-12 13:53:42.227 osascript[1361:27923] CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme
execution error: Can’t make file ":image" into type file. (-1700)
This will copy the image as a data URL:
import base64
from io import BytesIO
from PIL import Image
image = Image.open('img.png')
buffered = BytesIO()
image.save(buffered, format="PNG")
base64_enc = base64.b64encode(buffered.getvalue())
print("data:image/png;base64,"+"".join(map(chr,base64_enc)))
which you then will be able to paste in your browser, and probably not in file manager though
if this didn't answer your question then you probably have to find a library that supports mac clipboard.

How to get size and resolution on image that already in bytes

i want to know if there some way to get the size and resolution of the image that already in bytes in python, i tried to search about it and it was using an external library.. did you know how to do it by external libraries or not.
thanks
you can use PIL (pillow).
to install it run "pip install pillow" on your terminal:
from PIL import Image
import io
image_data = ... # byte values of the image
image = Image.open(io.BytesIO(image_data))
print(f'{image.height}, {image.width}')

Using MemoryFS file-like object with PIL Image

I'm trying to open Image file from PyFileSystem MemoryFS using PIL, I tried to do that like this example and i got the error below:
from PIL import Image
from fs.memoryfs import MemoryFS
fs=MemoryFS()
fs.makedir("test")
out=fs.open("test/file.jpg",'wb')
out.write(someimagefile.read())
out.close()
in=fs.open("test/file.jpg",'rb')
im=Image.open(in) #error: cannot identify image file <IO wrapper for <MemoryFile in <MemoryFS> test/file.jpg>>
however if I don't use a directory (ex. out=fs.open("file.jpg",'wb')) It does work as expected.
What am I doing wrong?
Thanks for your help.
I already get an error using the following line:
from fs.memoryfs import MemoryFS
Probably I don't have that library installed. Do you need this library? You can just open the image without opening it with MemoryFS:
im = Image.open("test/file.jpg")
Side note: I wouldn't use in as variable name, since it is also a Python keyword.

Converting .jpg images to .png

I've looked around and read the docs, and found no way or solution, so I ask here. Is there any packages available to use Python to convert a JPG image to a PNG image?
You could always use the Python Image Library (PIL) for this purpose. There might be other packages/libraries too, but I've used this before to convert between formats.
This works with Python 2.7 under Windows (Python Imaging Library 1.1.7 for Python 2.7), I'm using it with 2.7.1 and 2.7.2
from PIL import Image
im = Image.open('Foto.jpg')
im.save('Foto.png')
Note your original question didn't mention the version of Python or the OS you are using. That may make a difference of course :)
Python Image Library: http://www.pythonware.com/products/pil/
From: http://effbot.org/imagingbook/image.htm
import Image
im = Image.open("file.png")
im.save("file.jpg", "JPEG")
save
im.save(outfile, options...)
im.save(outfile, format, options...)
Saves the image under the given filename. If format is omitted, the
format is determined from the filename extension, if possible. This
method returns None.
Keyword options can be used to provide additional instructions to the
writer. If a writer doesn't recognise an option, it is silently
ignored. The available options are described later in this handbook.
You can use a file object instead of a filename. In this case, you
must always specify the format. The file object must implement the
seek, tell, and write methods, and be opened in binary mode.
If the save fails, for some reason, the method will raise an exception
(usually an IOError exception). If this happens, the method may have
created the file, and may have written data to it. It's up to your
application to remove incomplete files, if necessary.
As I searched for a quick converter of files in a single directory, I wanted to share this short snippet that converts any file in the current directory into .png or whatever target you specify.
from PIL import Image
from os import listdir
from os.path import splitext
target_directory = '.'
target = '.png'
for file in listdir(target_directory):
filename, extension = splitext(file)
try:
if extension not in ['.py', target]:
im = Image.open(filename + extension)
im.save(filename + target)
except OSError:
print('Cannot convert %s' % file)
from glob import glob
import cv2
pngs = glob('./*.png')
for j in pngs:
img = cv2.imread(j)
cv2.imwrite(j[:-3] + 'jpg', img)
this url: https://gist.github.com/qingswu/1a58c9d66dfc0a6aaac45528bbe01b82
import cv2
image =cv2.imread("test_image.jpg", 1)
cv2.imwrite("test_image.png", image)
I don't use python myself, but try looking into:
http://www.pythonware.com/products/pil/
import Image
im = Image.open("infile.png")
im.save("outfile.jpg")
(taken from http://mail.python.org/pipermail/python-list/2001-April/700256.html )

Python Image Library (PIL) error with floating point tiff

I am writing a script that changes the resolution of a floating point 2K (2048x2048) tiff image to 1024x1024.
But I get the following error:
File "C:\Python26\lib\site-packages\PIL\Image.py", line 1916, in open
IOError: cannot identify image file
My Code:
import Image
im = Image.open( inPath )
im = im.resize( (1024, 1024) , Image.ANTIALIAS )
im.save( outPath )
Any Ideas?
Download My Image From This Link
Also I'm using pil 1.1.6. The pil install is x64 same as the python install (2.6.6)
Try one of these two:
open the file in binary mode,
give the full path to the file.
HTH!
EDIT after testing the OP's image:
It definitively seems like is the image having some problem. I'm on GNU/Linux and couldn't find a single program being able to handle it. Among the most informative about what the problem is have been GIMP:
and ImageMagik:
display: roadnew_disp27-dm_u0_v0_hr.tif: invalid TIFF directory; tags are not sorted in ascending order. `TIFFReadDirectory' # warning/tiff.c/TIFFWarnings/703.
display: roadnew_disp27-dm_u0_v0_hr.tif: unknown field with tag 18 (0x12) encountered. `TIFFReadDirectory' # warning/tiff.c/TIFFWarnings/703.
I did not try it myself, but googling for "python tiff" returned the pylibtiff library, which - being specifically designed for TIFF files, it might perhaps offer some more power in processing this particular ones...
HTH!

Categories