Thread overview
How to resize an image ? 🤔
Dec 25, 2020
vnr
Dec 25, 2020
Ferhat Kurtulmuş
Dec 25, 2020
vnr
Dec 25, 2020
Adam D. Ruppe
Dec 25, 2020
Ali Çehreli
Dec 25, 2020
Guillaume Piolat
Dec 27, 2020
Guillaume Piolat
Dec 27, 2020
vnr
Dec 27, 2020
Adam D. Ruppe
Dec 27, 2020
JN
December 25, 2020
Hello 😺

For a small "script" that generates printable files, I would need to change the size of an image (which is loaded into memory as an array of bytes) to shrink it to scale if it exceeds the A4 page size.

To load the images into memory and generate a PDF, I use the "printed" package. It is not very provided but is sufficient for my use, I just need the resize option... Is there a relatively simple way to do this?

Thank you.
December 25, 2020
On Friday, 25 December 2020 at 20:59:03 UTC, vnr wrote:
> Hello 😺
>
> For a small "script" that generates printable files, I would need to change the size of an image (which is loaded into memory as an array of bytes) to shrink it to scale if it exceeds the A4 page size.
>
> To load the images into memory and generate a PDF, I use the "printed" package. It is not very provided but is sufficient for my use, I just need the resize option... Is there a relatively simple way to do this?
>
> Thank you.

Check this out:
https://github.com/adamdruppe/arsd/blob/master/image.d#L434
December 25, 2020
On Friday, 25 December 2020 at 21:05:19 UTC, Ferhat Kurtulmuş wrote:
> On Friday, 25 December 2020 at 20:59:03 UTC, vnr wrote:
>> Hello 😺
>>
>> For a small "script" that generates printable files, I would need to change the size of an image (which is loaded into memory as an array of bytes) to shrink it to scale if it exceeds the A4 page size.
>>
>> To load the images into memory and generate a PDF, I use the "printed" package. It is not very provided but is sufficient for my use, I just need the resize option... Is there a relatively simple way to do this?
>>
>> Thank you.
>
> Check this out:
> https://github.com/adamdruppe/arsd/blob/master/image.d#L434

Thank you very much, this solution seems to be really suitable. Nevertheless, when I try to use the given function, the value it returns seems to be null. Here is my code:


   // Define an image usable by arsd.image
   auto tmp = new IndexedImage(myWidth, myHeight);
   tmp.data = cast(ubyte[]) myImageData;

   // Setting the new image from the 'tmp' resized
   auto newImg = imageResize(tmp, 500, 500);

   // Testing
   writeln(test.imageData.bytes[0 .. 50]); // [0, 0, 0, 0, ..., 0]
   writeln(guessImageFormatFromMemory(tci.imageData.bytes)); // "Unknown"


This code seems relatively logical to me, but it doesn't work, as we can see, the resulting array of bytes is filled with 0, as if the resizeImage function had had no effect. Do you know where the error is?
December 25, 2020
On Friday, 25 December 2020 at 22:59:55 UTC, vnr wrote:
>    tmp.data = cast(ubyte[]) myImageData;

You set bytes here but an IndexedImage also needs a palette which you didn't set up.

What format is your myImageData in? It might be more appropriate to set it to a TrueColorImage.

IndexedImage: each pixel is one byte and it refers to a Color[] palette.

TrueColorImage: each pixel is 4 bytes, red, green, blue, and alpha inline.

If your image data isn't already read, you might load it as a different thing instead with loadImageFromMemory. It all depends what format you are starting with.

>    writeln(guessImageFormatFromMemory(tci.imageData.bytes)); // "Unknown"

That function is also for reading files, not raw bytes like inside the classes. The classes are supposed to already be in a specific layout.


December 25, 2020
On 12/25/20 12:59 PM, vnr wrote:

> Is there a relatively simple way to do this?

I have a minimalist photo album program that resizes images with the help of the magickwand library:

  https://github.com/acehreli/alibum

It has only the magickwand bindings that I needed.

Ali

P.S. The program takes a directory of pictures and creates static html pages as an album, which includes thumbnails.

December 25, 2020
On Friday, 25 December 2020 at 20:59:03 UTC, vnr wrote:
> Hello 😺
>
> For a small "script" that generates printable files, I would need to change the size of an image (which is loaded into memory as an array of bytes) to shrink it to scale if it exceeds the A4 page size.
>
> To load the images into memory and generate a PDF, I use the "printed" package. It is not very provided but is sufficient for my use, I just need the resize option... Is there a relatively simple way to do this?
>
> Thank you.

printed use the DPI information in your image to set the target size. You do not necessarily need to change the pixels. Save your PNG / JPEG with proper DPI information.
December 27, 2020
On Friday, 25 December 2020 at 20:59:03 UTC, vnr wrote:
> Hello 😺
>
> For a small "script" that generates printable files, I would need to change the size of an image (which is loaded into memory as an array of bytes) to shrink it to scale if it exceeds the A4 page size.
>
> To load the images into memory and generate a PDF, I use the "printed" package. It is not very provided but is sufficient for my use, I just need the resize option... Is there a relatively simple way to do this?
>
> Thank you.

Hello,

I've updated `printed` to v1.0.1, you can now the call:

    /// Draws an image at the given position, with the given width and height.
    /// Both `width` and `height` must be provided.
    void drawImage(Image image, float x, float y, float width, float height);

http://printed.dpldocs.info/printed.canvas.irenderer.IRenderingContext2D.html
December 27, 2020
On Sunday, 27 December 2020 at 16:49:49 UTC, Guillaume Piolat wrote:
> On Friday, 25 December 2020 at 20:59:03 UTC, vnr wrote:
>> Hello 😺
>>
>> For a small "script" that generates printable files, I would need to change the size of an image (which is loaded into memory as an array of bytes) to shrink it to scale if it exceeds the A4 page size.
>>
>> To load the images into memory and generate a PDF, I use the "printed" package. It is not very provided but is sufficient for my use, I just need the resize option... Is there a relatively simple way to do this?
>>
>> Thank you.
>
> Hello,
>
> I've updated `printed` to v1.0.1, you can now the call:
>
>     /// Draws an image at the given position, with the given width and height.
>     /// Both `width` and `height` must be provided.
>     void drawImage(Image image, float x, float y, float width, float height);
>
> http://printed.dpldocs.info/printed.canvas.irenderer.IRenderingContext2D.html


Thank you very much for all your answers!
The one given at the beginning by Adam D. Ruppe was fine with me, but the update of printed is very appreciable, so I use successfully this last feature which is the easiest to use.
December 27, 2020
On Sunday, 27 December 2020 at 18:48:18 UTC, vnr wrote:
> The one given at the beginning by Adam D. Ruppe was fine with me,

fyi i think I am going to move that resize code from image.d to a more independent imageresize.d or something. Same repo. Obviously won't affect you if you already using it but for the future... tbh I forgot that code was there myself, so making it independent of the image file loaders will probably be nice.
December 27, 2020
On Friday, 25 December 2020 at 20:59:03 UTC, vnr wrote:
> Hello 😺
>
> For a small "script" that generates printable files, I would need to change the size of an image (which is loaded into memory as an array of bytes) to shrink it to scale if it exceeds the A4 page size.
>
> To load the images into memory and generate a PDF, I use the "printed" package. It is not very provided but is sufficient for my use, I just need the resize option... Is there a relatively simple way to do this?
>
> Thank you.

I use the trusty stb_image C libraries (bindings here https://code.dlang.org/packages/stb ), specifically the stbir_resize_* functions.