Python 使用相同目录的图像更新目录中的图像

Python 使用相同目录的图像更新目录中的图像,python,image-processing,google-colaboratory,Python,Image Processing,Google Colaboratory,我有一个目录,里面有一组不同大小的图片,让我给你看看图片和它们的大小 from google.colab import drive drive.mount('/content/drive') from PIL import Image import glob import time from pylab import * for filename in glob.iglob('/content/drive/My Drive/Colab Notebooks/Cats/*.jpg'): prin

我有一个目录,里面有一组不同大小的图片,让我给你看看图片和它们的大小

from google.colab import drive
drive.mount('/content/drive')
from PIL import Image
import glob
import time
from pylab import *
for filename in glob.iglob('/content/drive/My Drive/Colab Notebooks/Cats/*.jpg'):
  print(filename)
from PIL import Image
import glob
import time
from pylab import *
for filename in glob.iglob('/content/drive/My Drive/Colab Notebooks/Cats/*.jpg'):
  im=array(Image.open(filename))
  print(im.shape)
该代码的结果是:

/content/drive/My Drive/Colab Notebooks/Cats/cat1.jpg
/content/drive/My Drive/Colab Notebooks/Cats/cat2.jpg
/content/drive/My Drive/Colab Notebooks/Cats/cat3.jpg
/content/drive/My Drive/Colab Notebooks/Cats/cat4.jpg
/content/drive/My Drive/Colab Notebooks/Cats/cat5.jpg
/content/drive/My Drive/Colab Notebooks/Cats/cat6.jpg
/content/drive/My Drive/Colab Notebooks/Cats/cat7.jpg
现在让我们考虑它们的尺寸

from google.colab import drive
drive.mount('/content/drive')
from PIL import Image
import glob
import time
from pylab import *
for filename in glob.iglob('/content/drive/My Drive/Colab Notebooks/Cats/*.jpg'):
  print(filename)
from PIL import Image
import glob
import time
from pylab import *
for filename in glob.iglob('/content/drive/My Drive/Colab Notebooks/Cats/*.jpg'):
  im=array(Image.open(filename))
  print(im.shape)
该代码的结果是:

(410, 618, 3)
(1200, 1800, 3)
(576, 1024, 3)
(1533, 2300, 3)
(400, 600, 3)
(264, 191, 3)
(194, 259, 3)
当然,我可以使用下面的行将其转换为灰度

im=array(Image.open(filename).convert('L'))
结果:

(410, 618)
(1200, 1800)
(576, 1024)
(1533, 2300)
(400, 600)
(264, 191)
(194, 259)
(100, 100)
(100, 100)
(100, 100)
(100, 100)
(100, 100)
(100, 100)
(100, 100)

正如你所看到的,不同的图像有不同的大小,我想要的是以相同的大小重塑所有图像(我知道这一个存在调整大小的功能),我想用相同的图像更新(替换)旧图像,所以我有,我的目录中的所有图像都应该有相同的大小,我该怎么做?请帮助我解决这个问题非常简单,然后我就开始考虑了,下面是我的解决方案

from google.colab import drive
drive.mount('/content/drive')
from PIL import Image
import glob
import time
from pylab import *
#directory_name ='/content/drive/My Drive/Colab Notebooks/Cats/'
for filename in glob.iglob('/content/drive/My Drive/Colab Notebooks/Cats/*.jpg'):
  #print(filename[-8:])
  im=(Image.open(filename).convert('L'))
  im=im.resize((100,100))
  #filename=filename[-8:]
  #complete_name =directory_name+filename
  print(complete_name)
  im.save(filename)
测试:

for filename in glob.iglob('/content/drive/My Drive/Colab Notebooks/Cats/*.jpg'):
   im=array((Image.open(filename).convert('L')))
   print(im.shape)
结果:

(410, 618)
(1200, 1800)
(576, 1024)
(1533, 2300)
(400, 600)
(264, 191)
(194, 259)
(100, 100)
(100, 100)
(100, 100)
(100, 100)
(100, 100)
(100, 100)
(100, 100)

解决方案非常简单,然后我就在考虑,所以这里是我的解决方案

from google.colab import drive
drive.mount('/content/drive')
from PIL import Image
import glob
import time
from pylab import *
#directory_name ='/content/drive/My Drive/Colab Notebooks/Cats/'
for filename in glob.iglob('/content/drive/My Drive/Colab Notebooks/Cats/*.jpg'):
  #print(filename[-8:])
  im=(Image.open(filename).convert('L'))
  im=im.resize((100,100))
  #filename=filename[-8:]
  #complete_name =directory_name+filename
  print(complete_name)
  im.save(filename)
测试:

for filename in glob.iglob('/content/drive/My Drive/Colab Notebooks/Cats/*.jpg'):
   im=array((Image.open(filename).convert('L')))
   print(im.shape)
结果:

(410, 618)
(1200, 1800)
(576, 1024)
(1533, 2300)
(400, 600)
(264, 191)
(194, 259)
(100, 100)
(100, 100)
(100, 100)
(100, 100)
(100, 100)
(100, 100)
(100, 100)

在这种情况下,接受你自己的答案是肯定的。在这种情况下,接受你自己的答案是肯定的。