Jak zmienić rozmiar obrazu za pomocą PIL i zachować jego proporcje?
Czy jest jakiś oczywisty sposób, żeby to zrobić, którego mi brakuje? Próbuję zrobić miniaturki.
11 answers
Zdefiniuj maksymalny rozmiar.
Następnie Oblicz współczynnik zmiany rozmiaru, biorąc min(maxwidth/width, maxheight/height)
.
Odpowiedni rozmiar to oldsize*ratio
.
Istnieje oczywiście również metoda biblioteczna: Metoda Image.thumbnail
.
Poniżej znajduje się (edytowany) przykład z dokumentacji PIL.
import os, sys
import Image
size = 128, 128
for infile in sys.argv[1:]:
outfile = os.path.splitext(infile)[0] + ".thumbnail"
if infile != outfile:
try:
im = Image.open(infile)
im.thumbnail(size, Image.ANTIALIAS)
im.save(outfile, "JPEG")
except IOError:
print "cannot create thumbnail for '%s'" % infile
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-11 18:46:28
Ten skrypt zmieni rozmiar obrazu (somepic.jpg) za pomocą Pil (Python Imaging Library) do szerokości 300 pikseli i wysokości proporcjonalnej do nowej szerokości. Robi to poprzez określenie, jaki procent 300 pikseli jest oryginalnej szerokości (img.rozmiar [0]), a następnie pomnożenie oryginalnej wysokości (img.wielkość [1]) o tę wartość procentową. Zmień "basewidth" na dowolną inną liczbę, aby zmienić domyślną szerokość obrazów.
from PIL import Image
basewidth = 300
img = Image.open('somepic.jpg')
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
img.save('sompic.jpg')
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-11 07:08:51
Polecam również użycie metody pil thumbnail, ponieważ usuwa z ciebie wszystkie problemy związane z proporcjami.
Jedna ważna wskazówka: Zastąp
im.thumbnail(size)
Z
im.thumbnail(size,Image.ANTIALIAS)
Domyślnie pil używa obrazu.Najbliższy filtr do zmiany rozmiaru, co powoduje dobrą wydajność, ale słabą jakość.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-06-02 16:03:07
Bazując na @ tomvon, skończyłem używać:
Zmiana szerokości:
new_width = 680
new_height = new_width * height / width
Zmiana rozmiaru wysokości:
new_height = 680
new_width = new_height * width / height
Wtedy po prostu:
img = img.resize((new_width, new_height), Image.ANTIALIAS)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-10 17:39:45
PIL ma już opcję kadrowania obrazu
img = ImageOps.fit(img, size, Image.ANTIALIAS)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-01 13:09:57
Jeśli próbujesz zachować ten sam współczynnik proporcji, nie zmieniłbyś rozmiaru o pewien procent oryginalnego rozmiaru?
Na przykład połowa oryginalnego rozmiaru
half = 0.5
out = im.resize( [int(half * s) for s in im.size] )
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-12-13 03:43:00
from PIL import Image
img = Image.open('/your iamge path/image.jpg') # image extension *.png,*.jpg
new_width = 200
new_height = 300
img = img.resize((new_width, new_height), Image.ANTIALIAS)
img.save('output image name.png') # format may what u want ,*.png,*jpg,*.gif
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-08 12:44:06
from PIL import Image
from resizeimage import resizeimage
def resize_file(in_file, out_file, size):
with open(in_file) as fd:
image = resizeimage.resize_thumbnail(Image.open(fd), size)
image.save(out_file)
image.close()
resize_file('foo.tif', 'foo_small.jpg', (256, 256))
Używam tej biblioteki:
pip install python-resize-image
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-27 10:02:19
Mój brzydki przykład.
Funkcja get file like: "pic[0-9a-z].[rozszerzenie]", Zmień rozmiar na 120x120, przenieś sekcję do środka i zapisz do " ico[0-9A-z].[rozszerzenie]", działa z portretem i krajobrazem:
def imageResize(filepath):
from PIL import Image
file_dir=os.path.split(filepath)
img = Image.open(filepath)
if img.size[0] > img.size[1]:
aspect = img.size[1]/120
new_size = (img.size[0]/aspect, 120)
else:
aspect = img.size[0]/120
new_size = (120, img.size[1]/aspect)
img.resize(new_size).save(file_dir[0]+'/ico'+file_dir[1][3:])
img = Image.open(file_dir[0]+'/ico'+file_dir[1][3:])
if img.size[0] > img.size[1]:
new_img = img.crop( (
(((img.size[0])-120)/2),
0,
120+(((img.size[0])-120)/2),
120
) )
else:
new_img = img.crop( (
0,
(((img.size[1])-120)/2),
120,
120+(((img.size[1])-120)/2)
) )
new_img.save(file_dir[0]+'/ico'+file_dir[1][3:])
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-22 10:48:36
Właśnie uaktualniam to pytanie o bardziej nowoczesne opakowanie Ta biblioteka owija poduszkę (widelec PIL) https://pypi.org/project/python-resize-image/
Pozwala Ci zrobić coś takiego:-
from PIL import Image
from resizeimage import resizeimage
fd_img = open('test-image.jpeg', 'r')
img = Image.open(fd_img)
img = resizeimage.resize_width(img, 200)
img.save('test-image-width.jpeg', img.format)
fd_img.close()
Zawiera więcej przykładów w powyższym linku.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-15 10:04:53
Prosta metoda utrzymywania ograniczonych proporcji i przechodzenia maksymalnej szerokości / wysokości. Nie najładniejsza ale daje radę i jest łatwa do zrozumienia:
def resize(img_path, max_px_size, output_folder):
with Image.open(img_path) as img:
width_0, height_0 = img.size
out_f_name = os.path.split(img_path)[-1]
out_f_path = os.path.join(output_folder, out_f_name)
if max((width_0, height_0)) <= max_px_size:
print('writing {} to disk (no change from original)'.format(out_f_path))
img.save(out_f_path)
return
if width_0 > height_0:
wpercent = max_px_size / float(width_0)
hsize = int(float(height_0) * float(wpercent))
img = img.resize((max_px_size, hsize), Image.ANTIALIAS)
print('writing {} to disk'.format(out_f_path))
img.save(out_f_path)
return
if width_0 < height_0:
hpercent = max_px_size / float(height_0)
wsize = int(float(width_0) * float(hpercent))
img = img.resize((max_px_size, wsize), Image.ANTIALIAS)
print('writing {} to disk'.format(out_f_path))
img.save(out_f_path)
return
Oto skrypt Pythona, który używa tej funkcji do uruchamiania wsadowej zmiany rozmiaru obrazu.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-31 06:40:10