如何在不获取Google App Engine中所有数据的情况下获取图像的宽度和高度值? [英] How can I get width and height values of the image without fetching all data in Google App Engine?

查看:72
本文介绍了如何在不获取Google App Engine中所有数据的情况下获取图像的宽度和高度值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google App Engine上创建了一个应用程序.我们将所有图像保存在Google Cloud Storage中.该应用使用 get_serving_url 来提供照片方法.该应用程序通过更改服务网址来进行大小控制.此时,我们需要获取图像的宽度和高度值.

I create an application on Google App Engine. We keep all images on Google Cloud Storage. This app serves photos with using get_serving_url method. The application does size manipulations by changing the serving url. At this point, we need to get image width and height values.

我可以这样:

blob_key = blobstore.create_gs_key(_gspath)
data = blobstore.fetch_data(blob_key, 0, 50000)
raw_image = images.Image(image_data=data)

这样做之后,我可以通过raw_image变量获取宽度和高度值.但是我想这样不好.我不想获取所有数据,因为某些图像太大.如何在不获取数据的情况下获取此信息?

After doing that, I can get width and height values via raw_image variable. But I guess this way isn't good. I don't want to fetch all data because some images are too big. How can I get this information without fetch data operation?

推荐答案

Google App Engine不提供一种内置方法来获取图像尺寸,而无需将其加载到内存中.您可以使用一些替代方法:

Google App Engine does not provide a built-in way to get dimensions of an image without loading it into memory. There are some alternatives you could use:

  1. 您可以创建一个在GCE上运行的服务,该服务将从GCS提取图像并返回大小.然后,您的GAE应用可以调用此方法.这就将内存需求移到了GCE,它可以处理更多的需求.

  1. You could create a service running on GCE that will fetch an image from GCS, and return the size. Your GAE app could then call this. This moves the memory requirement to GCE, which is much more able to handle it.

如果仅使用PNG,GIF,BMP和ICO格式(而不是JPEG或TIFF),则只需获取图像的前26个字节并将其作为image_data传递.这足以使Images类能够获取宽度和高度.

If you only use PNG, GIF, BMP and ICO formats (not JPEG or TIFF) then you can just fetch the first 26 bytes of the image and pass it as image_data. That is enough for the Images class to be able to get width and height.

如果您控制上传过程,则可以将宽度和高度作为元数据存储在GCS中.然后,您无需获取GCS对象,只需获取它的元数据即可获取尺寸.

If you control the upload process, you could store the width and height as metadata in GCS. Then you don't need to fetch the GCS object, just it's metadata in order to get the dimensions.

这篇关于如何在不获取Google App Engine中所有数据的情况下获取图像的宽度和高度值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆