I did a bit of googling, and it seems the easiest way out is to simply check the first few bytes of the file for magic numbers. So here's a bit of Python code for checking for whether the file data belongs to a JPEG, PNG or TIFF image:
def is_jpg(data):If the file is already on disk, you can grab the first few bytes with
return data[:2] == '\xff\xd8'
def is_png(data):
return data[:8] == '\x89PNG\x0d\x0a\x1a\x0a'
def is_tiff(data):
return data[:4] == 'MM\x00\x2a' or data[:4] == 'II\x2a\x00'
f = open("somefile.jpg", 'r')Of course this won't test that the whole file is valid. But it's easier to do that afterwards with an image library once the extension is correct.
data = f.read(11)
if is_jpeg(data):
ext = ".jpg"
elif is_png(data):
ext = ".jpg"
The magic numbers are documented in the specifications for the formats. You can also find some help for other formats in the source code of the
file
command on Unix systems.Update: I'm liking this so much that I ended up putting it in a separate file and making a convenience function for getting an extension like '.jpg'. Grab the Python file here. I also added support for GIF. Here's another easy reference for magic file numbers.
Second update: I've updated the code, there was a bug detecting JPEGs from certain digital cameras that put Exif data in the first segment. Suffice to check the two first bytes of the JPEG, then the problem does not occur.
i guess one shouldn't reinvent the wheel... why not use the "file" command for this?
ReplyDelete> file IMG_0019.JPG
IMG_0019.JPG: JPEG image data, EXIF standard
os.system("...") should do the work
Well, file is neat and I did consider it, but it won't work unless you already have the file on disk (despite my example, I would like to get the name right before I write it) and it's harder to reason about (can file crash? what can go wrong when you use os.system?).
ReplyDeleteAlso, using os.system in a web app is a bit scary, you have to double check that no user entered data can ever end up in the command, at least not unescaped.
So that's why. :)
I recently found out you can feed data in chunks to the Python Imaging Library, so another possibility is to feed it one chunk and see what happens.
Thank you, Ole, for a very useful piece of code! It'll be in the next version of sqlpython to allow browsing of image BLOBs straight from the database.
ReplyDeleteAs for UNIX's `file`... that's nice, but I don't believe it exists on windows, so no use for a cross-platform app!
Cool! Glad you can use it. :)
ReplyDeleteJust what I was looking for- thanks for sharing.
ReplyDeleteYou don't actually need to write the file to disk, you can just pass it thru `| file -`
ReplyDeleteToni: that's an interesting idea. Here's a little snippet for doing it in Python:
ReplyDeleteimport subprocess
f = open("test.jpg", 'r')
data = f.read(11)
p = subprocess.Popen(["/usr/bin/file", "-", "--mime-type", "-b"], stdin=subprocess.PIPE)
print p.communicate(data)[0]
# outputs "image/jpeg"
Translations for Ruby:
ReplyDeletedef jpeg?(data)
return data[0,2]=="\xff\xd8"
end
To read a file from disk:
f = File.open(filename,'rb' # read binary
data = f.read(11)
f.close
if jpg?(data)
ext = ".jpg"
end
More magic numbers are http://www.astro.keele.ac.uk/oldusers/rno/Computing/File_magic.html
bitlis
ReplyDeletekastamonu
çorum
van
sakarya
V3YD
https://titandijital.com.tr/
ReplyDeleteafyon parça eşya taşıma
düzce parça eşya taşıma
erzincan parça eşya taşıma
elazığ parça eşya taşıma
MTGGX
maraş evden eve nakliyat
ReplyDeleteosmaniye evden eve nakliyat
adıyaman evden eve nakliyat
istanbul evden eve nakliyat
ordu evden eve nakliyat
E5N
3896F
ReplyDeletesinop sesli sohbet sesli chat
ardahan yabancı görüntülü sohbet
yalova canlı sohbet siteleri
adana rastgele sohbet uygulaması
bingöl ücretsiz sohbet uygulamaları
yozgat görüntülü sohbet kızlarla
sohbet
karabük canlı görüntülü sohbet uygulamaları
uşak sohbet odaları
36BD6
ReplyDeleteArbitrum Coin Hangi Borsada
Tumblr Beğeni Satın Al
Kripto Para Kazanma Siteleri
Binance Ne Kadar Komisyon Alıyor
Binance Yaş Sınırı
Bitcoin Mining Nasıl Yapılır
Onlyfans Takipçi Hilesi
Spotify Takipçi Hilesi
Linkedin Takipçi Satın Al