Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在上传到google云存储django之前调整图像大小_Python_Django_Google Cloud Platform - Fatal编程技术网

Python 在上传到google云存储django之前调整图像大小

Python 在上传到google云存储django之前调整图像大小,python,django,google-cloud-platform,Python,Django,Google Cloud Platform,我正在尝试调整和压缩从django应用程序上传的图像,然后再上传到google云存储。我尝试使用firebase调整自动上传的任何图像的大小,但它给出了调整大小的错误。我希望我的django应用程序在上传到谷歌云之前先进行压缩。标准枕头压缩在云存储上不起作用。如何才能做到这一点 以下面的查看功能为例,在上传之前我如何调整图像的大小 if request.method == 'POST': form = GalleryForm(request.POST, request.FILES)

我正在尝试调整和压缩从django应用程序上传的图像,然后再上传到google云存储。我尝试使用firebase调整自动上传的任何图像的大小,但它给出了调整大小的错误。我希望我的django应用程序在上传到谷歌云之前先进行压缩。标准枕头压缩在云存储上不起作用。如何才能做到这一点

以下面的查看功能为例,在上传之前我如何调整图像的大小

if request.method == 'POST':
    form = GalleryForm(request.POST, request.FILES)
    if form.is_valid():
        image = form.save(commit=False)
        image.user = usr
        image.save()
        return HttpResponseRedirect(reverse('profile-detail', args=(), kwargs={'slug': slug}))
现在,我的应用程序会将图片(无论大小)上传到谷歌云,然后打开图片,调整大小,覆盖最初上传的图片。我觉得这最终可能会给我的应用程序进程带来压力。我希望我的应用程序在上传之前只调整图像大小。下面是我上传后用来调整图片大小的代码

def save(self, *args, **kwargs):
    super().save(*args, **kwargs)

    img_read = storage.open(self.image.name, 'r')
    img = Image.open(img_read)

    if img.height > 801 or img.width > 534:
        output_size = (801, 534)
        img.thumbnail(output_size)
        in_mem_file = io.BytesIO()
        img.save(in_mem_file, format='JPEG')
        img_write = storage.open(self.image.name, 'w+')
        img_write.write(in_mem_file.getvalue())
        img_write.close()

    img_read.close() 
另一个回溯是,图像必须是“JPEG”格式才能工作。我正在寻找一种更好的方法来完成这项工作。

(尽管缺乏信息,但仍在尝试回答)

可以使用OpenCV调整大小:

import cv2

img = cv2.imread('/home/img/image_path', cv2.IMREAD_UNCHANGED)

print('Original Dimensions : ',img.shape)

scale_percent = 60 # percent of original size
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
dim = (width, height)
# resize image
resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)

from PIL import Image

im = Image.open(r"1.png")

width, height = im.size

# reduce to 30% of the original size
target_size_percent = 30
new_width = int(width * target_size_percent / 100)
new_height = int(height * target_size_percent / 100)
new_size = (new_width, new_height)
im1 = im.resize(new_size)

im1.show()
im1.save('resized.png')

既然您提到了使用枕头,这段代码应该有助于调整大小:

import cv2

img = cv2.imread('/home/img/image_path', cv2.IMREAD_UNCHANGED)

print('Original Dimensions : ',img.shape)

scale_percent = 60 # percent of original size
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
dim = (width, height)
# resize image
resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)

from PIL import Image

im = Image.open(r"1.png")

width, height = im.size

# reduce to 30% of the original size
target_size_percent = 30
new_width = int(width * target_size_percent / 100)
new_height = int(height * target_size_percent / 100)
new_size = (new_width, new_height)
im1 = im.resize(new_size)

im1.show()
im1.save('resized.png')
如果有固定的目标大小,可以更改新的\u宽度和新的\u高度变量以匹配这些大小。只要尽可能保持图像的纵横比即可