GAE images.resize 使用固定比例裁剪

发布于 2024-08-23 19:18:57 字数 851 浏览 4 评论 0原文

我需要从各种尺寸调整大小并裁剪为正好 60x80px 长宽比。就在我放入数据存储之前。任何人都已经得到了 这个问题解决了。

目前我已经成功地将其转换为精确的高度(80px) 具有各种宽度,当我尝试显示它时看起来不太好 一个清单。例如杰卡鲁塞尔。

我的 db.put 代码如下所示:

    if users.get_current_user():
        personal.personal_id = int(self.request.get('personal_id'))
        personal.name = self.request.get('name')
        personal.latitude = self.request.get('latitude')
        personal.info = self.request.get('info')
        photo = images.resize(self.request.get('img'), 0, 80)
        personal.photo = db.Blob(photo)
        personal.lc_id = int(self.request.get('lc_id'))
        personal.put()
        self.redirect('/admin/personal')

    else:
      self.response.out.write('I\'m sorry, you don\'t have permission to add this LP Personal Data.')

当我们在谷歌上上传头像时,我只想做类似的结果 谈话/谷歌聊天。

有人解决这个问题了吗?

谢谢

I need to resize and crop to exactly 60x80px from various size and
aspect ratio. Just before i put into Datastore. Anyone already got
this issue resolved.

Currently i already succed to just transform it to exact height (80px)
with various width which nott look so good when i try to display it on
a list. e.g jcaroussel.

My db.put code is like bellow:

    if users.get_current_user():
        personal.personal_id = int(self.request.get('personal_id'))
        personal.name = self.request.get('name')
        personal.latitude = self.request.get('latitude')
        personal.info = self.request.get('info')
        photo = images.resize(self.request.get('img'), 0, 80)
        personal.photo = db.Blob(photo)
        personal.lc_id = int(self.request.get('lc_id'))
        personal.put()
        self.redirect('/admin/personal')

    else:
      self.response.out.write('I\'m sorry, you don\'t have permission to add this LP Personal Data.')

I just want to do similar result when we upload our avatar on google
talk/google chat.

Anyone solved this?

Thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

屋檐 2024-08-30 19:18:57

将图片高度调整为 80 像素后,您必须使用 此处。例如:

img = images.Image(self.request.get('img'))
img.resize(0, 80)
resized_img = img.execute_transforms(output_encoding=images.JPEG)
left_x = (resized_img.width - 60) / 2
resized_img.crop(left_x, 0, left_x + 60, 80)
cropped_img = resized_image.execute_transforms(output_encoding=images.JPEG)

在我的示例中,它裁剪到图像的中心。
它假设调整后的图像宽度至少为 60 像素,但显然您必须添加一些检查来确认这一点,因为用户可能无法上传正确尺寸的图像。

After your resize your image down to 80 pixels in height, you would have to use the crop function as defined here. For example:

img = images.Image(self.request.get('img'))
img.resize(0, 80)
resized_img = img.execute_transforms(output_encoding=images.JPEG)
left_x = (resized_img.width - 60) / 2
resized_img.crop(left_x, 0, left_x + 60, 80)
cropped_img = resized_image.execute_transforms(output_encoding=images.JPEG)

In my example it crops to the center of the image.
It assumes that the resized image is at least 60 pixels wide, but obviously you would have to add some checks to confirm this, because a user might not upload an image in the right size.

痕至 2024-08-30 19:18:57

我使用了其他东西:

  • 将原始图像调整为最大高度 (80)
  • 存储调整大小(但完整/未裁剪)的图像
  • 将其显示在具有以下 CSS 的
    中: 宽度:60px;高度:80像素;溢出:隐藏;

这样它会在您的列表中很好地显示,但您仍然可以在用户的​​个人资料页面上显示完整的调整大小的图片(看着您的代码,我想这就是您想要做的,对吧?)

I used something else:

  • Resize the original image to your max height (80)
  • Store the resized (but complete/not cropped) image
  • Display it inside a <div> that has the following CSS: width: 60px; height: 80px; overflow: hidden;

That way it will show nicely in your list, but you can still display the complete resized picture on your user's profile page (looking at you code I imagine that's what you are trying to do, right?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文