2

Please note that I'm doing my first programming project and I'm inexperienced. It's a simple project, but I'm having trouble with one problem: the result is always the same image. I've checked the file names and they are correct, but the picture is the same in all of them. I'm using Colab to work on this project.

Any suggestions would be greatly appreciated, including criticism. Thank you!

    from google.colab import files

import os #para poder ejecutar ordenes sobre el sistema   \<------- check

archivos = files.upload()
import zipfile

#Let's use with as method for descompress my .zip (It's Javascript), this opens the files and after function, close it
#"r"= read files 
#"zip_ref" is the new reference to call pixelsdata.zip descompressed

with zipfile.ZipFile("pixelsdataset.zip", "r") as zip_ref:    
    zip_ref.extractall("pixelxtract")  #files will be sent to this adress
pixelextract = ('//content//pixelxtract') #save folder
pixelsized= ('//content//pixelsized') #this will be final folder
import matplotlib.pyplot as plt #this library contains useful functions to show img files
listaimg= os.listdir(pixelextract) 
print(listaimg)
for img in listaimg:
    if img.endswith(".jpg") or img.endswith(".PNG") :   #makes sure its format
      ruta_img = os.path.join(pixelextract, img) #match rute parts so uses the corect way using fx '//' or `/`
      imagenfull= plt.imread(ruta_img) # reads img with matplotlib
      plt.imshow(imagenfull) #shows figure
      plt.title(img)
      plt.show() #shows picture of the figure
      plt.clf()
from PIL import Image
newsize= (500,500)
counter= 0
for img in listaimg:
  ruta_img
  if img.endswith(".jpg") or img.endswith(".PNG") :
    try:
      imgopen= Image.open(ruta_img)  #open original file, uses rutes so needs joined one
      imgsized= imgopen.resize(newsize) #convert size
      ruta_sizedimg= os.path.join(pixelsized, img) 
      imgsized.save(ruta_sizedimg)        
      counter += 1
      plt.clf()
      
    except: print({f'Error opening file  {imgopen}, {counter}'})
 #this library contains useful functions to show img files
listaimgszed= os.listdir(pixelsized) 
print(listaimgszed)
counter= 0
for img in listaimgszed:
    if img.endswith(".jpg") or img.endswith(".PNG") :   #makes sure its format
      ruta_sizedimg = os.path.join(pixelsized, img) 
      resultado= plt.imread(ruta_sizedimg) # reads img with matplotlib
      plt.imshow(resultado) #shows figure
      plt.title(f"{img} ({counter+1}/{len(listaimgszed)})")
      plt.show() 
      counter += 1

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
omarzmr
  • 21
  • 3
  • 1
    In your second `for` loop, you are opening `ruta_img` on every iteration of the loop, rather than `img` which is the variable actually being assigned by the loop. – jasonharper May 10 '23 at 19:06

0 Answers0