Skip to content

Instantly share code, notes, and snippets.

@shun115
Created March 8, 2011 10:39
Show Gist options
  • Save shun115/860132 to your computer and use it in GitHub Desktop.
Save shun115/860132 to your computer and use it in GitHub Desktop.
PythonでImageからSHA1 Hashを取得
$ wget http://k.yimg.jp/images/top/sp/logo.gif
--2011-03-08 19:34:32-- http://k.yimg.jp/images/top/sp/logo.gif
k.yimg.jp をDNSに問いあわせています... 202.93.65.122
k.yimg.jp|202.93.65.122|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 2077 (2.0K) [image/gif]
`logo.gif' に保存中
100%[====================================================================================================================================================================>] 2,077 --.-K/s 時間 0s
2011-03-08 19:34:32 (124 MB/s) - `logo.gif' へ保存完了 [2077/2077]
$ python
>>> import hashlib
>>> import urllib
>>> import Image
>>> from cStringIO import StringIO
>>> test1 = hashlib.sha1(''.join(map(str, list(Image.open('logo.gif').getdata())))).hexdigest()
>>> test1
'464e3bf1cfe65de3396cdb1973542aa4a7318b94'
>>> a = urllib.urlopen('http://k.yimg.jp/images/top/sp/logo.gif')
>>> test2 = hashlib.sha1(''.join(map(str, list(Image.open(StringIO(a.read())).getdata())))).hexdigest()
>>> test2
'464e3bf1cfe65de3396cdb1973542aa4a7318b94'
>>> test1 == test2
True
@shun115
Copy link
Author

shun115 commented Mar 8, 2011

hex(sum(list(Image.open('logo.gif').getdata())))

これでもいいかも。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment