from PIL import Image
import pywikibot as pw
from pywikibot import pagegenerators as g
import requests
from io import BytesIO
import os

site = pw.Site('hy', 'wikipedia')

gen = g.LinkedPageGenerator(pw.Page(site, 'Մասնակից:ԱշոտՏՆՂ/ցանկեր/մեծ նկարներ'))
for file in gen:
    url = file.get_file_url()
    response = requests.get(url)
    try:
        img = Image.open(BytesIO(response.content))
    except OSError:
        continue
    if max(img.height, img.width) <= 600:
        continue
    ratio = 600/max(img.height, img.width)
    h = int(img.height * ratio)
    w = int(img.width * ratio)
    img = img.resize((w, h), Image.ANTIALIAS)
    img.save(file.title())
    file.upload(file.title(), comment='կանոնակարգին համապատասխանող փոքր տարբերակ', ignore_warnings=True)
    os.remove(file.title())
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.
Upload successful.