(self, image, interpolation="LANCZOS", crop_position="center", sharpening=0.0)
| 1544 | CATEGORY = "ipadapter/utils" |
| 1545 | |
| 1546 | def prep_image(self, image, interpolation="LANCZOS", crop_position="center", sharpening=0.0): |
| 1547 | size = (224, 224) |
| 1548 | _, oh, ow, _ = image.shape |
| 1549 | output = image.permute([0,3,1,2]) |
| 1550 | |
| 1551 | if crop_position == "pad": |
| 1552 | if oh != ow: |
| 1553 | if oh > ow: |
| 1554 | pad = (oh - ow) // 2 |
| 1555 | pad = (pad, 0, pad, 0) |
| 1556 | elif ow > oh: |
| 1557 | pad = (ow - oh) // 2 |
| 1558 | pad = (0, pad, 0, pad) |
| 1559 | output = T.functional.pad(output, pad, fill=0) |
| 1560 | else: |
| 1561 | crop_size = min(oh, ow) |
| 1562 | x = (ow-crop_size) // 2 |
| 1563 | y = (oh-crop_size) // 2 |
| 1564 | if "top" in crop_position: |
| 1565 | y = 0 |
| 1566 | elif "bottom" in crop_position: |
| 1567 | y = oh-crop_size |
| 1568 | elif "left" in crop_position: |
| 1569 | x = 0 |
| 1570 | elif "right" in crop_position: |
| 1571 | x = ow-crop_size |
| 1572 | |
| 1573 | x2 = x+crop_size |
| 1574 | y2 = y+crop_size |
| 1575 | |
| 1576 | output = output[:, :, y:y2, x:x2] |
| 1577 | |
| 1578 | imgs = [] |
| 1579 | for img in output: |
| 1580 | img = T.ToPILImage()(img) # using PIL for better results |
| 1581 | img = img.resize(size, resample=Image.Resampling[interpolation]) |
| 1582 | imgs.append(T.ToTensor()(img)) |
| 1583 | output = torch.stack(imgs, dim=0) |
| 1584 | del imgs, img |
| 1585 | |
| 1586 | if sharpening > 0: |
| 1587 | output = contrast_adaptive_sharpening(output, sharpening) |
| 1588 | |
| 1589 | output = output.permute([0,2,3,1]) |
| 1590 | |
| 1591 | return (output, ) |
| 1592 | |
| 1593 | class IPAdapterSaveEmbeds: |
| 1594 | def __init__(self): |
nothing calls this directly
no test coverage detected