Home > Mobile >  In python have: 'from PIL import ImageTK, Image', but I also want to have 'from exif
In python have: 'from PIL import ImageTK, Image', but I also want to have 'from exif

Time:05-08

I am trying to create a project that selects an jpg file, shows the image in a small frame and lists the required exif values in a label underneath. I have to use PIL Image to pen the file and get 'an image' (I really don't know what the variable content is) I ten resize that file and then turn it into something that Tinter Label can display with label = pilpic

from PIL import ImageTk, Image
....
self.img1=Image.open(self.imagefile)
self.img2=self.img1.resize((300,300), Image.Resampling.NEAREST)
self.pilpic=ImageTk.PhotoImage(self.img2)

This works exactly as expected, until I Introduce the exif module.

from exif import Image
.....

The instruction are the same: self.exifdata=Image.open(self.imagefile)

But of course Image is already taken and while this command works, the self.exifdata file is of no use in other exif commands such as :

if self.exifdata.has_exif: 

fail totally, due I suspect that the Image.open is from OPIL and the contents of the variable self.exifdata are not compatible with exif commands

Now depending on which from module sport function I list first, it fails in various other ways, all totally consistent with one or the other of the code bits using the wrong Image.open

I would use some other module/s to either open the file for display in tinter Label or extract the exif data.

At this stage pillow / PIL is the only one I can get to do the display part, and be able to resize the image to fit the frame/lable.

I would use another module other than 'exif' to extract the exif data, but 'exif' is the only one I've found that is a) easy to use b) simple to get proper string data from and c).... gives GPS data without having to recode the universe and understand weird mathematics.

So, can anyone tell me how to import both these Image functions and have them live happily together?

CodePudding user response:

You can import it using an alias:

from exif import Image as exImage

And then you can use it in your code with the name exImage. It will not interfere with Image from PIL.

By the way, PIL.Image has the ._getexif() method, that could possibly do what you want without the need of exif.

img._getexif()
  • Related