以编程方式更改图像分辨率

发布于 2025-01-03 13:44:48 字数 268 浏览 0 评论 0原文

我计算过,如果我希望生成的图像为 A4 尺寸 @ 600dpi 用于打印目的,则需要为 7016x4961px @ 72dpi。因此,我以编程方式生成它,然后在 Photoshop 中测试它,它似乎很好,所以如果我调整它的大小,它会获得适当的大小和分辨率

Photoshop 中的图像尺寸对话框

我想知道是否可以通过编程方式调整大小,最好使用 PIL,但不一定使用它。我需要提高 DPI。

I have calculated that if I want my generated image to be A4 size @ 600dpi for print purpose, it needs to be 7016x4961px @ 72dpi. So, I generate it programmatically, then test it in Photoshop and it seems to be fine so if I resize it, it gets proper size and resolution

Image size dialog in Photoshop.

What I wonder about is if it's possible to make this resizing programmatically, preferably with PIL, but not necessarily with it. I need to make it higher DPI.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陪你搞怪i 2025-01-10 13:44:48

如果您生成的图像尺寸为 7016 x 4961 像素,则它已经是 600 dpi 的 A4 尺寸。所以你不需要调整它的大小,你只需要在文件中设置分辨率信息。

你可以用 PIL 来做到这一点:

from PIL import Image

im = Image.open("test.png")
im.save("test-600.png", dpi=(600,600))

If you have generated your image 7016 x 4961 px, it is already A4 at 600 dpi. So you don't need to resize it, you just have to set resolution information in file.

You can do it with PIL:

from PIL import Image

im = Image.open("test.png")
im.save("test-600.png", dpi=(600,600))
北笙凉宸 2025-01-10 13:44:48

此代码将使用 PIL 将 PNG 图像的大小调整为 7016x4961:

size = 7016, 4961
im = Image.open("my_image.png")
im_resized = im.resize(size, Image.ANTIALIAS)
im_resized.save("my_image_resized.png", "PNG")

也许更好的方法是在打印之前将画布放大 x 倍,其中 x 是您拥有的一个因素找出(此特定图像的尺寸为 7016x4961)。

This code will resize a PNG image into 7016x4961 with PIL:

size = 7016, 4961
im = Image.open("my_image.png")
im_resized = im.resize(size, Image.ANTIALIAS)
im_resized.save("my_image_resized.png", "PNG")

Perhaps a better approach would be to make your canvas x times bigger prior to printing, where x is a factor you have to figure out (7016x4961 in size for this particular image).

护你周全 2025-01-10 13:44:48

以下是如何批量调整大小(每个文件夹)并跳过其他文件类型和 Mac 系统文件(例如 .DS_Store)

from PIL import Image
import os

Image.MAX_IMAGE_PIXELS = None

path = "./*your-source-folder*"

resize_ratio = 2  # where 0.5 is half size, 2 is double size



def resize_aspect_fit():
    dirs = os.listdir(path)
    for item in dirs:
        print(item)
        if item == '.DS_Store':
            continue

        if item == 'Icon\r':
            continue
      
        if item.endswith(".mp4"):
            continue
        
        if item.endswith(".txt"):
            continue
        
        if item.endswith(".db"):
            continue

        if os.path.isfile(path+item):
            image = Image.open(path+item)
            file_path, extension = os.path.splitext(path+item)
            new_image_height = int(image.size[0] / (1/resize_ratio))
            new_image_length = int(image.size[1] / (1/resize_ratio))

            image = image.resize((new_image_height, new_image_length), Image.ANTIALIAS)
            
            image.save("./*your-output-folder*/" + item)


resize_aspect_fit()

Here's how you can resize by batch (per folder) and skip other file types and Mac system files like .DS_Store

from PIL import Image
import os

Image.MAX_IMAGE_PIXELS = None

path = "./*your-source-folder*"

resize_ratio = 2  # where 0.5 is half size, 2 is double size



def resize_aspect_fit():
    dirs = os.listdir(path)
    for item in dirs:
        print(item)
        if item == '.DS_Store':
            continue

        if item == 'Icon\r':
            continue
      
        if item.endswith(".mp4"):
            continue
        
        if item.endswith(".txt"):
            continue
        
        if item.endswith(".db"):
            continue

        if os.path.isfile(path+item):
            image = Image.open(path+item)
            file_path, extension = os.path.splitext(path+item)
            new_image_height = int(image.size[0] / (1/resize_ratio))
            new_image_length = int(image.size[1] / (1/resize_ratio))

            image = image.resize((new_image_height, new_image_length), Image.ANTIALIAS)
            
            image.save("./*your-output-folder*/" + item)


resize_aspect_fit()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文