生成任意文件大小的图像文件

发布于 2024-08-15 04:54:45 字数 283 浏览 3 评论 0原文

对于我正在进行的网络实验,我需要任意大小的图像文件。

我不知道如何创建此类文件。我需要 JPEG、PNG 和 PDF 格式的 5MB、10MB 和 20MB 文件。

在我第一次尝试生成这些文件时,很明显,由于这些格式的压缩方案,不可能(或者至少我不知道如何)仅通过指定空白文件的分辨率来生成任意大小的文件。

是否有一种快速(编程?)方法可以生成这些文件?

编辑:我正在研究创建任意大小的虚拟文件,但如果有一种方法可以创建具有正确内部格式的实际 JPEG、PNG 或 PDF,那将是理想的。

For a network experiment I am conducting, I require image files of arbitrary size.

I am lost on how to create such files. I require 5MB, 10MB and 20MB files in JPEG, PNG and PDF format.

In my first attempt at generating these files it became clear that due to the compression schemes of these formats, it's not possible (or at least I don't know how) to generate files of arbitrary size by just specifying resolution of blank files.

Is there a quick (programmatic?) method by which I could generate these files?

Edit: I am investigating creating arbitrarily sized dummy files, but if there is a way to create actual JPEG, PNGs or PDFs that have the correct internal format, that would be ideal.

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

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

发布评论

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

评论(4

带上头具痛哭 2024-08-22 04:54:45

这只是一个建议。不知道它是否会起作用。

也许您可以生成正确大小的位图并转换它们?

如果您使位图很难压缩(1)?然后,您应该能够将它们转换为非常接近目标尺寸的 jpg 和 png。

(1) 我的建议是,这可以通过使位图中的每个像素具有不同的颜色(尽可能)来实现

编辑:
这个简单的解决方案可能不适用于 jpg,但至少 png 似乎可以工作。

我用 C# 编写了一个小测试应用程序来尝试一下。这是它的核心内容:

private void make_bitmap(int width, int height)
{
    Random random = new Random();
    Bitmap B = new Bitmap(width, height);
    int redvalue = 0;
    int greenvalue = 0;
    int bluevalue = 0;

    bool pick_random_color = true;
    for (int x = 0; x < width; x++)
    {
        for (int y = 0; y < width; y++)
        {
            Color col;
            if (pick_random_color)
            {
               col = Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
            }
            else  // iterate over all colours repeatedly
            {   
                if (redvalue == 255) { redvalue = 0; greenvalue++; } else { redvalue++; }
                if (greenvalue == 256) { greenvalue = 0; bluevalue++; }
                if (bluevalue == 256) { redvalue = 0; greenvalue = 0; bluevalue = 0; }
                col = Color.FromArgb(redvalue, greenvalue, bluevalue);
            }
            B.SetPixel(x, y, col);
        }
    }
    B.Save("image.bmp");
    B.Save("image.jpg", ImageFormat.Jpeg);
    B.Save("image.png", ImageFormat.Png);
    B.Dispose();
}

This is just a suggestion. No idea if it will work.

Perhaps you could generate bitmaps at the correct size and convert them?

If you make the bitmaps very hard to compress(1)? You should then be able to convert them to jpg and png very close to the target size.

(1) My suggestion is that this might be achieved by making each pixel in the bitmap a different colour (as far as possible)

Edit:
This simple solution probably does not work for jpg's, but at least png's seems to work.

I hacked together a small testapp in c# to try it out. Here is the meat of it:

private void make_bitmap(int width, int height)
{
    Random random = new Random();
    Bitmap B = new Bitmap(width, height);
    int redvalue = 0;
    int greenvalue = 0;
    int bluevalue = 0;

    bool pick_random_color = true;
    for (int x = 0; x < width; x++)
    {
        for (int y = 0; y < width; y++)
        {
            Color col;
            if (pick_random_color)
            {
               col = Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
            }
            else  // iterate over all colours repeatedly
            {   
                if (redvalue == 255) { redvalue = 0; greenvalue++; } else { redvalue++; }
                if (greenvalue == 256) { greenvalue = 0; bluevalue++; }
                if (bluevalue == 256) { redvalue = 0; greenvalue = 0; bluevalue = 0; }
                col = Color.FromArgb(redvalue, greenvalue, bluevalue);
            }
            B.SetPixel(x, y, col);
        }
    }
    B.Save("image.bmp");
    B.Save("image.jpg", ImageFormat.Jpeg);
    B.Save("image.png", ImageFormat.Png);
    B.Dispose();
}
空城之時有危險 2024-08-22 04:54:45

文件内部是否需要为 JPEG 格式,或者只是名为 JPEG 的任意二进制文件?如果是后者,请参阅 Stack Overflow 答案:

用 C# 在几秒钟内创建一个巨大的虚拟文件

Do the files need to be JPEG format internally, or just arbitrary binary files named JPEG? If the latter, see this Stack Overflow answer:

Creating a Huge Dummy File in a Matter of Seconds in C#

风筝在阴天搁浅。 2024-08-22 04:54:45

分辨率不会改变像素数量,只会改变像素的显示大小 - 增加分辨率会使显示更大,但不会使文件更大。

如果你制作的 JPEG 一遍又一遍地重复相同的信息,它会变得更大——压缩是在 8x8 单元的基础上——如果你有更多的单元,你就会有更多的数据。要使它们更大,请将质量设置为 100 进行编码。另一件事是,JPEG 会以单元为单位去除亮度,因此色调变化的压缩效果比亮度变化的压缩效果更差。

我使用我公司的产品 DotImage,使用此代码创建了 10mb jpg。您可以下载一个 eval 并尝试一下,或者对您的图像 SDK 执行相同的操作。

using System;
using System.Drawing;
using Atalasoft.Imaging;
using Atalasoft.Imaging.Codec;
using Atalasoft.Imaging.Drawing;

namespace MakeLargeImage
{
    class Program
    {
        static void Main(string[] args)
        {
            AtalaImage img = new AtalaImage(1000, 8000, PixelFormat.Pixel24bppBgr);
            Canvas c = new Canvas(img);

            for (int block = 0; block < img.Height; block += 1000)
            {
                for (int line = 0; line < 1000; line += 4)
                {
                    int y = block + line;
                    c.DrawLine(new Point(0, y), new Point(img.Width, block), new AtalaPen(Color.Yellow));
                    c.DrawLine(new Point(0, y + 1), new Point(img.Width, y + 1), new AtalaPen(Color.Red));
                    c.DrawLine(new Point(0, y + 2), new Point(img.Width, y + 2), new AtalaPen(Color.Green));
                    c.DrawLine(new Point(0, block), new Point(img.Width, y + 3), new AtalaPen(Color.Blue));
                }
            }
            JpegEncoder jpeg = new JpegEncoder(100);
            img.Save("10mb.jpg", jpeg, null);
        }
    }
}

如果图像像素尺寸变大,JPEG 文件也会变大。您还可以添加元数据,但很难获得仅包含元数据的大文件,因为存在一些大小限制。

PDF是最简单的——只需用未压缩的图像制作一个大页面并不断添加页面——它会很快变大——除非你打开它,否则没有压缩。您可以使用 PdfEncoder 在 DotImage 中轻松完成此操作 - 只需在 FileSystemImageSource 中给它一个 jpg 列表即可 - 代码未显示,以免让每个人感到厌烦,但我'我很乐意将其提供给任何需要它的人 - 只需致电 Atalasoft 并询问我(或打开支持案例并参考这个问题。

The resolution doesn't change the number of pixels, just the display size of a pixel -- increasing resolution makes the display larger, but doesn't make the file larger.

If you make a JPEG that repeats the same information over and over again it will get bigger -- the compression is on a 8x8 cell basis -- if you have more cells, you'll have more data. To make them bigger, encode with quality set to 100. Another thing, JPEG strips out luminance on a cell basis, so variation in hue compresses worse than variation in luminance.

I used my company's product, DotImage, to create a 10mb jpg with this code. You could download an eval and try it out, or do the same with your image SDK.

using System;
using System.Drawing;
using Atalasoft.Imaging;
using Atalasoft.Imaging.Codec;
using Atalasoft.Imaging.Drawing;

namespace MakeLargeImage
{
    class Program
    {
        static void Main(string[] args)
        {
            AtalaImage img = new AtalaImage(1000, 8000, PixelFormat.Pixel24bppBgr);
            Canvas c = new Canvas(img);

            for (int block = 0; block < img.Height; block += 1000)
            {
                for (int line = 0; line < 1000; line += 4)
                {
                    int y = block + line;
                    c.DrawLine(new Point(0, y), new Point(img.Width, block), new AtalaPen(Color.Yellow));
                    c.DrawLine(new Point(0, y + 1), new Point(img.Width, y + 1), new AtalaPen(Color.Red));
                    c.DrawLine(new Point(0, y + 2), new Point(img.Width, y + 2), new AtalaPen(Color.Green));
                    c.DrawLine(new Point(0, block), new Point(img.Width, y + 3), new AtalaPen(Color.Blue));
                }
            }
            JpegEncoder jpeg = new JpegEncoder(100);
            img.Save("10mb.jpg", jpeg, null);
        }
    }
}

If you make the image pixel size larger, the JPEG file will get larger. You could also add in meta-data, but it's hard to get a big file with just meta-data as there are some size limitations.

PDF is the easiest -- just make a big page with uncompressed image and keep adding pages -- it will get big fast -- there's no compression unless you turn it on. You can do this easily in DotImage with the PdfEncoder -- just give it a list of jpgs in a FileSystemImageSource -- code not shown as to not bore everyone, but I'd be happy to provide it to anyone that wants it -- just call Atalasoft and ask for me (or open a support case and reference this question.

星光不落少年眉 2024-08-22 04:54:45

我将从所有这些格式的“巨大”文件开始(超过最大文件大小),然后使用 imagemagick 转换,直到找到所需文件大小的具体信息。

可能需要一些尝试和错误(不过可以很容易地编写脚本),但应该适合一次性实验。

I would start with a 'huge' file in all of these formats (over max. filesize) and then iteratively resize them using imagemagick convert until you found the specifics for your wanted filesize.

May take some trial and error (can easily be scripted though) but should do for a one-time experiment.

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