Questions tagged [imghdr]

imghdr is a Python module for determining the type of images contained in a file or byte stream.

18 questions
152
votes
11 answers

How to check if a file is a valid image file?

I am currently using PIL. from PIL import Image try: im=Image.open(filename) # do stuff except IOError: # filename not an image file However, while this sufficiently covers most cases, some image files like, xcf, svg and psd are not…
Sujoy
  • 8,041
  • 3
  • 30
  • 36
6
votes
3 answers

imghdr / python - Can't detec type of some images (image extension)

I'm downloading a lot of images from imgur.com with a Python script and since I have all the links in the format http://imgur.com/{id} I have to force download them by replacing the original url with http://i.imgur.com/{id}.gif, then saving all the…
Hyperion
  • 2,515
  • 11
  • 37
  • 59
6
votes
1 answer

python - How to use StringIO with imghdr to determine if valid image

How to use StringIO with imghdr to determine if valid image I loaded a image into image_file = StringIO(open("test.gif",'rb').read()) imghdr.what(image_file.getvalue()) print imghdr.what(image_file.getvalue()) File…
Tampa
  • 75,446
  • 119
  • 278
  • 425
4
votes
3 answers

identifying the format of files

In linux, we have a utility called “file”, which helps us to determine the identification of a file. Is there any python module that can do the same job? I don't prefer to use subprocess.Popen(['file', 'blah.blah']), because it is platform…
prgbenz
  • 1,129
  • 4
  • 13
  • 27
3
votes
1 answer

Django content type of an image file

I want to check a file type before uploading it by: content = self.cleaned_data['picture'] content_type = content.content_type.split('/')[0] When I upload the picture I get an error: 'NoneType' object has no attribute 'content_type' What can be…
hln
  • 1,071
  • 4
  • 21
  • 37
2
votes
1 answer

Determine image in memory

I want to determine if a buffer I have (downloaded it) is an image file, without saving it to the disk. I looked it up and found out that: imghdr can do it only for files. python-magic can give me the file type, but than I need to map it to image…
2
votes
1 answer

why do python imghdr test functions take the file as an argument?

I was looking through the source code for the imghdr module, which is part of the python standard library (I use 2.7). The structure is pretty simple—a what function that iterates over a list of functions with names like test_filetype, and if the…
jdmcbr
  • 5,964
  • 6
  • 28
  • 38
2
votes
3 answers

Open a picture via URL and detecting if it is png, jpg or gif in Python?

I want to determine if a puush.me image (Links are no HTML, just the image) is png, jpg or gif. Is there a way to do that n python? Urllib does not seem to open images and detecting them.
user3760874
  • 75
  • 1
  • 5
1
vote
1 answer

AttributeError: 'bytes' object has no attribute 'tell'

I am using email.message and smtplib to send emails using python. When an image is sent as an attachment, it raises this error: AttributeError: 'bytes' object has no attribute 'tell' Here is the code for the image attachment: if filetype.lower() in…
Coder Station
  • 53
  • 2
  • 8
1
vote
1 answer

How to extract base64 extension without using lib imghdr

I have a view that works perfectly for receiving base64 images. My problem is that in some rare cases it doesn't recognize the sent jpg image. It looks like None. Looking on the internet I saw that the problem is the lib imghdr. I tried to use OS…
Joey Fran
  • 598
  • 2
  • 24
1
vote
0 answers

imghdr.what() does not work on a BytesIO buffer

I am working on a feature that allows users to upload images to a Python-Flask web app. The uploaded image is converted into a BytesIO buffer and is never saved to disk. I want to use imghdr.what() to determine the image type (png, jpg, etc.) and…
ByteByByte
  • 303
  • 1
  • 2
  • 11
1
vote
1 answer

PIL or imghdr for image validation

I'm working on a website that will handle image uploads so I need to verify for valid images, and I have two options, I either use PIL(Pillow) or the imghdr module. My question is, which one should I use? Since PIL is not being mantained will imghdr…
0
votes
0 answers

How to save HDR file with numpy array?

I try to save HDR file with numpy array as follows. numpy array like this (dtype:np.float32) # hdr_file_data [[[0.32543945, 0.38671875, 0.44213867], [0.30029297, 0.34204102, 0.3737793 ], [0.2697754, 0.28515625, 0.28295898], [0.2388916, …
陆一凡
  • 137
  • 1
  • 11
0
votes
0 answers

Imghdr module in Python 3.9.5 misidentifying .png file

When I call imghdr.what(sampleImage.png) it recognizes it as a jpeg for some reason. For example if I use print(imghdr.what(sampleImage.png)) it will output the string "jpeg". I checked the image file and it is displayed as having the expected PNG…
0
votes
0 answers

python: determine file type by its content (not only by its magic)

So I'm trying to verify that files I'm getting (uploaded by the user) are indeed images, and valid. I'm running ClamAV (using python's clamd package) but it doesn't give back the actual file content. I'm using python's magic package in order to…
Roie Labes
  • 55
  • 5
1
2