Page 1 of 1

[Resolved] Enlarge image without distorting it

Posted: Fri Jun 04, 2021 9:30 am
by firace
What would be a good way of enlarging a simple monochrome PNG image with right angles only (such as a typical QR code) without distorting it?

I have tried this approach, which introduces heavy shape distorsion, making the QR code unusable:

Code: Select all

InitNetwork()
UsePNGImageDecoder()

imageURL$ = "https://nsa40.casimages.com/img/2021/06/04/210604103322144271.png"

*queryresponse = ReceiveHTTPMemory(imageURL$)

img = CatchImage(#PB_Any, *queryresponse, MemorySize(*queryresponse))

CopyImage(img, imgEnlarged)


OpenWindow(0, 50, 0, 900, 300, "")


ImageGadget(350,   0,    0,  0, 0, ImageID(img))  

ResizeImage(imgEnlarged, ImageWidth(img)*1.2, ImageHeight(img)*1.2, #PB_Image_Raw)   ;;; enlarging by 120%


ImageGadget(351,   450,  0,  0, 0, ImageID(imgEnlarged))  


Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow 


Re: Enlarge image without distorting it

Posted: Fri Jun 04, 2021 9:37 am
by Keya
possibly try enlarging by 2.0 or 4.0 etc first, then reduce back to 1.2 ?
or you might need to look at the various upscaling algorithms

Re: Enlarge image without distorting it

Posted: Fri Jun 04, 2021 10:03 am
by firace
Nice idea! Will play around with it, thanks.

Re: Enlarge image without distorting it

Posted: Fri Jun 04, 2021 10:46 am
by BarryG
firace wrote: Fri Jun 04, 2021 9:30 amheavy shape distorsion, making the QR code unusable
QR codes are pretty much readable by any camera, even from distorted angles. They're not meant to be 100% perfect images when read, so I doubt resizing it will be a problem. As a test, I scanned both your original and resized "distorted" code with my phone's camera and they both linked to this webpage:

https://www.example.com/cs/000036864:ba ... 805712e88d

They also showed a block of text lines in the below format. Is that what both QR codes are meant to hold? If so, your resizing works and you have nothing to worry about.

Code: Select all

|000047104:3a62faf1617ef164083c2bbf8c457b9d56eb15ec
|047937600:6e6379c5bbaf0641d3cf7bc22eda8b90bde6d495
|006839696:0b3ade6e70f36202bf358045d8f89d9bd5968802
...

Re: Enlarge image without distorting it

Posted: Fri Jun 04, 2021 2:48 pm
by RASHAD
Hi
BarryG report that there is no any problem
But just in case try the next [For Windows]

Code: Select all

InitNetwork()
UsePNGImageDecoder()

imageURL$ = "https://nsa40.casimages.com/img/2021/06/04/210604103322144271.png"

*queryresponse = ReceiveHTTPMemory(imageURL$)

img = CatchImage(#PB_Any, *queryresponse, MemorySize(*queryresponse))

imgH = CopyImage_(ImageID(img),#IMAGE_BITMAP	,1.2*ImageWidth(img),1.2*ImageHeight(img),0)

OpenWindow(0, 50, 0, 900, 300, "")
ImageGadget(350,   0,    0,  0, 0, ImageID(img))
ImageGadget(351,   450,  0,  0, 0, imgH)  


Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow 

Re: Enlarge image without distorting it

Posted: Fri Jun 04, 2021 3:33 pm
by Saki
Have fun

Code: Select all

; Use on Windows DPI aware, this is needed for clear output on high scalings !

EnableExplicit

InitNetwork()
UsePNGImageDecoder()

Define imageURL$ = "https://nsa40.casimages.com/img/2021/06/04/210604103322144271.png"

Define *queryresponse = ReceiveHTTPMemory(imageURL$)

Define img = CatchImage(#PB_Any, *queryresponse, MemorySize(*queryresponse))

Define scaling_x.d=1.2
Define scaling_y.d=1.2

Define imgEnlarged=CreateImage(#PB_Any, ImageWidth(img)*DesktopResolutionX()*scaling_x, ImageHeight(img)*DesktopResolutionY()*scaling_y)

StartVectorDrawing(ImageVectorOutput(imgEnlarged))
ScaleCoordinates(DesktopResolutionX()*scaling_x, DesktopResolutionY()*scaling_y)
DrawVectorImage(ImageID(img))
StopVectorDrawing()

OpenWindow(0, 50, 0, 900, 300, "")

ImageGadget(350,   0,    0,  0, 0, ImageID(img))  

ImageGadget(351,   450,  0,  0, 0, ImageID(imgEnlarged))  


Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow 

Re: Enlarge image without distorting it

Posted: Fri Jun 04, 2021 5:23 pm
by firace
Thanks for all responses!

@BarryG, yes that's the expected output, but it seems some phones (including my own) can't always recognize the enlarged version

Saki's approach seems to help!

Re: Enlarge image without distorting it

Posted: Fri Jun 04, 2021 10:54 pm
by Saki
Yes, it's not that easy to do.
Otherwise, you always have to increase the size by a factor of 2, otherwise it will never work,
because something always has to be added somewhere to achieve the desired size.

The usual way with the QR code is to completely recreate it in the appropriate size.

Re: Enlarge image without distorting it

Posted: Sat Jun 05, 2021 1:37 am
by BarryG
firace wrote: Fri Jun 04, 2021 5:23 pmsome phones (including my own) can't always recognize the enlarged version
I'd say your phone needs a software update, or you should download a modern QR app that can handle codes.

As I said, QR codes are designed to be read from any angle. For example, these scan perfectly for my 3-year-old phone directly from my monitor, and without me rotating my phone to match (ie. just holding the camera vertical):

https://i.imgur.com/SM7njDO.jpg

https://i.imgur.com/K2jwKqC.jpg

Re: Enlarge image without distorting it

Posted: Sat Jun 05, 2021 9:55 am
by Saki
I don't know if he just wants to use that for a QR code.
It's just with a QR code that it should be as clean as possible to be readable even under bad conditions.
It also looks unprofessional, even if it still works.

Re: Enlarge image without distorting it

Posted: Sat Jun 05, 2021 5:46 pm
by Tenaja
firace wrote: Fri Jun 04, 2021 9:30 am What would be a good way of enlarging a simple monochrome PNG image with right angles only (such as a typical QR code) without distorting it?

I have tried this approach, which introduces heavy shape distorsion, making the QR code unusable:

Code: Select all

InitNetwork()
UsePNGImageDecoder()

imageURL$ = "https://nsa40.casimages.com/img/2021/06/04/210604103322144271.png"

*queryresponse = ReceiveHTTPMemory(imageURL$)

img = CatchImage(#PB_Any, *queryresponse, MemorySize(*queryresponse))

CopyImage(img, imgEnlarged)


OpenWindow(0, 50, 0, 900, 300, "")


ImageGadget(350,   0,    0,  0, 0, ImageID(img))  

ResizeImage(imgEnlarged, ImageWidth(img)*1.2, ImageHeight(img)*1.2, #PB_Image_Raw)   ;;; enlarging by 120%


ImageGadget(351,   450,  0,  0, 0, ImageID(imgEnlarged))  


Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow 

Have you searched the forum? A few years back there was a very impressive enlarger that left nearly no artifacts on cartoon style images with sharp edges.

Since qr codes also have sharp edges, I would presume it would also work for you.

Re: Enlarge image without distorting it

Posted: Sat Jun 05, 2021 6:00 pm
by Saki
@Tenaja
As sample.
Imagine you have an image with 100 vertical lines with a thickness of one pixel.
Now you want to enlarge it by 20%.
Then you may only add another line to 20 lines to enlarge the image 20%.
Then you have 80 lines with a width of one pixel and 20 lines with a width of two pixels.

Re: Enlarge image without distorting it

Posted: Sat Jun 05, 2021 11:03 pm
by Fig
If you want to decode a QRCode from a picture (scan, photo...), I suggest you use ZBAR in command line for that purpose...

Re: Enlarge image without distorting it

Posted: Sun Jun 06, 2021 6:58 am
by [blendman]
Hi
You could use sprite and zoomsprite (), with spritequality() set to 0.
I use it on all'my 2d applications, and it works great ;).

Re: Enlarge image without distorting it

Posted: Sun Jun 06, 2021 11:01 am
by firace
Thanks for all the good explanations and comments. I'm happy with the approaches suggested so I am marking this as resolved.