日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python 下载图片到内存卡_python - 获取图像大小而不将图像加载到内存中

發(fā)布時間:2024/9/27 python 81 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 下载图片到内存卡_python - 获取图像大小而不将图像加载到内存中 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

如果您不關(guān)心圖像內(nèi)容,PIL可能是一種過度殺傷力。

我建議解析python magic模塊的輸出:

>>> t = magic.from_file('teste.png')

>>> t

'PNG image data, 782 x 602, 8-bit/color RGBA, non-interlaced'

>>> re.search('(\d+) x (\d+)', t).groups()

('782', '602')

這是libmagic的包裝器,它讀取盡可能少的字節(jié)以識別文件類型簽名。

相關(guān)版本的腳本:

[https://raw.githubusercontent.com/scardine/image_size/master/get_image_size.py]

[更新]

嗯,不幸的是,當(dāng)應(yīng)用于jpegs時,上面給出了''JPEG圖像數(shù)據(jù),EXIF標準2.21'“。 沒有圖像尺寸! - 亞歷克斯弗林特

看起來像jpegs是魔法抗性的。:-)

我明白為什么:為了獲得JPEG文件的圖像尺寸,你可能需要閱讀比libmagic喜歡的字節(jié)。

卷起袖子,帶著這個非常未經(jīng)測試的片段(從GitHub獲取),不需要第三方模塊。

#-------------------------------------------------------------------------------

# Name: get_image_size

# Purpose: extract image dimensions given a file path using just

# core modules

#

# Author: Paulo Scardine (based on code from Emmanuel VA?SSE)

#

# Created: 26/09/2013

# Copyright: (c) Paulo Scardine 2013

# Licence: MIT

#-------------------------------------------------------------------------------

#!/usr/bin/env python

import os

import struct

class UnknownImageFormat(Exception):

pass

def get_image_size(file_path):

"""

Return (width, height) for a given img file content - no external

dependencies except the os and struct modules from core

"""

size = os.path.getsize(file_path)

with open(file_path) as input:

height = -1

width = -1

data = input.read(25)

if (size >= 10) and data[:6] in ('GIF87a', 'GIF89a'):

# GIFs

w, h = struct.unpack("

width = int(w)

height = int(h)

elif ((size >= 24) and data.startswith('\211PNG\r\n\032\n')

and (data[12:16] == 'IHDR')):

# PNGs

w, h = struct.unpack(">LL", data[16:24])

width = int(w)

height = int(h)

elif (size >= 16) and data.startswith('\211PNG\r\n\032\n'):

# older PNGs?

w, h = struct.unpack(">LL", data[8:16])

width = int(w)

height = int(h)

elif (size >= 2) and data.startswith('\377\330'):

# JPEG

msg = " raised while trying to decode as JPEG."

input.seek(0)

input.read(2)

b = input.read(1)

try:

while (b and ord(b) != 0xDA):

while (ord(b) != 0xFF): b = input.read(1)

while (ord(b) == 0xFF): b = input.read(1)

if (ord(b) >= 0xC0 and ord(b) <= 0xC3):

input.read(3)

h, w = struct.unpack(">HH", input.read(4))

break

else:

input.read(int(struct.unpack(">H", input.read(2))[0])-2)

b = input.read(1)

width = int(w)

height = int(h)

except struct.error:

raise UnknownImageFormat("StructError" + msg)

except ValueError:

raise UnknownImageFormat("ValueError" + msg)

except Exception as e:

raise UnknownImageFormat(e.__class__.__name__ + msg)

else:

raise UnknownImageFormat(

"Sorry, don't know how to get information from this file."

)

return width, height

總結(jié)

以上是生活随笔為你收集整理的python 下载图片到内存卡_python - 获取图像大小而不将图像加载到内存中的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。