1

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?

Brian
  • 25
  • 4

1 Answers1

4

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()
user2246849
  • 4,217
  • 1
  • 12
  • 16
  • Yes, thanks, that was the firstling I tried and it failed, but as you've mentioned it, I'll try it again. Here's what I have: from PIL import ImageTk, Image / from exif import Image as exImage. – Brian May 08 '22 at 07:49
  • @Brian that looks correct. Why are you saying you failed? You get the same errors? – user2246849 May 08 '22 at 07:52
  • Sorry, I didn't realise there was a time limit on comments, Basically, just having you say it was the way to do it, was enough for me to go back and do it again, very carefully. Just to be sure i did the 'as' thing to both 'Image' functions and now it works just fine. I'd give you a gold star if I could. Brian – Brian May 08 '22 at 08:19
  • As a secondary question, having used Image,open(filename) to open the file, do I need to close it? – Brian May 08 '22 at 08:22
  • @Brian If it helped, you can accept the answer! :) Regarding your second question: yes, you need to close it. You can also use a `with` block. See [this good answer](https://stackoverflow.com/a/31751501/2246849) for more info – user2246849 May 08 '22 at 08:24